Added del and has and some helper functions

pull/13/head
Chris Bednarski 9 years ago
parent 62832cd57b
commit 0750805ceb

@ -86,9 +86,27 @@ func main() {
Flags: app.Flags, Flags: app.Flags,
}, },
{ {
Name: "fix", Name: "del",
// Usage: "add a task to the list", // Usage: "add a task to the list",
Action: hostess.Fix, Action: hostess.Del,
Flags: app.Flags,
},
{
Name: "has",
// Usage: "add a task to the list",
Action: hostess.Has,
Flags: app.Flags,
},
{
Name: "off",
// Usage: "add a task to the list",
Action: hostess.Off,
Flags: app.Flags,
},
{
Name: "on",
// Usage: "add a task to the list",
Action: hostess.On,
Flags: app.Flags, Flags: app.Flags,
}, },
{ {
@ -97,6 +115,24 @@ func main() {
Action: hostess.Ls, Action: hostess.Ls,
Flags: app.Flags, Flags: app.Flags,
}, },
{
Name: "fix",
// Usage: "add a task to the list",
Action: hostess.Fix,
Flags: app.Flags,
},
{
Name: "dump",
// Usage: "add a task to the list",
Action: hostess.Dump,
Flags: app.Flags,
},
{
Name: "apply",
// Usage: "add a task to the list",
Action: hostess.Apply,
Flags: app.Flags,
},
} }
// switch flag.Arg(0) { // switch flag.Arg(0) {

@ -7,34 +7,40 @@ import (
"strings" "strings"
) )
// MaybeErrorln will print an error message unless -q is passed
func MaybeErrorln(c *cli.Context, message string) { func MaybeErrorln(c *cli.Context, message string) {
if !c.Bool("q") { if !c.Bool("q") {
os.Stderr.WriteString(fmt.Sprintf("%s: %s\n", c.Command.Name, message)) os.Stderr.WriteString(fmt.Sprintf("%s: %s\n", c.Command.Name, message))
} }
} }
// MaybeError will print an error message unless -q is passed and then exit
func MaybeError(c *cli.Context, message string) { func MaybeError(c *cli.Context, message string) {
MaybeErrorln(c, message) MaybeErrorln(c, message)
os.Exit(1) os.Exit(1)
} }
// MaybePrintln will print a message unless -s is passed
func MaybePrintln(c *cli.Context, message string) { func MaybePrintln(c *cli.Context, message string) {
if !c.Bool("s") { if !c.Bool("s") {
fmt.Println(message) fmt.Println(message)
} }
} }
// MaybeLoadHostFile will try to load, parse, and return a Hostfile. If we
// encounter errors we will terminate, unless -f is passed.
func MaybeLoadHostFile(c *cli.Context) *Hostfile { func MaybeLoadHostFile(c *cli.Context) *Hostfile {
hostsfile, errs := LoadHostFile() hostsfile, errs := LoadHostFile()
if len(errs) > 0 && !c.Bool("f") { if len(errs) > 0 && !c.Bool("f") {
for _, err := range errs { for _, err := range errs {
MaybeErrorln(c, err.Error()) MaybeErrorln(c, err.Error())
} }
MaybeError(c, "Errors while parsing hostsfile") MaybeError(c, "Errors while parsing hostsfile. Try using fix -f")
} }
return hostsfile return hostsfile
} }
// ShowEnabled turns a boolean into a string (On) or (Off)
func ShowEnabled(on bool) string { func ShowEnabled(on bool) string {
if on { if on {
return "(On)" return "(On)"
@ -43,10 +49,13 @@ func ShowEnabled(on bool) string {
} }
} }
// ShowHostname turns a Hostname into a string for display
func ShowHostname(hostname Hostname) string { func ShowHostname(hostname Hostname) string {
return fmt.Sprintf("%s -> %s %s", hostname.Domain, hostname.Ip, ShowEnabled(hostname.Enabled)) return fmt.Sprintf("%s -> %s %s", hostname.Domain, hostname.Ip, ShowEnabled(hostname.Enabled))
} }
// StrPadRight adds spaces to the right of a string until it reaches l length.
// If the input string is already that long, do nothing.
func StrPadRight(s string, l int) string { func StrPadRight(s string, l int) string {
return s + strings.Repeat(" ", l-len(s)) return s + strings.Repeat(" ", l-len(s))
} }
@ -58,7 +67,11 @@ func Add(c *cli.Context) {
hostsfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
hostname := Hostname{c.Args()[0], c.Args()[1], true} hostname := Hostname{c.Args()[0], c.Args()[1], true}
err := hostsfile.Add(hostname) var err error
if !hostsfile.Contains(hostname) {
err = hostsfile.Add(hostname)
}
if err == nil { if err == nil {
if c.Bool("n") { if c.Bool("n") {
fmt.Println(hostsfile.Format()) fmt.Println(hostsfile.Format())
@ -71,27 +84,61 @@ func Add(c *cli.Context) {
} }
} }
func Del(c *cli.Context) error { func Del(c *cli.Context) {
return nil if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c)
found := hostsfile.ContainsDomain(domain)
if found {
hostsfile.Delete(domain)
if c.Bool("n") {
fmt.Println(hostsfile.Format())
} else {
MaybePrintln(c, fmt.Sprintf("Deleted %s", domain))
hostsfile.Save()
}
} else {
MaybePrintln(c, fmt.Sprintf("%s not found in %s", domain, GetHostsPath()))
}
} }
func Has(c *cli.Context) error { func Has(c *cli.Context) {
return nil if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c)
found := hostsfile.ContainsDomain(domain)
if found {
MaybePrintln(c, fmt.Sprintf("Found %s in %s", domain, GetHostsPath()))
} else {
MaybeError(c, fmt.Sprintf("%s not found in %s", domain, GetHostsPath()))
}
} }
func Off(c *cli.Context) error { func Off(c *cli.Context) {
return nil if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
} }
func On(c *cli.Context) error { func On(c *cli.Context) {
return nil if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
} }
func Ls(c *cli.Context) { func Ls(c *cli.Context) {
hostfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
maxdomain := 0 maxdomain := 0
maxip := 0 maxip := 0
for _, hostname := range hostfile.Hosts { for _, hostname := range hostsfile.Hosts {
dlen := len(hostname.Domain) dlen := len(hostname.Domain)
if dlen > maxdomain { if dlen > maxdomain {
maxdomain = dlen maxdomain = dlen
@ -102,8 +149,8 @@ func Ls(c *cli.Context) {
} }
} }
for _, domain := range hostfile.ListDomains() { for _, domain := range hostsfile.ListDomains() {
hostname := hostfile.Hosts[domain] hostname := hostsfile.Hosts[domain]
fmt.Printf("%s -> %s %s\n", fmt.Printf("%s -> %s %s\n",
StrPadRight(hostname.Domain, maxdomain), StrPadRight(hostname.Domain, maxdomain),
StrPadRight(hostname.Ip, maxip), StrPadRight(hostname.Ip, maxip),
@ -121,18 +168,18 @@ whitespace and comments will be removed.
` `
func Fix(c *cli.Context) { func Fix(c *cli.Context) {
hostfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
if c.Bool("n") { if c.Bool("n") {
fmt.Println(hostfile.Format()) fmt.Println(hostsfile.Format())
} else { } else {
hostfile.Save() hostsfile.Save()
} }
} }
func Dump(c *cli.Context) error { func Dump(c *cli.Context) {
return nil
} }
func Apply(c *cli.Context) error { func Apply(c *cli.Context) {
return nil
} }

@ -137,6 +137,13 @@ func (h *Hostname) Format() string {
return r return r
} }
func (a *Hostname) Equals(b Hostname) bool {
if a.Domain == b.Domain && a.Ip == b.Ip {
return true
}
return false
}
// Hostfile represents /etc/hosts (or a similar file, depending on OS), and // Hostfile represents /etc/hosts (or a similar file, depending on OS), and
// includes a list of Hostnames. Hostfile includes // includes a list of Hostnames. Hostfile includes
type Hostfile struct { type Hostfile struct {
@ -319,6 +326,24 @@ func (h *Hostfile) Save() error {
return nil return nil
} }
func (h *Hostfile) Contains(b Hostname) bool {
for _, a := range h.Hosts {
if a.Equals(b) {
return true
}
}
return false
}
func (h *Hostfile) ContainsDomain(search string) bool {
for _, hostname := range h.Hosts {
if hostname.Domain == search {
return true
}
}
return false
}
func (h *Hostfile) Add(host Hostname) error { func (h *Hostfile) Add(host Hostname) error {
host_f, found := h.Hosts[host.Domain] host_f, found := h.Hosts[host.Domain]
if found { if found {

Loading…
Cancel
Save