-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add toggle profile command. Closes #35
- Loading branch information
Showing
4 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |