Make on and off commands use the same function

pull/13/head
Chris Bednarski 9 years ago
parent e8acd27dc9
commit 4780643dc9

@ -79,14 +79,14 @@ func main() {
Flags: app.Flags,
},
{
Name: "off",
Usage: "disable a hosts entry (don't delete it)",
Action: hostess.Off,
Name: "on",
Usage: "enable a hosts entry (if if exists)",
Action: hostess.On,
Flags: app.Flags,
},
{
Name: "on",
Usage: "enable a hosts entry (if if exists)",
Name: "off",
Usage: "disable a hosts entry (don't delete it)",
Action: hostess.On,
Flags: app.Flags,
},

@ -150,29 +150,29 @@ func Has(c *cli.Context) {
}
// Off command disables (comments) the specified hostname in the hosts file
func Off(c *cli.Context) {
if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
}
// On command enabled (uncomments) the specified hostname in the hosts file
// On (and off) command enables (uncomments) or disables (comments) the
// specified hostname in the hosts file
func On(c *cli.Context) {
if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c)
success := hostsfile.Hosts.Enable(domain)
// Switch on / off commands
success := false
if c.Command.Name == "on" {
success = hostsfile.Hosts.Enable(domain)
} else {
success = hostsfile.Hosts.Disable(domain)
}
if success {
MaybeSaveHostFile(c, hostsfile)
MaybePrintln(c, fmt.Sprintf("Enabled %s", domain))
} else {
MaybeError(c, fmt.Sprintf("%s not found in %s", domain, GetHostsPath()))
}
}
// Ls command shows a list of hostnames in the hosts file

Loading…
Cancel
Save