|
|
|
@ -11,10 +11,14 @@ import (
|
|
|
|
|
"github.com/cbednarski/hostess/hostess"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ErrParsingHostsFile = errors.New("Errors while parsing hostsfile. Please resolve any conflicts and try again.")
|
|
|
|
|
const (
|
|
|
|
|
IPv4 = 1 << iota
|
|
|
|
|
IPv6 = 1 << iota
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
|
Preview bool
|
|
|
|
|
IPVersion int
|
|
|
|
|
Preview bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PrintErrLn will print to stderr followed by a newline
|
|
|
|
@ -23,30 +27,18 @@ func PrintErrLn(err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadHostfile will try to load, parse, and return a Hostfile. If we
|
|
|
|
|
// encounter errors we will terminate.
|
|
|
|
|
func LoadHostfile(options *Options) (*hostess.Hostfile, error) {
|
|
|
|
|
// encounter errors we will terminate, unless -f is passed.
|
|
|
|
|
func LoadHostfile() (*hostess.Hostfile, error) {
|
|
|
|
|
hosts, errs := hostess.LoadHostfile()
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
|
// If -n is passed, we'll always exit with an error code on duplicate.
|
|
|
|
|
// See issue 39
|
|
|
|
|
if options.Preview {
|
|
|
|
|
err = ErrParsingHostsFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, currentErr := range errs {
|
|
|
|
|
PrintErrLn(currentErr)
|
|
|
|
|
// If we find a duplicate we'll notify the user and continue. For
|
|
|
|
|
// other errors we'll bail out.
|
|
|
|
|
if !strings.Contains(currentErr.Error(), "duplicate hostname entry") {
|
|
|
|
|
err = ErrParsingHostsFile
|
|
|
|
|
}
|
|
|
|
|
for _, err := range errs {
|
|
|
|
|
PrintErrLn(err)
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New("Errors while parsing hostsfile. Please fix any dupes or conflicts and try again.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hosts, err
|
|
|
|
|
return hosts, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SaveOrPreview will display or write the Hostfile
|
|
|
|
@ -78,7 +70,7 @@ func StrPadRight(input string, length int) string {
|
|
|
|
|
// Add command parses <hostname> <ip> and adds or updates a hostname in the
|
|
|
|
|
// hosts file
|
|
|
|
|
func Add(options *Options, hostname, ip string) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
|
|
|
|
|
newHostname, err := hostess.NewHostname(hostname, ip, true)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -111,7 +103,7 @@ func Add(options *Options, hostname, ip string) error {
|
|
|
|
|
|
|
|
|
|
// Remove command removes any hostname(s) matching <domain> from the hosts file
|
|
|
|
|
func Remove(options *Options, hostname string) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -132,7 +124,7 @@ func Remove(options *Options, hostname string) error {
|
|
|
|
|
|
|
|
|
|
// Has command indicates whether a hostname is present in the hosts file
|
|
|
|
|
func Has(options *Options, hostname string) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -149,7 +141,7 @@ func Has(options *Options, hostname string) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Enable(options *Options, hostname string) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -168,7 +160,7 @@ func Enable(options *Options, hostname string) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Disable(options *Options, hostname string) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -195,7 +187,7 @@ func Disable(options *Options, hostname string) error {
|
|
|
|
|
|
|
|
|
|
// List command shows a list of hostnames in the hosts file
|
|
|
|
|
func List(options *Options) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -224,9 +216,9 @@ func List(options *Options) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format command removes duplicates from the hosts file
|
|
|
|
|
// Format command removes duplicates and conflicts from the hosts file
|
|
|
|
|
func Format(options *Options) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -241,7 +233,7 @@ func Format(options *Options) error {
|
|
|
|
|
|
|
|
|
|
// Dump command outputs hosts file contents as JSON
|
|
|
|
|
func Dump(options *Options) error {
|
|
|
|
|
hostsfile, err := LoadHostfile(options)
|
|
|
|
|
hostsfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -262,7 +254,7 @@ func Apply(options *Options, filename string) error {
|
|
|
|
|
return fmt.Errorf("Unable to read JSON from %s: %s", filename, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hostfile, err := LoadHostfile(options)
|
|
|
|
|
hostfile, err := LoadHostfile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|