Skip to content

Commit

Permalink
Eliminate duplicated arguments
Browse files Browse the repository at this point in the history
The function `rmDup()` is added at the beginnings of the commands.
  • Loading branch information
chyeh committed Jan 20, 2017
1 parent da7e761 commit 8ec9d3c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func check(c *cobra.Command, args []string) error {
if len(args) == 0 {
return c.Usage()
}
args = g.RmDup(args)

if len(args) == 1 && args[0] == "all" {
for moduleName := range g.Modules {
Expand Down
2 changes: 2 additions & 0 deletions cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func restart(c *cobra.Command, args []string) error {
if len(args) == 0 {
return c.Usage()
}
args = g.RmDup(args)

for _, moduleName := range args {
if err := stop(c, []string{moduleName}); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func start(c *cobra.Command, args []string) error {
if len(args) == 0 {
return c.Usage()
}
args = g.RmDup(args)

if PreqOrderFlag {
args = g.PreqOrder(args)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func stop(c *cobra.Command, args []string) error {
if len(args) == 0 {
return c.Usage()
}
args = g.RmDup(args)

for _, moduleName := range args {
if !g.HasModule(moduleName) {
return fmt.Errorf("%s doesn't exist", moduleName)
Expand Down
20 changes: 20 additions & 0 deletions g/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,23 @@ func IsRunning(name string) bool {
}
return true
}

func RmDup(args []string) []string {
if len(args) == 0 {
return []string{}
}
if len(args) == 1 {
return args
}

ret := []string{}
isDup := make(map[string]bool)
for _, arg := range args {
if isDup[arg] == true {
continue
}
ret = append(ret, arg)
isDup[arg] = true
}
return ret
}
27 changes: 27 additions & 0 deletions g/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ func TestPreqOrder(t *testing.T) {
}
}

func TestRmDup(t *testing.T) {
tests := []struct {
input []string
expected []string
}{
{[]string{"2nd-module", "1st-module"}, []string{"2nd-module", "1st-module"}},
{[]string{"2nd-module", "1st-module", "1st-module"}, []string{"2nd-module", "1st-module"}},
{[]string{"2nd-module", "2nd-module", "1st-module"}, []string{"2nd-module", "1st-module"}},
{[]string{"1st-module", "2nd-module", "1st-module"}, []string{"1st-module", "2nd-module"}},
{[]string{"2nd-module", "1st-module", "2nd-module"}, []string{"2nd-module", "1st-module"}},
{[]string{"2nd-module", "2nd-module", "1st-module", "3rd-module"}, []string{"2nd-module", "1st-module", "3rd-module"}},
{[]string{"2nd-module", "1st-module", "1st-module", "3rd-module"}, []string{"2nd-module", "1st-module", "3rd-module"}},
{[]string{"2nd-module", "1st-module", "3rd-module", "3rd-module"}, []string{"2nd-module", "1st-module", "3rd-module"}},
{[]string{"2nd-module", "1st-module", "2nd-module", "1st-module", "3rd-module"}, []string{"2nd-module", "1st-module", "3rd-module"}},
{[]string{"2nd-module", "1st-module", "2nd-module", "3rd-module"}, []string{"2nd-module", "1st-module", "3rd-module"}},
{[]string{"2nd-module", "1st-module", "3rd-module", "1st-module"}, []string{"2nd-module", "1st-module", "3rd-module"}},
}
for i, v := range tests {
actual := RmDup(v.input)
expected := v.expected
t.Logf("Check case %d: %s(actual) == %s(expected)", i, actual, expected)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Error on case %d: %s(actual) != %s(expected)", i, actual, expected)
}
}
}

func TestMain(m *testing.M) {
setup()
code := m.Run()
Expand Down

0 comments on commit 8ec9d3c

Please sign in to comment.