Skip to content

Commit

Permalink
global options fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jrperritt committed Jul 28, 2015
1 parent a59c378 commit e5fe557
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 54 deletions.
5 changes: 1 addition & 4 deletions commandoptions/cliopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ type Cred struct {

func CLIopts(c *cli.Context, have map[string]Cred, need map[string]string) {
for opt := range need {
if c.GlobalIsSet(opt) {
have[opt] = Cred{Value: c.GlobalString(opt), From: "command-line"}
delete(need, opt)
} else if c.IsSet(opt) {
if c.IsSet(opt) {
have[opt] = Cred{Value: c.String(opt), From: "command-line"}
delete(need, opt)
}
Expand Down
4 changes: 1 addition & 3 deletions commandoptions/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (

func ConfigFile(c *cli.Context, have map[string]Cred, need map[string]string) error {
var profile string
if c.GlobalIsSet("profile") {
profile = c.GlobalString("profile")
} else if c.IsSet("profile") {
if c.IsSet("profile") {
profile = c.String("profile")
}

Expand Down
12 changes: 4 additions & 8 deletions handler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ func (ctx *Context) handleGlobalOptions() error {
}

var outputFormat string
if ctx.CLIContext.GlobalIsSet("output") {
outputFormat = ctx.CLIContext.GlobalString("output")
} else if ctx.CLIContext.IsSet("output") {
if ctx.CLIContext.IsSet("output") {
outputFormat = ctx.CLIContext.String("output")
} else if value, ok := defaultKeysHash["output"]; ok && value != "" {
outputFormat = value
Expand All @@ -141,22 +139,20 @@ func (ctx *Context) handleGlobalOptions() error {
return fmt.Errorf("Invalid value for `output` flag: '%s'. Options are: json, csv, table.", outputFormat)
}

if ctx.CLIContext.IsSet("no-header") || ctx.CLIContext.GlobalIsSet("no-header") {
if ctx.CLIContext.IsSet("no-header") {
ctx.GlobalOptions.noHeader = true
} else if value, ok := defaultKeysHash["no-header"]; ok && value != "" {
ctx.GlobalOptions.noHeader = true
}

if ctx.CLIContext.IsSet("no-cache") || ctx.CLIContext.GlobalIsSet("no-cache") {
if ctx.CLIContext.IsSet("no-cache") {
ctx.GlobalOptions.noCache = true
} else if value, ok := defaultKeysHash["no-cache"]; ok && value != "" {
ctx.GlobalOptions.noCache = true
}

var logLevel string
if ctx.CLIContext.GlobalIsSet("log") {
logLevel = ctx.CLIContext.GlobalString("log")
} else if ctx.CLIContext.IsSet("log") {
if ctx.CLIContext.IsSet("log") {
logLevel = ctx.CLIContext.String("log")
} else if value, ok := defaultKeysHash["log"]; ok && value != "" {
logLevel = value
Expand Down
39 changes: 0 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"text/tabwriter"
"text/template"

"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/commands/blockstoragecommands"
"github.com/jrperritt/rack/commands/filescommands"
"github.com/jrperritt/rack/commands/networkscommands"
Expand All @@ -33,10 +32,6 @@ OPTIONS:
app.Usage = Usage()
app.EnableBashCompletion = true
app.Commands = Cmds()
app.Flags = commandoptions.GlobalFlags()
app.BashComplete = func(c *cli.Context) {
completeGlobals(globalOptions(app))
}
app.Before = func(c *cli.Context) error {
//fmt.Printf("c.Args: %+v\n", c.Args())
return nil
Expand Down Expand Up @@ -94,40 +89,6 @@ func Cmds() []cli.Command {
}
}

// completeGlobals returns the options for completing global flags.
func completeGlobals(vals []interface{}) {
for _, val := range vals {
switch val.(type) {
case cli.StringFlag:
fmt.Println("--" + val.(cli.StringFlag).Name)
case cli.IntFlag:
fmt.Println("--" + val.(cli.IntFlag).Name)
case cli.BoolFlag:
fmt.Println("--" + val.(cli.BoolFlag).Name)
case cli.Command:
fmt.Println(val.(cli.Command).Name)
default:
continue
}
}
}

// globalOptions returns the options (flags and commands) that can be used after
// `rack` in a command. For example, `rack --json`, `rack servers`, and
// `rack --json servers` are all legitimate command prefixes.
func globalOptions(app *cli.App) []interface{} {
var i []interface{}
globalFlags := commandoptions.GlobalFlags()
for _, globalFlag := range globalFlags {
i = append(i, globalFlag)
}

for _, cmd := range app.Commands {
i = append(i, cmd)
}
return i
}

func printHelp(out io.Writer, templ string, data interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
Expand Down

0 comments on commit e5fe557

Please sign in to comment.