├── Makefile ├── README.md ├── VERSION ├── cmd └── cli │ └── main.go ├── commands ├── commands.go ├── config.go └── hello.go └── config └── config.go /Makefile: -------------------------------------------------------------------------------- 1 | COMMIT_ID=$(shell git rev-parse --short HEAD) 2 | VERSION=$(shell cat VERSION) 3 | 4 | NAME=cli 5 | 6 | all: clean build 7 | 8 | clean: 9 | @echo ">> cleaning..." 10 | @rm -f cli 11 | 12 | build: clean 13 | @echo ">> building..." 14 | @echo "Commit: $(COMMIT_ID)" 15 | @echo "Version: $(VERSION)" 16 | @go build -ldflags "-X main.Version=$(VERSION) -X main.CommitId=$(COMMIT_ID)" ./cmd/... 17 | 18 | install: 19 | @go install -ldflags "-X main.Version=$(VERSION) -X main.CommitId=$(COMMIT_ID)" ./cmd/... 20 | 21 | .PHONY: all clean build install 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cli Boilerplate 2 | 3 | A boiler plate project for the popular `github.com/urfave/cli` previously `codegangsta/cli`. 4 | 5 | ## Install 6 | 7 | `go get github.com/robtec/cli-boilerplate` 8 | 9 | or fork this repo 10 | 11 | ## Build or Install using make 12 | 13 | * `make install` - adds the binary to `$GOPATH/bin` 14 | * `make build` - builds the binary 15 | 16 | The `Makefile` builds the binary and adds the short git commit id and the version to the final build. 17 | 18 | ## Customise 19 | 20 | The binary is named `cli`, the parent directory of `main.go` in `cmd/cli/` 21 | You can rename the directory to change the name of the final binary 22 | 23 | ## Extras 24 | 25 | Checkout the `github.com/robtec/cli-boilderplate/config` package for saving configs to `$HOME/.cli` 26 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.1 -------------------------------------------------------------------------------- /cmd/cli/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/robtec/cli-boilerplate/commands" 8 | "github.com/urfave/cli" 9 | ) 10 | 11 | var ( 12 | Version = "0" 13 | CommitId = "0" 14 | ) 15 | 16 | func main() { 17 | 18 | app := cli.NewApp() 19 | app.Name = "My Cli Example" 20 | app.Usage = "Demo of urfave Cli Sweetness!" 21 | 22 | app.Version = fmt.Sprintf("%s - %s", Version, CommitId) 23 | 24 | app.Commands = commands.Commands 25 | 26 | app.Run(os.Args) 27 | } 28 | -------------------------------------------------------------------------------- /commands/commands.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "github.com/urfave/cli" 5 | ) 6 | 7 | var Commands = []cli.Command{ 8 | { 9 | Name: "say-hello", 10 | Usage: "Say hello command", 11 | Action: sayHello, 12 | Flags: []cli.Flag{ 13 | cli.StringFlag{ 14 | Name: "name, n", 15 | Value: "Jim", 16 | }, 17 | }, 18 | }, 19 | { 20 | Name: "config", 21 | Usage: "test config", 22 | Action: testconfig, 23 | }, 24 | } 25 | -------------------------------------------------------------------------------- /commands/config.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/robtec/cli-boilerplate/config" 7 | "github.com/urfave/cli" 8 | ) 9 | 10 | type Data struct { 11 | Name string 12 | Summary string 13 | } 14 | 15 | func testconfig(c *cli.Context) error { 16 | conf := config.NewConfig() 17 | conf.Save(Data{"54343", "ghlglg"}) 18 | 19 | d, _ := conf.Load() 20 | 21 | fmt.Print(d) 22 | 23 | return nil 24 | } 25 | -------------------------------------------------------------------------------- /commands/hello.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | "github.com/urfave/cli" 6 | ) 7 | 8 | func sayHello(c *cli.Context) error { 9 | fmt.Println("Hello",c.String("name")) 10 | return nil 11 | } -------------------------------------------------------------------------------- /config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "bufio" 5 | "os" 6 | "path/filepath" 7 | "sync" 8 | 9 | "github.com/BurntSushi/toml" 10 | "github.com/mitchellh/go-homedir" 11 | ) 12 | 13 | type Config struct { 14 | mux sync.Mutex 15 | } 16 | 17 | func NewConfig() *Config { 18 | return &Config{} 19 | } 20 | 21 | var filename = ".cli" 22 | 23 | func (c *Config) Load() (interface{}, error) { 24 | c.mux.Lock() 25 | defer c.mux.Unlock() 26 | 27 | var data interface{} 28 | 29 | filepath := getFilePath() 30 | 31 | _, err := toml.DecodeFile(filepath, &data) 32 | 33 | return data, err 34 | } 35 | 36 | func (c *Config) Save(data interface{}) error { 37 | c.mux.Lock() 38 | defer c.mux.Unlock() 39 | 40 | filepath := getFilePath() 41 | createFile(filepath) 42 | 43 | var file, err = os.OpenFile(filepath, os.O_RDWR, 0644) 44 | 45 | w := bufio.NewWriter(file) 46 | err = toml.NewEncoder(w).Encode(data) 47 | 48 | return err 49 | } 50 | 51 | func getFilePath() string { 52 | 53 | home, _ := homedir.Dir() 54 | filepath := filepath.Join(home, filename) 55 | 56 | return filepath 57 | } 58 | 59 | func createFile(path string) error { 60 | 61 | var _, err = os.Stat(path) 62 | 63 | if os.IsNotExist(err) { 64 | file, _ := os.Create(path) 65 | defer file.Close() 66 | } 67 | return err 68 | } 69 | --------------------------------------------------------------------------------