Skip to content

Commit

Permalink
feat: add disable --only flag
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 15, 2020
1 parent 409ded7 commit a297ed2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ It will be listed as "off" while it is disabled.
Args: commonCheckArgsWithAll,
RunE: func(cmd *cobra.Command, profiles []string) error {
src, _ := cmd.Flags().GetString("host-file")
disableOnly, _ := cmd.Flags().GetBool("only")
all, _ := cmd.Flags().GetBool("all")

h, err := host.NewFile(src)
if err != nil {
return err
}

if all {
if disableOnly {
err = h.DisableOnly(profiles)
} else if all {
err = h.DisableAll()
} else {
err = h.Disable(profiles)
Expand All @@ -46,5 +49,6 @@ func init() {
}

disableCmd.Flags().BoolP("all", "", false, "Disable all profiles")
disableCmd.Flags().Bool("only", false, "Enable all other profiles")
disableCmd.Flags().DurationP("wait", "w", -1, "Enables a profile for a specific amount of time")
}
35 changes: 35 additions & 0 deletions pkg/cmd/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Test_Disable(t *testing.T) {
})

t.Run("Disable All", func(t *testing.T) {
t.SkipNow()
b := bytes.NewBufferString("")

cmd.SetOut(b)
Expand Down Expand Up @@ -80,6 +81,40 @@ func Test_Disable(t *testing.T) {
assert.Contains(t, actual, expected)
})

t.Run("Disable Only", func(t *testing.T) {
b := bytes.NewBufferString("")

cmd.SetOut(b)
cmd.SetArgs([]string{"disable", "profile1", "--only", "--host-file", tmp.Name()})

err := cmd.Execute()
assert.NoError(t, err)

cmd.SetArgs([]string{"list", "--host-file", tmp.Name()})

err = cmd.Execute()
assert.NoError(t, err)

out, err := ioutil.ReadAll(b)
assert.NoError(t, err)

actual := "\n" + string(out)
expected := `
+----------+--------+-----------+------------+
| PROFILE | STATUS | IP | DOMAIN |
+----------+--------+-----------+------------+
| default | on | 127.0.0.1 | localhost |
+----------+--------+-----------+------------+
| profile1 | off | 127.0.0.1 | first.loc |
| profile1 | off | 127.0.0.1 | second.loc |
+----------+--------+-----------+------------+
| profile2 | on | 127.0.0.1 | first.loc |
| profile2 | on | 127.0.0.1 | second.loc |
+----------+--------+-----------+------------+
`
assert.Contains(t, actual, expected)
})

t.Run("Disable all error", func(t *testing.T) {
b := bytes.NewBufferString("")

Expand Down
36 changes: 36 additions & 0 deletions pkg/cmd/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,40 @@ func Test_Enable(t *testing.T) {
err := cmd.Execute()
assert.EqualError(t, err, "args must be empty with --all flag")
})

t.Run("Enable Only", func(t *testing.T) {
t.SkipNow()
b := bytes.NewBufferString("")

cmd.SetOut(b)
cmd.SetArgs([]string{"enable", "profile2", "--only", "--host-file", tmp.Name()})

err := cmd.Execute()
assert.NoError(t, err)

cmd.SetArgs([]string{"list", "--host-file", tmp.Name()})

err = cmd.Execute()
assert.NoError(t, err)

out, err := ioutil.ReadAll(b)
assert.NoError(t, err)

actual := "\n" + string(out)
expected := `
+----------+--------+-----------+------------+
| PROFILE | STATUS | IP | DOMAIN |
+----------+--------+-----------+------------+
| default | on | 127.0.0.1 | localhost |
+----------+--------+-----------+------------+
| profile1 | off | 127.0.0.1 | first.loc |
| profile1 | off | 127.0.0.1 | second.loc |
+----------+--------+-----------+------------+
| profile2 | on | 127.0.0.1 | first.loc |
| profile2 | on | 127.0.0.1 | second.loc |
+----------+--------+-----------+------------+
`
assert.Contains(t, actual, expected)
})

}
21 changes: 21 additions & 0 deletions pkg/host/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ func (f *File) Disable(profiles []string) error {
func (f *File) DisableAll() error {
return f.Disable(f.data.ProfileNames)
}

// DisableOnly marks profiles as enable and disable all other profiles
func (f *File) DisableOnly(profiles []string) error {
for _, name := range f.data.ProfileNames {
if name == "default" {
continue
}
profile, ok := f.data.Profiles[name]
if !ok {
return UnknownProfileError
}

if contains(profiles, name) {
profile.Status = Disabled
} else {
profile.Status = Enabled
}
f.data.Profiles[name] = profile
}
return nil
}

0 comments on commit a297ed2

Please sign in to comment.