Skip to content

Commit

Permalink
better status types and management with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 8, 2020
1 parent 530ede5 commit 0e675d9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cmd/list.go
Expand Up @@ -36,8 +36,8 @@ The "default" profile is all the content that is not handled by hostctl tool.
func init() {
rootCmd.AddCommand(listCmd)

listCmd.AddCommand(makeListStatusCmd("enabled"))
listCmd.AddCommand(makeListStatusCmd("disabled"))
listCmd.AddCommand(makeListStatusCmd(host.Enabled))
listCmd.AddCommand(makeListStatusCmd(host.Disabled))

listCmd.PersistentFlags().StringSliceP("column", "c", nil, "Columns to show on lists")
listCmd.PersistentFlags().Bool("raw", false, "Output without table borders")
Expand Down
19 changes: 11 additions & 8 deletions cmd/list_status.go
Expand Up @@ -9,17 +9,20 @@ import (
)

// makeListStatusCmd represents the list enabled command
var makeListStatusCmd = func(cmd string) *cobra.Command {
status := ""
switch cmd {
case "enabled":
status = "on"
case "disabled":
status = "off"
var makeListStatusCmd = func(status host.ProfileStatus) *cobra.Command {
cmd := ""
alias := ""
switch status {
case host.Enabled:
cmd = "enabled"
alias = "on"
case host.Disabled:
cmd = "disabled"
alias = "off"
}
return &cobra.Command{
Use: cmd,
Aliases: []string{status},
Aliases: []string{alias},
Short: fmt.Sprintf("Shows list of %s profiles on your hosts file.", cmd),
Long: fmt.Sprintf(`
Shows a detailed list of %s profiles on your hosts file with name, ip and host name.
Expand Down
21 changes: 12 additions & 9 deletions pkg/host/list.go
Expand Up @@ -13,19 +13,22 @@ 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"
type ProfileStatus string

// DISABLED marks a profile not active on your hosts file.
var DISABLED = "off"
const (
// Enabled marks a profile active on your hosts file.
Enabled ProfileStatus = "on"
// Disabled marks a profile not active on your hosts file.
Disabled ProfileStatus = "off"
)

// ListOptions contains available options for listing.
type ListOptions struct {
Profile string
RawTable bool
Columns []string
ProfilesOnly bool
StatusFilter string
StatusFilter ProfileStatus
}

// ListProfiles shows a table with profile names status and routing information
Expand Down Expand Up @@ -107,21 +110,21 @@ func appendProfile(profile string, table *tablewriter.Table, data hostLines, opt
}
rs := strings.Split(cleanLine(r), " ")

status := ENABLED
status := Enabled
ip, domain := rs[0], rs[1]
if IsDisabled(r) {
// skip empty comments lines
if rs[1] == "" {
continue
}
status = DISABLED
status = Disabled
ip, domain = rs[1], rs[2]
}
if opts.StatusFilter != "" && status != opts.StatusFilter {
continue
}
if opts.ProfilesOnly {
table.Append([]string{profile, status})
table.Append([]string{profile, string(status)})
return
}
var row []string
Expand All @@ -130,7 +133,7 @@ func appendProfile(profile string, table *tablewriter.Table, data hostLines, opt
case "profile":
row = append(row, profile)
case "status":
row = append(row, status)
row = append(row, string(status))
case "ip", "ips":
row = append(row, ip)
case "domain", "domains":
Expand Down
10 changes: 5 additions & 5 deletions pkg/host/toggle.go
Expand Up @@ -11,9 +11,9 @@ func Toggle(dst, profile string) error {
status := getProfileStatus(h, profile)

switch status {
case ENABLED:
case Enabled:
disableProfile(h, profile)
case DISABLED:
case Disabled:
enableProfile(h, profile)
default:
return UnknownProfileError
Expand All @@ -22,7 +22,7 @@ func Toggle(dst, profile string) error {
return writeHostData(dst, h)
}

func getProfileStatus(h *hostFile, profile string) string {
func getProfileStatus(h *hostFile, profile string) ProfileStatus {
pData, ok := h.profiles[profile]
if !ok {
return ""
Expand All @@ -33,9 +33,9 @@ func getProfileStatus(h *hostFile, profile string) string {
continue
}
if IsDisabled(pData[0]) {
return DISABLED
return Disabled
}
return ENABLED
return Enabled
}

return ""
Expand Down

0 comments on commit 0e675d9

Please sign in to comment.