hostess/commands.go

183 lines
4.4 KiB
Go
Raw Normal View History

2015-02-14 08:21:20 +00:00
package hostess
import (
"fmt"
"github.com/codegangsta/cli"
"os"
"strings"
2015-02-14 08:21:20 +00:00
)
2015-02-14 13:01:43 +00:00
// MaybeErrorln will print an error message unless -s is passed
func MaybeErrorln(c *cli.Context, message string) {
2015-02-14 13:01:43 +00:00
if !c.Bool("s") {
os.Stderr.WriteString(fmt.Sprintf("%s: %s\n", c.Command.Name, message))
2015-02-14 08:21:20 +00:00
}
}
2015-02-14 13:01:43 +00:00
// MaybeError will print an error message unless -s is passed and then exit
func MaybeError(c *cli.Context, message string) {
MaybeErrorln(c, message)
os.Exit(1)
}
2015-02-14 13:01:43 +00:00
// MaybePrintln will print a message unless -q or -s is passed
func MaybePrintln(c *cli.Context, message string) {
2015-02-14 13:01:43 +00:00
if !c.Bool("q") && !c.Bool("s") {
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 {
2015-03-21 10:01:41 +00:00
hostsfile, errs := LoadHostfile()
if len(errs) > 0 && !c.Bool("f") {
for _, err := range errs {
MaybeErrorln(c, err.Error())
}
MaybeError(c, "Errors while parsing hostsfile. Try using fix -f")
}
return hostsfile
}
// 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 {
return s + strings.Repeat(" ", l-len(s))
}
2015-03-02 08:22:05 +00:00
// Add command parses <hostname> <ip> and adds or updates a hostname in the
// hosts file
func Add(c *cli.Context) {
if len(c.Args()) != 2 {
MaybeError(c, "expected <hostname> <ip>")
}
hostsfile := MaybeLoadHostFile(c)
hostname := NewHostname(c.Args()[0], c.Args()[1], true)
var err error
2015-03-21 09:40:19 +00:00
if !hostsfile.Hosts.Contains(hostname) {
err = hostsfile.Hosts.Add(hostname)
}
if err == nil {
if c.Bool("n") {
fmt.Println(hostsfile.Format())
} else {
MaybePrintln(c, fmt.Sprintf("Added %s", hostname.FormatHuman()))
hostsfile.Save()
}
} else {
MaybeError(c, err.Error())
}
}
2015-03-02 08:22:05 +00:00
// Del command removes any hostname(s) matching <domain> from the hosts file
func Del(c *cli.Context) {
if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c)
2015-03-21 09:40:19 +00:00
found := hostsfile.Hosts.ContainsDomain(domain)
if found {
2015-03-21 09:40:19 +00:00
hostsfile.Hosts.RemoveDomain(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()))
}
2015-02-14 08:21:20 +00:00
}
2015-03-02 08:22:05 +00:00
// Has command indicates whether a hostname is present in the hosts file
func Has(c *cli.Context) {
if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c)
2015-03-21 09:40:19 +00:00
found := hostsfile.Hosts.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()))
}
2015-02-14 08:21:20 +00:00
}
2015-03-02 08:22:05 +00:00
// 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>")
}
2015-02-14 08:21:20 +00:00
}
2015-03-02 08:22:05 +00:00
// On command enabled (uncomments) the specified hostname in the hosts file
func On(c *cli.Context) {
if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>")
}
2015-02-14 08:21:20 +00:00
}
2015-03-02 08:22:05 +00:00
// Ls command shows a list of hostnames in the hosts file
func Ls(c *cli.Context) {
hostsfile := MaybeLoadHostFile(c)
maxdomain := 0
maxip := 0
for _, hostname := range hostsfile.Hosts {
dlen := len(hostname.Domain)
if dlen > maxdomain {
maxdomain = dlen
}
ilen := len(hostname.IP)
if ilen > maxip {
maxip = ilen
}
}
// for _, domain := range hostsfile.ListDomains() {
// hostname := hostsfile.Hosts[domain]
// fmt.Printf("%s -> %s %s\n",
// StrPadRight(hostname.Domain, maxdomain),
// StrPadRight(hostname.Ip.String(), maxip),
// ShowEnabled(hostname.Enabled))
// }
2015-02-14 08:21:20 +00:00
}
const fixHelp = `Programmatically rewrite your hostsfile.
Domains pointing to the same IP will be consolidated, sorted, and extra
whitespace and comments will be removed.
hostess fix Rewrite the hostsfile
hostess fix -n Show the new hostsfile. Don't write it
`
2015-03-02 08:22:05 +00:00
// Fix command removes duplicates and conflicts from the hosts file
func Fix(c *cli.Context) {
hostsfile := MaybeLoadHostFile(c)
if c.Bool("n") {
fmt.Println(hostsfile.Format())
} else {
hostsfile.Save()
}
}
2015-03-02 08:22:05 +00:00
// Dump command outputs hosts file contents as JSON
func Dump(c *cli.Context) {
2015-02-14 08:21:20 +00:00
}
2015-03-02 08:22:05 +00:00
// Apply command adds hostnames to the hosts file from JSON
func Apply(c *cli.Context) {
2015-02-14 08:21:20 +00:00
}