From e479c82f64bb917d6783e9a38d81b9eec96c03e7 Mon Sep 17 00:00:00 2001 From: Simon Thulbourn Date: Sun, 11 Jan 2015 22:16:26 +0000 Subject: [PATCH] Fix for panics when no active host is set By adding some better error checking on `store.GetActive`, we can stop getting panics on some commands Signed-off-by: Simon Thulbourn --- commands.go | 15 ++++++++++++++- store.go | 1 + store_test.go | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 33a7efd78d..2db411e86b 100644 --- a/commands.go +++ b/commands.go @@ -256,6 +256,15 @@ func cmdLs(c *cli.Context) { for _, host := range hostList { if !quiet { + tmpHost, err := store.GetActive() + if err != nil { + log.Errorf("There's a problem with the active host: %s", err) + } + + if tmpHost == nil { + log.Errorf("There's a problem finding the active host") + } + go getHostState(host, *store, hostListItems) } else { fmt.Fprintf(w, "%s\n", host.Name) @@ -399,6 +408,10 @@ func getHost(c *cli.Context) *Host { if err != nil { log.Fatalf("unable to get active host: %v", err) } + + if host == nil { + log.Fatal("unable to get active host, active file not found") + } return host } @@ -426,7 +439,7 @@ func getHostState(host Host, store Store, hostListItems chan<- hostListItem) { isActive, err := store.IsActive(&host) if err != nil { - log.Errorf("error determining whether host %q is active: %s", + log.Debugf("error determining whether host %q is active: %s", host.Name, err) } diff --git a/store.go b/store.go index 1fbcd20407..9905e2fbd0 100644 --- a/store.go +++ b/store.go @@ -64,6 +64,7 @@ func (s *Store) Remove(name string, force bool) error { if err != nil { return err } + if active != nil && active.Name == name { if err := s.RemoveActive(); err != nil { return err diff --git a/store_test.go b/store_test.go index 3d540f2c5a..b78f35557d 100644 --- a/store_test.go +++ b/store_test.go @@ -189,6 +189,7 @@ func TestStoreGetSetActive(t *testing.T) { if err != nil { t.Fatal(err) } + if host != nil { t.Fatalf("GetActive: Active host should not exist") } @@ -227,6 +228,7 @@ func TestStoreGetSetActive(t *testing.T) { if err != nil { t.Fatal(err) } + if host != nil { t.Fatalf("Active host %s is not nil", host.Name) }