|
|
@ -3,9 +3,11 @@ package hostess
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ErrCantWriteHostFile indicates that we are unable to write to the hosts file
|
|
|
|
// ErrCantWriteHostFile indicates that we are unable to write to the hosts file
|
|
|
@ -231,10 +233,32 @@ func Fix(c *cli.Context) {
|
|
|
|
|
|
|
|
|
|
|
|
// Dump command outputs hosts file contents as JSON
|
|
|
|
// Dump command outputs hosts file contents as JSON
|
|
|
|
func Dump(c *cli.Context) {
|
|
|
|
func Dump(c *cli.Context) {
|
|
|
|
|
|
|
|
hostsfile := AlwaysLoadHostFile(c)
|
|
|
|
|
|
|
|
output, err := hostsfile.Hosts.Dump()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
MaybeError(c, err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(fmt.Sprintf("%s", output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Apply command adds hostnames to the hosts file from JSON
|
|
|
|
// Apply command adds hostnames to the hosts file from JSON
|
|
|
|
func Apply(c *cli.Context) {
|
|
|
|
func Apply(c *cli.Context) {
|
|
|
|
|
|
|
|
if len(c.Args()) != 1 {
|
|
|
|
|
|
|
|
MaybeError(c, "Usage should be apply [filename]")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := c.Args()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
MaybeError(c, fmt.Sprintf("Unable to read %s: %s", filename, err))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hostfile := AlwaysLoadHostFile(c)
|
|
|
|
|
|
|
|
err = hostfile.Hosts.Apply(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
MaybeError(c, fmt.Sprintf("Error applying changes to hosts file: %s", err))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MaybeSaveHostFile(c, hostfile)
|
|
|
|
|
|
|
|
MaybePrintln(c, fmt.Sprintf("%s applied", filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|