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 <simon+github@thulbourn.com>
This commit is contained in:
Simon Thulbourn 2015-01-11 22:16:26 +00:00
parent c2f1fea9dc
commit e479c82f64
3 changed files with 17 additions and 1 deletions

View File

@ -256,6 +256,15 @@ func cmdLs(c *cli.Context) {
for _, host := range hostList { for _, host := range hostList {
if !quiet { 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) go getHostState(host, *store, hostListItems)
} else { } else {
fmt.Fprintf(w, "%s\n", host.Name) fmt.Fprintf(w, "%s\n", host.Name)
@ -399,6 +408,10 @@ func getHost(c *cli.Context) *Host {
if err != nil { if err != nil {
log.Fatalf("unable to get active host: %v", err) 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 return host
} }
@ -426,7 +439,7 @@ func getHostState(host Host, store Store, hostListItems chan<- hostListItem) {
isActive, err := store.IsActive(&host) isActive, err := store.IsActive(&host)
if err != nil { 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) host.Name, err)
} }

View File

@ -64,6 +64,7 @@ func (s *Store) Remove(name string, force bool) error {
if err != nil { if err != nil {
return err return err
} }
if active != nil && active.Name == name { if active != nil && active.Name == name {
if err := s.RemoveActive(); err != nil { if err := s.RemoveActive(); err != nil {
return err return err

View File

@ -189,6 +189,7 @@ func TestStoreGetSetActive(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if host != nil { if host != nil {
t.Fatalf("GetActive: Active host should not exist") t.Fatalf("GetActive: Active host should not exist")
} }
@ -227,6 +228,7 @@ func TestStoreGetSetActive(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if host != nil { if host != nil {
t.Fatalf("Active host %s is not nil", host.Name) t.Fatalf("Active host %s is not nil", host.Name)
} }