• Stars
    star
    925
  • Rank 47,433 (Top 1.0 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The easy way to build Golang command-line application.

gcli

GitHub release Travis MIT License Go Documentation

gcli generates a skeleton (codes and its directory structure) you need to start building Command Line Interface (CLI) tool by Golang right out of the box. You can use your favorite CLI framework.

Why ?

Why you need gcli? Because you should focus on writing core function of CLI, not on interface. During developing CLI tool by Golang, you may find you're writing the chunk of boilerplate code for interfaces. Stop writing the same codes every time. gcli generates them and save you a large amount of time by writing such code. This is like Rails scaffold. Not only that, gcli know the best practices of golang CLI framework library which you want to use. Generated codes follows the most ideal way of using that framework, and you don't need to know about that. See the frameworks it supports now.

Demo

The following demo shows creating todo CLI application which has add, list and delete command with mitchellh/cli (Which is used for Hashicorp products) with one command. As you can see, the generated codes are go build-able from beginning.

gif

And this video shows creating same todo CLI application with design & apply commands. This is the other way to start building new CLI application. First, it starts with creating design file by design command. In this file, you can define, CLI name, description of the CLI , framework you want to use, and commands & flags with its usages. After editing, it executes apply command to generating a project from that design file.

Usage

gcli is single command-line application. This application then takes subcommands. To check the all available commands,

$ gcli help

To get help for any specific subcommand, run it with the -h flag.

gcli has 2 main subcommand to generate the project. The one is the new command, the other is the design & apply commands. The former is for generating the project by command line one-liner, the latter is for when you want to design it in your editor before generating (It generates design file and you can generate project based on it). The following section explain, how to use these commands.

new command

The new command tells gcli to generate CLI project with command-line one-liner,

$ gcli new [options] NAME

You must provides project name (NAME), the name will be the directory name it includes all codes and be the default binary name. By default, gcli creates a project under $GOPATH/github.com/<username> (If you don't provide username via option, it uses github.user or user.name in .gitconfig file). In option, you can set subcommand or flag it has and its description. You can also set your favorite CLI framework there. The followings are all available opntions,

-command=name, -c           Command name which you want to add.
                            This is valid only when cli pacakge support commands.
                            This can be specified multiple times. Synopsis can be
                            set after ":". Namely, you can specify command by
                            -command=NAME:SYNOPSYS. Only NAME is required.
                            You can set multiple variables at same time with ","
                            separator.

-flag=name, -f              Global flag option name which you want to add.
                            This can be specified multiple times. By default, flag type
                            is string and its description is empty. You can set them,
                            with ":" separator. Namaly, you can specify flag by
                            -flag=NAME:TYPE:DESCIRPTION. Order must be flow  this and
                            TYPE must be string, bool or int. Only NAME is required.
                            You can set multiple variables at same time with ","
                            separator.

-framework=name, -F         Cli framework name. By default, gcli use "codegangsta/cli"
                            To check cli framework you can use, run 'gcli list'.
                            If you set invalid framework, it will be failed.

-owner=name, -o             Command owner (author) name. This value is also used for
                            import path name. By default, owner name is extracted from
                            ~/.gitconfig variable.

-skip-test, -T              Skip generating *_test.go file. By default, gcli generates
                            test file If you specify this flag, gcli will not generate
                            test files.

For example, to todo CLI application which has add, list and delete command with mitchellh/cli,

$ gcli new -F mitchellh_cli -c add -c list -c delete todo

design & apply command

The design command tells gcli to prepare design template file (.toml). The design file defines all necessary information to generate CLI application. Some fields are filled with the ideal default value, and some have empty value. You can fill that empty filed with your favorite editor with thinking like what interface that should have or description of that and so on. You can see sample template file sample.toml.

After design, use apply command and tells gcli to generate CLI project based on the design file. The following describes this workflow.

First, generate design template file by design command,

$ gcli design [options] NAME

You must provides project name (NAME). In option, you can set subcommand or flag it has and its description. You can also set your favorite CLI framework there. You can edit these values in design file later.

Then, edit design file by your favorite $EDITOR.

$ $EDITOR <NAME>-design.toml

After that validate design by validate command to check required fields are filled,

$ gcli validate <NAME>-design.toml

Finnaly, generate CLI project with that design file by apply command,

$ gcli apply <NAME>-desigon.toml

The video for this workflow is available on Vimeo.

Frameworks

There are many framework (package) for buidling command line application by golang. For example, one of the most famous frameworks is codegangsta/cli. The framework helps you not writing many codes. But still you need to write many boilerplate code for that framework. And there are different way to use that framework and learning the ideal way to use is waste of time. gcli writes out with following the best practice for that framework (learn from famous tool that is built with that framework).

gcli can generate 2 types of CLI pattern. The one is sub-command pattern, the other is flag pattern. The former is flexible and you can add many behavior in one command application. The later is for simple application. You can check the all available frameworks by list command,

$ gcli list

To change framework, you can use -framework or -F option with the framework name. This option can be used for new, design and apply command. By default, codegangsta_cli will be used.

The following section will explain sub-command pattern and flag pattern.

Sub-Command

Sub-Command pattern is the pattern that executable takes sub-command for change its behavior. git command is one example for this pattern. It takes push, pull subcommands. gcli is also this pattern. gcli supports the following frameworks for the command pattern.

Name Sample projects
codegangsta_cli docker machine
mitchellh_cli consul, terraform
go_cmd go

(go_cmd is not framework. It only uses standard package. It generates same struct and functions that go command uses.)

Flag

Flag pattern is the pattern that executable has flag options for changing its behavior. For example, grep command is this pattern. Now gcli only supports the official flag package for this pattern.

For example, to create command it has -ignore-case option and context option (your own grep),

$ gcli new -F flag -flag=i:Bool -flag=C:Int grep

Installation

To install, use go get and make install. We tag versions so feel free to checkout that tag and compile.

$ go get -d github.com/tcnksm/gcli
$ cd $GOPATH/src/github.com/tcnksm/gcli
$ make install 

Contribution

  1. Fork (https://github.com/tcnksm/gcli/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the make test command and confirm that it passes
  6. Create a new Pull Request

Author

Taichi Nakashima

More Repositories

1

ghr

Upload multiple artifacts to GitHub Release in parallel
Go
1,229
star
2

go-httpstat

Tracing golang HTTP request latency
Go
416
star
3

docker-alias

My Docker alias and functions
Shell
371
star
4

go-slack-interactive

Sample slack bot which uses slack interactive message (button & menue) written in Golang
Go
306
star
5

gotests

[Archived] A tool to generate Go test functions from the given source code like gofmt
Go
264
star
6

go-input

Go package for ideal tty prompt
Go
253
star
7

awesome-container

A curated list of awesome container technologies and services
172
star
8

go-latest

Simple way to check version is latest or not from various sources in Golang
Go
136
star
9

license

Generate LICENSE file you want
Go
117
star
10

go-casper

Cache aware server push (CASPer) in Golang
Go
108
star
11

go-binary-only-package

Sample of Go1.7 Binary-Only Packages
Go
79
star
12

dockerfile-rbenv

Dockerfile to build Image which is installed muliple versions of ruby
74
star
13

go-gitconfig

Use gitconfig values in Golang.
Go
59
star
14

boot2kubernetes

Run single node kubernetes cluseter in one command
Go
38
star
15

dockerfile-gox

Docekerfile for gox, cross-compiling golang project parallelly
Dockerfile
36
star
16

dotfiles

@tcnksm does dotfiles
Shell
30
star
17

waypoint-plugin-kustomize

Experimental implementation of waypoint plugin for kustomize
Go
30
star
18

talks

@tcnksm talks at conference or on Podcast
Go
27
star
19

rbdock

Generate Dockerfile for Ruby or Rails, Sinatra.
Ruby
24
star
20

deeeet.com

@tcnksm writes blog
SCSS
24
star
21

dutyme

Assign PagerDuty on-call to you while operation
Go
23
star
22

go-algorithms

Algorithms and data structure by golang
Go
21
star
23

go-distributed-tracing

Sample kubernetes config and golang apps to try distributed tracing with Stackdriver
Go
20
star
24

vagrant-digitalocean-coreos

Create CoreOS cluster on DigitalOcean by Vagrant
19
star
25

go-crypto

Play with golang crypto package
Go
19
star
26

go-holidayjp

Go package for detecting holiday in Japan
Go
15
star
27

gox-server

Golang cross-compile on Heroku
Go
15
star
28

docker-link-pattern

Docker container link patterns
Ruby
13
star
29

alexa-irkit-ac

A sample implementation of custom Alexa Smart Home Skill by Golang
Go
12
star
30

sudo-controller

Kubernetes controller which allows you to create time bound role binding with approval.
Go
11
star
31

vagrant-secret

Enable to read external secret file for configuration
Ruby
10
star
32

go-reconcile

Super tiny go package which does reconcile planning
Go
10
star
33

wercker-step-ghr

Wercker step for tcnksm/ghr, create Github Release and uploading artifacts
Shell
10
star
34

go-irkit

The unofficial golang client for IRKit
Go
9
star
35

vagrant-pushover

Add pushover notification to your vagrant
Ruby
9
star
36

docc

docc open your project document (GitHub page or README)
Go
9
star
37

dockerfile-centos-buildpack-deps

Docker CentOS Image for buildpack-deps
9
star
38

heroku-docker-registry

Run Docker Registry on Heroku
Shell
8
star
39

yo

Yo to your twitter friend from CLI
Go
8
star
40

misc

The collection of programs which is not general consumption
Go
8
star
41

rspec-logstash-filter

RSpec logstash filter on docker container
Shell
7
star
42

bash-init

The easy way to start building bash command-line application.
Go
7
star
43

docker-meetup-4-demo

Demo for docker meetup #4
Shell
7
star
44

dockerfile-single-kafka

Dockerfile for single node kafka
Shell
7
star
45

vagrant-runc

Play with runC on Ubuntu14.04 by Vagrant
7
star
46

wercker-box-gox

Wercker box for mitchellh/gox, cross-compiling golang project parallelly
6
star
47

go-httptraceutils

Small Go helper package for logging out each hook info of httptrace
Go
6
star
48

wercker-step-goveralls

Wercker step for mattn/goveralls, integrate with coveralls.io
Shell
5
star
49

go-context101

Sample codes to understand why context package
Go
5
star
50

go-distribution-scripts

Shellscripts for cross-compiling, packaging and uploading golang tool
Shell
5
star
51

hubot-google-trend

Hubot script to show recent trend words on Google
CoffeeScript
5
star
52

wercker-step-zip

Wercker step for packaging directories
Shell
4
star
53

dockerfiles

@tcnksm does Dockerfile
4
star
54

wercker-step-gox

Wercker step for mitchellh/gox, cross-compiling golang project parallelly
Shell
4
star
55

vagrant-appc

Play with CoreOS/Rocket in Vagrant
4
star
56

hubot-cron-json

Manage hubot cron job defined by json configuration file
CoffeeScript
4
star
57

oh-my-zsh-beer-theme

oh-my-zsh 🍺 theme
3
star
58

tf-dnsimple-gh-pages

Setup custom apex domain for your GitHub pages with Terraform
HCL
3
star
59

coreos-meetup-tokyo

CoreOS meetup tokyo
3
star
60

dist-ghr

Redirect distribution for
Go
2
star
61

init

Initial template
Go
2
star
62

random_japanese_string

Generate random japanese strings
Ruby
2
star
63

retrobot

tweet what @tcnksm tweet 1 year ago
Ruby
2
star
64

dockerfile-go-release

Dockerfile for cross-compiling & uploading artifacts to Github Release page. Compiling and uploading are executed parallelly.
Shell
2
star
65

scripts

Automation scripts for my daily job/life
Shell
1
star
66

go-playground

My Go Playground
Go
1
star
67

tcnksm.github.io

HTML
1
star
68

homebrew-ghr

A Homebrew formula for tcnksm/ghr
Ruby
1
star
69

tcnksm

README for @tcnksm
Go
1
star
70

ghr-demo

GHR demo
Go
1
star
71

go-cf-manifest

Go package for handling CloudFoundry manifest.yml
Go
1
star
72

dockerfile-download-deb

Dockerfile for donwloading deb package
1
star
73

writing.deeeet.com

My blog powered by octopress
Ruby
1
star