Handle errors in commands

Some errors were swallowed entirely, some didn't print the error
message.

Signed-off-by: Ben Firshman <ben@firshman.co.uk>
This commit is contained in:
Ben Firshman 2014-12-12 15:03:39 -08:00
parent da95ea3968
commit 91172449c0
2 changed files with 40 additions and 44 deletions

View File

@ -54,7 +54,7 @@ var Commands = []cli.Command{
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error finding active host")
log.Fatalf("error getting active host: %v", err)
}
if host != nil {
fmt.Println(host.Name)
@ -62,14 +62,11 @@ var Commands = []cli.Command{
} else if name != "" {
host, err := store.Load(name)
if err != nil {
log.Errorln(err)
log.Errorf("error loading new active host")
os.Exit(1)
log.Fatalf("error loading host: %v", err)
}
if err := store.SetActive(host); err != nil {
log.Errorf("error setting new active host")
os.Exit(1)
log.Fatalf("error setting active host: %v", err)
}
} else {
cli.ShowCommandHelp(c, "active")
@ -101,25 +98,21 @@ var Commands = []cli.Command{
keyExists, err := drivers.PublicKeyExists()
if err != nil {
log.Errorf("error")
os.Exit(1)
log.Fatal(err)
}
if !keyExists {
log.Errorf("error key doesn't exist")
os.Exit(1)
log.Fatalf("Identity authentication public key doesn't exist at %q. Create your public key by running the \"docker\" command.", drivers.PublicKeyPath())
}
store := NewStore()
host, err := store.Create(name, driver, c)
if err != nil {
log.Errorf("%s", err)
os.Exit(1)
log.Fatal(err)
}
if err := store.SetActive(host); err != nil {
log.Errorf("%s", err)
os.Exit(1)
log.Fatalf("error setting active host: %v", err)
}
log.Infof("%q has been created and is now the active machine. To point Docker at this machine, run: export DOCKER_HOST=$(machine url) DOCKER_AUTH=identity", name)
@ -129,13 +122,12 @@ var Commands = []cli.Command{
Name: "inspect",
Usage: "Inspect information about a machine",
Action: func(c *cli.Context) {
prettyJson, err := json.MarshalIndent(getHost(c), "", " ")
prettyJSON, err := json.MarshalIndent(getHost(c), "", " ")
if err != nil {
log.Error("error with json")
os.Exit(1)
log.Fatal(err)
}
fmt.Println(string(prettyJson))
fmt.Println(string(prettyJSON))
},
},
{
@ -144,8 +136,7 @@ var Commands = []cli.Command{
Action: func(c *cli.Context) {
ip, err := getHost(c).Driver.GetIP()
if err != nil {
log.Errorf("error unable to get IP")
os.Exit(1)
log.Fatal(err)
}
fmt.Println(ip)
@ -155,7 +146,9 @@ var Commands = []cli.Command{
Name: "kill",
Usage: "Kill a machine",
Action: func(c *cli.Context) {
getHost(c).Driver.Kill()
if err := getHost(c).Driver.Kill(); err != nil {
log.Fatal(err)
}
},
},
{
@ -173,8 +166,7 @@ var Commands = []cli.Command{
hostList, err := store.List()
if err != nil {
log.Errorf("error unable to list hosts")
os.Exit(1)
log.Fatal(err)
}
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
@ -232,7 +224,9 @@ var Commands = []cli.Command{
Name: "restart",
Usage: "Restart a machine",
Action: func(c *cli.Context) {
getHost(c).Driver.Restart()
if err := getHost(c).Driver.Restart(); err != nil {
log.Fatal(err)
}
},
},
{
@ -262,7 +256,7 @@ var Commands = []cli.Command{
}
}
if isError {
log.Errorf("There was an error removing a machine. To force remove it, pass the -f option. Warning: this might leave it running on the provider.")
log.Fatal("There was an error removing a machine. To force remove it, pass the -f option. Warning: this might leave it running on the provider.")
}
},
},
@ -283,8 +277,7 @@ var Commands = []cli.Command{
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
log.Fatalf("unable to get active host: %v", err)
}
name = host.Name
@ -297,8 +290,7 @@ var Commands = []cli.Command{
host, err := store.Load(name)
if err != nil {
log.Errorf("%s", err)
os.Exit(1)
log.Fatal(err)
}
var sshCmd *exec.Cmd
@ -308,16 +300,14 @@ var Commands = []cli.Command{
sshCmd, err = host.Driver.GetSSHCommand(c.String("command"))
}
if err != nil {
log.Errorf("%s", err)
os.Exit(1)
log.Fatal(err)
}
sshCmd.Stdin = os.Stdin
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
if err := sshCmd.Run(); err != nil {
log.Errorf("%s", err)
os.Exit(1)
log.Fatal(err)
}
},
},
@ -325,21 +315,27 @@ var Commands = []cli.Command{
Name: "start",
Usage: "Start a machine",
Action: func(c *cli.Context) {
getHost(c).Start()
if err := getHost(c).Start(); err != nil {
log.Fatal(err)
}
},
},
{
Name: "stop",
Usage: "Stop a machine",
Action: func(c *cli.Context) {
getHost(c).Stop()
if err := getHost(c).Stop(); err != nil {
log.Fatal(err)
}
},
},
{
Name: "upgrade",
Usage: "Upgrade a machine to the latest version of Docker",
Action: func(c *cli.Context) {
getHost(c).Driver.Upgrade()
if err := getHost(c).Upgrade(); err != nil {
log.Fatal(err)
}
},
},
{
@ -348,8 +344,7 @@ var Commands = []cli.Command{
Action: func(c *cli.Context) {
url, err := getHost(c).GetURL()
if err != nil {
log.Errorf("error unable to get url for host")
os.Exit(1)
log.Fatal(err)
}
fmt.Println(url)
@ -364,17 +359,14 @@ func getHost(c *cli.Context) *Host {
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
log.Fatalf("unable to get active host: %v", err)
}
name = host.Name
return host
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load host")
os.Exit(1)
log.Fatalf("unable to load host: %v", err)
}
return host
}

View File

@ -78,6 +78,10 @@ func (h *Host) Stop() error {
return h.Driver.Stop()
}
func (h *Host) Upgrade() error {
return h.Driver.Upgrade()
}
func (h *Host) Remove(force bool) error {
if err := h.Driver.Remove(); err != nil {
if force {