From d5dd6760ab0662dba007effad8e03bee0b6ab6fa Mon Sep 17 00:00:00 2001 From: tcnksm Date: Wed, 7 Oct 2015 21:22:28 +0900 Subject: [PATCH] Refactoring: make commom NewFlagSet function --- command/apply.go | 16 +--------------- command/design.go | 16 +--------------- command/list.go | 5 ++--- command/meta.go | 29 ++++++++++++++++++++++++++++- command/new.go | 16 +--------------- command/validate.go | 20 +++----------------- 6 files changed, 36 insertions(+), 66 deletions(-) diff --git a/command/apply.go b/command/apply.go index 394ffb8..fe52a02 100644 --- a/command/apply.go +++ b/command/apply.go @@ -1,10 +1,7 @@ package command import ( - "bufio" - "flag" "fmt" - "io" "os" "strings" @@ -28,8 +25,7 @@ func (c *ApplyCommand) Run(args []string) int { name string ) - uflag := flag.NewFlagSet("apply", flag.ContinueOnError) - uflag.Usage = func() { c.UI.Error(c.Help()) } + uflag := c.Meta.NewFlagSet("apply", c.Help()) uflag.StringVar(&frameworkStr, "framework", "", "framework") uflag.StringVar(&frameworkStr, "F", "", "framework (short)") @@ -44,16 +40,6 @@ func (c *ApplyCommand) Run(args []string) int { uflag.StringVar(&owner, "owner", "", "owner (Should only for test)") uflag.StringVar(&name, "name", "", "name (Should only for test)") - errR, errW := io.Pipe() - errScanner := bufio.NewScanner(errR) - uflag.SetOutput(errW) - - go func() { - for errScanner.Scan() { - c.UI.Error(errScanner.Text()) - } - }() - if err := uflag.Parse(args); err != nil { return 1 } diff --git a/command/design.go b/command/design.go index 12169da..4786e9e 100644 --- a/command/design.go +++ b/command/design.go @@ -1,10 +1,7 @@ package command import ( - "bufio" - "flag" "fmt" - "io" "os" "strings" @@ -33,8 +30,7 @@ func (c *DesignCommand) Run(args []string) int { frameworkStr string ) - uflag := flag.NewFlagSet("design", flag.ContinueOnError) - uflag.Usage = func() { c.UI.Error(c.Help()) } + uflag := c.Meta.NewFlagSet("design", c.Help()) uflag.Var((*CommandFlag)(&commands), "command", "command") uflag.Var((*CommandFlag)(&commands), "c", "command (short)") @@ -51,16 +47,6 @@ func (c *DesignCommand) Run(args []string) int { uflag.StringVar(&output, "output", "", "output") uflag.StringVar(&output, "O", "", "output (short)") - errR, errW := io.Pipe() - errScanner := bufio.NewScanner(errR) - uflag.SetOutput(errW) - - go func() { - for errScanner.Scan() { - c.UI.Error(errScanner.Text()) - } - }() - if err := uflag.Parse(args); err != nil { return 1 } diff --git a/command/list.go b/command/list.go index e60870f..a3e7bf8 100644 --- a/command/list.go +++ b/command/list.go @@ -17,9 +17,8 @@ type ListCommand struct { // Run lists all avairable frameworks. func (c *ListCommand) Run(args []string) int { - if len(args) > 0 { - msg := fmt.Sprintf("Invalid arguments: %s", strings.Join(args, " ")) - c.UI.Error(msg) + uflag := c.Meta.NewFlagSet("list", c.Help()) + if err := uflag.Parse(args); err != nil { return 1 } diff --git a/command/meta.go b/command/meta.go index ffa2359..54b81b3 100644 --- a/command/meta.go +++ b/command/meta.go @@ -1,6 +1,12 @@ package command -import "github.com/mitchellh/cli" +import ( + "bufio" + "flag" + "io" + + "github.com/mitchellh/cli" +) // ExitCodes const ( @@ -17,3 +23,24 @@ const ( type Meta struct { UI cli.Ui } + +// NewFlagSet generates commom flag.FlagSet +func (m *Meta) NewFlagSet(name string, helpText string) *flag.FlagSet { + flags := flag.NewFlagSet(name, flag.ContinueOnError) + + // Set usage function + flags.Usage = func() { m.UI.Error(helpText) } + + // Set error output to Meta.UI.Error + errR, errW := io.Pipe() + errScanner := bufio.NewScanner(errR) + flags.SetOutput(errW) + + go func() { + for errScanner.Scan() { + m.UI.Error(errScanner.Text()) + } + }() + + return flags +} diff --git a/command/new.go b/command/new.go index 55f3b44..5a9b855 100644 --- a/command/new.go +++ b/command/new.go @@ -1,10 +1,7 @@ package command import ( - "bufio" - "flag" "fmt" - "io" "os" "strings" @@ -29,8 +26,7 @@ func (c *NewCommand) Run(args []string) int { verbose bool ) - uflag := flag.NewFlagSet("new", flag.ContinueOnError) - uflag.Usage = func() { c.UI.Error(c.Help()) } + uflag := c.Meta.NewFlagSet("new", c.Help()) uflag.Var((*CommandFlag)(&commands), "command", "command") uflag.Var((*CommandFlag)(&commands), "c", "command (short)") @@ -50,16 +46,6 @@ func (c *NewCommand) Run(args []string) int { uflag.BoolVar(&verbose, "verbose", false, "verbose") uflag.BoolVar(&verbose, "V", false, "verbose (short)") - errR, errW := io.Pipe() - errScanner := bufio.NewScanner(errR) - uflag.SetOutput(errW) - - go func() { - for errScanner.Scan() { - c.UI.Error(errScanner.Text()) - } - }() - if err := uflag.Parse(args); err != nil { return 1 } diff --git a/command/validate.go b/command/validate.go index 77ebc0e..9dfe09b 100644 --- a/command/validate.go +++ b/command/validate.go @@ -1,10 +1,7 @@ package command import ( - "bufio" - "flag" "fmt" - "io" "os" "strings" @@ -20,26 +17,15 @@ type ValidateCommand struct { // Run validates template file func (c *ValidateCommand) Run(args []string) int { - uflag := flag.NewFlagSet("validate", flag.ContinueOnError) - uflag.Usage = func() { c.UI.Error(c.Help()) } - - errR, errW := io.Pipe() - errScanner := bufio.NewScanner(errR) - uflag.SetOutput(errW) - - go func() { - for errScanner.Scan() { - c.UI.Error(errScanner.Text()) - } - }() - + uflag := c.Meta.NewFlagSet("validate", c.Help()) if err := uflag.Parse(args); err != nil { return 1 } parsedArgs := uflag.Args() if len(parsedArgs) != 1 { - c.UI.Error("Invalid argument: Usage glic validate [options] FILE") + c.UI.Error("Invalid argument") + c.UI.Error(c.Help()) return 1 }