Skip to content

Commit

Permalink
Merge pull request #22 from tcnksm/global-option
Browse files Browse the repository at this point in the history
Global option
  • Loading branch information
tcnksm committed Jul 21, 2015
2 parents 49bf356 + 7380256 commit 0a95f6d
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
COMMIT = $$(git describe --always)
DEBUG_FLAG = $(if $(DEBUG),-debug)

deps:
Expand All @@ -8,11 +9,11 @@ deps:

build: deps
cd skeleton; go-bindata -pkg="skeleton" resource/...
go build -o bin/gcli
go build -ldflags "-X main.GitCommit \"$(COMMIT)\"" -o bin/gcli

install: deps
cd skeleton; go-bindata -pkg="skeleton" resource/...
go install
go install -ldflags "-X main.GitCommit \"$(COMMIT)\""

test: build
./test.sh
Expand Down
6 changes: 3 additions & 3 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ This is road map of `gcli`, what I'm doing for next release and planing for futu

## 0.2.1

- `godoc`
- More document for flag
- Global option for command pattern
- **Done**: Global option for command pattern
- **Done**: Usge `flag.Var`
- **Done**: Generate bash script
- **Done**: More verbose outputs in `skeleton` package
Expand All @@ -20,6 +18,8 @@ This is road map of `gcli`, what I'm doing for next release and planing for futu
- `-gb` option
- Support LICENSE generation
- Support [jessevdk/go-flags](https://github.com/jessevdk/go-flags)
- `godoc`
- More document for flag

## 0.3.0

Expand Down
10 changes: 10 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func Run(args []string) int {
// RunCustom execute mitchellh/cli and return its exit code.
func RunCustom(args []string, commands map[string]cli.CommandFactory) int {

for _, arg := range args {
if arg == "-v" || arg == "-version" || arg == "--version" {
newArgs := make([]string, len(args)+1)
newArgs[0] = "version"
copy(newArgs[1:], args)
args = newArgs
break
}
}

cli := &cli.CLI{
Args: args[1:],
Commands: commands,
Expand Down
1 change: 1 addition & 0 deletions skeleton/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ cli is the library that powers the CLI for Packer, Serf, and Consul.
{"resource/tmpl/mitchellh_cli/cli.go.tmpl", "cli.go"},
{"resource/tmpl/mitchellh_cli/commands.go.tmpl", "commands.go"},
{"resource/tmpl/mitchellh_cli/command/meta.go.tmpl", "command/meta.go"},
{"resource/tmpl/mitchellh_cli/command/version.go.tmpl", "command/version.go"},
},
CommandTemplates: []Template{
{"resource/tmpl/mitchellh_cli/command/command.go.tmpl", "command/{{ .Name }}.go"},
Expand Down
13 changes: 10 additions & 3 deletions skeleton/resource/tmpl/codegangsta_cli/commands.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import (
"github.com/{{ .Owner }}/{{ .Name }}/command"
)

var GlobalFlags = []cli.Flag{}
var GlobalFlags = []cli.Flag{
{{ range .Flags }}cli.{{ title .TypeString }}Flag{
EnvVar: "ENV_{{ toUpper .Name }}",
Name: "{{ .LongName }}",
{{ if eq .TypeString "string" }}Value: {{ .Default }}, {{ end }}
Usage: "{{ .Description }}",
},
{{ end }}
}

var Commands = []cli.Command{
{{ range .Commands }}
{
{{ range .Commands }}{
Name: "{{ .Name }}",
Usage: "{{ .Synopsis }}",
Action: command.Cmd{{ title .Name }},
Expand Down
12 changes: 12 additions & 0 deletions skeleton/resource/tmpl/mitchellh_cli/cli.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ func Run(args []string) int {

func RunCustom(args []string, commands map[string]cli.CommandFactory) int {

// Get the command line args. We shortcut "--version" and "-v" to
// just show the version.
for _, arg := range args {
if arg == "-v" || arg == "-version" || arg == "--version" {
newArgs := make([]string, len(args)+1)
newArgs[0] = "version"
copy(newArgs[1:], args)
args = newArgs
break
}
}

cli := &cli.CLI{
Args: args[1:],
Commands: commands,
Expand Down
34 changes: 34 additions & 0 deletions skeleton/resource/tmpl/mitchellh_cli/command/version.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package command

import (
"bytes"
"fmt"
)

type VersionCommand struct {
Meta

Name string
Version string
Revision string
}

func (c *VersionCommand) Run(args []string) int {
var versionString bytes.Buffer

fmt.Fprintf(&versionString, "%s version %s", c.Name, c.Version)
if c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
}

c.Ui.Output(versionString.String())
return 0
}

func (c *VersionCommand) Synopsis() string {
return fmt.Sprintf("Print %s version and quit",c.Name)
}

func (c *VersionCommand) Help() string {
return ""
}
11 changes: 9 additions & 2 deletions skeleton/resource/tmpl/mitchellh_cli/commands.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import (

func Commands(meta *command.Meta) map[string]cli.CommandFactory {
return map[string]cli.CommandFactory{
{{ range .Commands }}
"{{ .Name }}": func() (cli.Command, error) {
{{ range .Commands }}"{{ .Name }}": func() (cli.Command, error) {
return &command.{{ title .Name }}Command{
Meta: *meta,
}, nil
},
{{ end }}
"version": func() (cli.Command, error) {
return &command.VersionCommand{
Meta: *meta,
Version: Version,
Revision: GitCommit,
Name: Name,
}, nil
},
}
}
5 changes: 5 additions & 0 deletions skeleton/resource/tmpl/mitchellh_cli/version.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ package main

const Name string = "{{ .Name }}"
const Version string = "{{ .Version }}"

// GitCommit describes latest commit hash.
// This value is extracted by git command when building.
// To set this from outside, use go build -ldflags "-X main.GitCommit \"$(COMMIT)\""
var GitCommit string
5 changes: 3 additions & 2 deletions skeleton/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func execute(content string, wr io.Writer, data interface{}) error {

func funcMap() template.FuncMap {
return template.FuncMap{
"date": dateFunc(),
"title": strings.Title,
"date": dateFunc(),
"title": strings.Title,
"toUpper": strings.ToUpper,
}
}
2 changes: 2 additions & 0 deletions tests/command_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func TestNew_command_frameworks(t *testing.T) {
"new",
"-framework", tt.framework,
"-owner", owner,
"-flag=verbose:bool:'Run verbose mode'",
"-flag=username:string:'Username'",
"-command=add:'Add new task'",
"-command=list:'List tasks'",
"-command=delete:'Delete specified task'",
Expand Down

0 comments on commit 0a95f6d

Please sign in to comment.