Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Commit

Permalink
Handle errors in commands
Browse files Browse the repository at this point in the history
Some errors were swallowed entirely, some didn't print the error
message.

Signed-off-by: Ben Firshman <ben@firshman.co.uk>
  • Loading branch information
bfirsh committed Dec 15, 2014
1 parent da95ea3 commit 9117244
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
80 changes: 36 additions & 44 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,19 @@ 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)
}
} 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")
Expand Down Expand Up @@ -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)
Expand All @@ -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))
},
},
{
Expand All @@ -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)
Expand All @@ -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)
}
},
},
{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
},
},
{
Expand Down Expand Up @@ -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.")
}
},
},
Expand All @@ -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
Expand All @@ -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
Expand All @@ -308,38 +300,42 @@ 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)
}
},
},
{
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)
}
},
},
{
Expand All @@ -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)
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9117244

Please sign in to comment.