mirror of https://github.com/docker/docs.git
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:
parent
da95ea3968
commit
91172449c0
80
commands.go
80
commands.go
|
@ -54,7 +54,7 @@ var Commands = []cli.Command{
|
||||||
if name == "" {
|
if name == "" {
|
||||||
host, err := store.GetActive()
|
host, err := store.GetActive()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error finding active host")
|
log.Fatalf("error getting active host: %v", err)
|
||||||
}
|
}
|
||||||
if host != nil {
|
if host != nil {
|
||||||
fmt.Println(host.Name)
|
fmt.Println(host.Name)
|
||||||
|
@ -62,14 +62,11 @@ var Commands = []cli.Command{
|
||||||
} else if name != "" {
|
} else if name != "" {
|
||||||
host, err := store.Load(name)
|
host, err := store.Load(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln(err)
|
log.Fatalf("error loading host: %v", err)
|
||||||
log.Errorf("error loading new active host")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := store.SetActive(host); err != nil {
|
if err := store.SetActive(host); err != nil {
|
||||||
log.Errorf("error setting new active host")
|
log.Fatalf("error setting active host: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cli.ShowCommandHelp(c, "active")
|
cli.ShowCommandHelp(c, "active")
|
||||||
|
@ -101,25 +98,21 @@ var Commands = []cli.Command{
|
||||||
|
|
||||||
keyExists, err := drivers.PublicKeyExists()
|
keyExists, err := drivers.PublicKeyExists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error")
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !keyExists {
|
if !keyExists {
|
||||||
log.Errorf("error key doesn't exist")
|
log.Fatalf("Identity authentication public key doesn't exist at %q. Create your public key by running the \"docker\" command.", drivers.PublicKeyPath())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
store := NewStore()
|
store := NewStore()
|
||||||
|
|
||||||
host, err := store.Create(name, driver, c)
|
host, err := store.Create(name, driver, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("%s", err)
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
if err := store.SetActive(host); err != nil {
|
if err := store.SetActive(host); err != nil {
|
||||||
log.Errorf("%s", err)
|
log.Fatalf("error setting active host: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
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",
|
Name: "inspect",
|
||||||
Usage: "Inspect information about a machine",
|
Usage: "Inspect information about a machine",
|
||||||
Action: func(c *cli.Context) {
|
Action: func(c *cli.Context) {
|
||||||
prettyJson, err := json.MarshalIndent(getHost(c), "", " ")
|
prettyJSON, err := json.MarshalIndent(getHost(c), "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("error with json")
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(prettyJson))
|
fmt.Println(string(prettyJSON))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -144,8 +136,7 @@ var Commands = []cli.Command{
|
||||||
Action: func(c *cli.Context) {
|
Action: func(c *cli.Context) {
|
||||||
ip, err := getHost(c).Driver.GetIP()
|
ip, err := getHost(c).Driver.GetIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unable to get IP")
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(ip)
|
fmt.Println(ip)
|
||||||
|
@ -155,7 +146,9 @@ var Commands = []cli.Command{
|
||||||
Name: "kill",
|
Name: "kill",
|
||||||
Usage: "Kill a machine",
|
Usage: "Kill a machine",
|
||||||
Action: func(c *cli.Context) {
|
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()
|
hostList, err := store.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unable to list hosts")
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
|
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
|
||||||
|
@ -232,7 +224,9 @@ var Commands = []cli.Command{
|
||||||
Name: "restart",
|
Name: "restart",
|
||||||
Usage: "Restart a machine",
|
Usage: "Restart a machine",
|
||||||
Action: func(c *cli.Context) {
|
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 {
|
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 == "" {
|
if name == "" {
|
||||||
host, err := store.GetActive()
|
host, err := store.GetActive()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unable to get active host")
|
log.Fatalf("unable to get active host: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
name = host.Name
|
name = host.Name
|
||||||
|
@ -297,8 +290,7 @@ var Commands = []cli.Command{
|
||||||
|
|
||||||
host, err := store.Load(name)
|
host, err := store.Load(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("%s", err)
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var sshCmd *exec.Cmd
|
var sshCmd *exec.Cmd
|
||||||
|
@ -308,16 +300,14 @@ var Commands = []cli.Command{
|
||||||
sshCmd, err = host.Driver.GetSSHCommand(c.String("command"))
|
sshCmd, err = host.Driver.GetSSHCommand(c.String("command"))
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("%s", err)
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sshCmd.Stdin = os.Stdin
|
sshCmd.Stdin = os.Stdin
|
||||||
sshCmd.Stdout = os.Stdout
|
sshCmd.Stdout = os.Stdout
|
||||||
sshCmd.Stderr = os.Stderr
|
sshCmd.Stderr = os.Stderr
|
||||||
if err := sshCmd.Run(); err != nil {
|
if err := sshCmd.Run(); err != nil {
|
||||||
log.Errorf("%s", err)
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -325,21 +315,27 @@ var Commands = []cli.Command{
|
||||||
Name: "start",
|
Name: "start",
|
||||||
Usage: "Start a machine",
|
Usage: "Start a machine",
|
||||||
Action: func(c *cli.Context) {
|
Action: func(c *cli.Context) {
|
||||||
getHost(c).Start()
|
if err := getHost(c).Start(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "stop",
|
Name: "stop",
|
||||||
Usage: "Stop a machine",
|
Usage: "Stop a machine",
|
||||||
Action: func(c *cli.Context) {
|
Action: func(c *cli.Context) {
|
||||||
getHost(c).Stop()
|
if err := getHost(c).Stop(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "upgrade",
|
Name: "upgrade",
|
||||||
Usage: "Upgrade a machine to the latest version of Docker",
|
Usage: "Upgrade a machine to the latest version of Docker",
|
||||||
Action: func(c *cli.Context) {
|
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) {
|
Action: func(c *cli.Context) {
|
||||||
url, err := getHost(c).GetURL()
|
url, err := getHost(c).GetURL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unable to get url for host")
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(url)
|
fmt.Println(url)
|
||||||
|
@ -364,17 +359,14 @@ func getHost(c *cli.Context) *Host {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
host, err := store.GetActive()
|
host, err := store.GetActive()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unable to get active host")
|
log.Fatalf("unable to get active host: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
name = host.Name
|
return host
|
||||||
}
|
}
|
||||||
|
|
||||||
host, err := store.Load(name)
|
host, err := store.Load(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unable to load host")
|
log.Fatalf("unable to load host: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return host
|
return host
|
||||||
}
|
}
|
||||||
|
|
4
host.go
4
host.go
|
@ -78,6 +78,10 @@ func (h *Host) Stop() error {
|
||||||
return h.Driver.Stop()
|
return h.Driver.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Host) Upgrade() error {
|
||||||
|
return h.Driver.Upgrade()
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Host) Remove(force bool) error {
|
func (h *Host) Remove(force bool) error {
|
||||||
if err := h.Driver.Remove(); err != nil {
|
if err := h.Driver.Remove(); err != nil {
|
||||||
if force {
|
if force {
|
||||||
|
|
Loading…
Reference in New Issue