Skip to content

Commit

Permalink
Merge pull request #30 from tcnksm/refactoring
Browse files Browse the repository at this point in the history
Refactoring: make commom NewFlagSet function
  • Loading branch information
tcnksm committed Oct 7, 2015
2 parents 0193913 + d5dd676 commit f07294e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 66 deletions.
16 changes: 1 addition & 15 deletions command/apply.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package command

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"

Expand All @@ -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)")
Expand All @@ -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
}
Expand Down
16 changes: 1 addition & 15 deletions command/design.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package command

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -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)")
Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
29 changes: 28 additions & 1 deletion command/meta.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package command

import "github.com/mitchellh/cli"
import (
"bufio"
"flag"
"io"

"github.com/mitchellh/cli"
)

// ExitCodes
const (
Expand All @@ -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
}
16 changes: 1 addition & 15 deletions command/new.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package command

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"

Expand All @@ -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)")
Expand All @@ -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
}
Expand Down
20 changes: 3 additions & 17 deletions command/validate.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package command

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"

Expand All @@ -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
}

Expand Down

0 comments on commit f07294e

Please sign in to comment.