Skip to content

Commit

Permalink
add toggle profile command. Closes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 8, 2020
1 parent 7ef2837 commit 651eae9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
50 changes: 50 additions & 0 deletions cmd/toggle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
)

// toggleCmd represents the enable command
var toggleCmd = &cobra.Command{
Use: "toggle",
Short: "Change status of a profile on your hosts file.",
Long: `
Alternates between on/off status of an existing profile.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

src, _ := cmd.Flags().GetString("host-file")
quiet, _ := cmd.Flags().GetBool("quiet")

err := host.Toggle(src, profile)
if err != nil {
return err
}

if quiet {
return nil
}
return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
},
}

func init() {
rootCmd.AddCommand(toggleCmd)
}
3 changes: 3 additions & 0 deletions pkg/host/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
// MissingProfileError when the profile is mandatory
var MissingProfileError = errors.New("missing profile name")

// UnknownProfileError when the profile is not present
var UnknownProfileError = errors.New("unknown profile name")

// DefaultProfileError when trying to edit default content
var DefaultProfileError = errors.New("'default' profile should not be handled by hostctl")

Expand Down
10 changes: 8 additions & 2 deletions pkg/host/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ var DefaultColumns = []string{"profile", "status", "ip", "domain"}
// ProfilesOnlyColumns are the columns used for profile status list
var ProfilesOnlyColumns = []string{"profile", "status"}

// ENABLED marks a profile active on your hosts file.
var ENABLED = "on"

// DISABLED marks a profile not active on your hosts file.
var DISABLED = "off"

// ListOptions contains available options for listing.
type ListOptions struct {
Profile string
Expand Down Expand Up @@ -101,14 +107,14 @@ func appendProfile(profile string, table *tablewriter.Table, data hostLines, opt
}
rs := strings.Split(cleanLine(r), " ")

status := "on"
status := ENABLED
ip, domain := rs[0], rs[1]
if IsDisabled(r) {
// skip empty comments lines
if rs[1] == "" {
continue
}
status = "off"
status = DISABLED
ip, domain = rs[1], rs[2]
}
if opts.StatusFilter != "" && status != opts.StatusFilter {
Expand Down
42 changes: 42 additions & 0 deletions pkg/host/toggle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package host

// Toggle alternates between enable and disable status of a profile.

func Toggle(dst, profile string) error {
h, err := getHostData(dst, profile)
if err != nil {
return err
}

status := getProfileStatus(h, profile)

switch status {
case ENABLED:
disableProfile(h, profile)
case DISABLED:
enableProfile(h, profile)
default:
return UnknownProfileError
}

return writeHostData(dst, h)
}

func getProfileStatus(h *hostFile, profile string) string {
pData, ok := h.profiles[profile]
if !ok {
return ""
}

for _, l := range pData {
if !IsHostLine(l) {
continue
}
if IsDisabled(pData[0]) {
return DISABLED
}
return ENABLED
}

return ""
}

0 comments on commit 651eae9

Please sign in to comment.