diff --git a/cli/commands.go b/cli/commands.go index 32352090..467f37d1 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -99,44 +99,103 @@ kubectl cluster-info`, os.Args[0], c.String("name")) // DeleteCluster removes the cluster container and its cluster directory func DeleteCluster(c *cli.Context) error { cmd := "docker" - args := []string{"rm", c.String("name")} - log.Printf("Deleting cluster [%s]", c.String("name")) - if err := runCommand(true, cmd, args...); err != nil { - log.Printf("WARNING: couldn't delete cluster [%s], trying a force remove now.", c.String("name")) - args = append(args, "-f") + args := []string{"rm"} + clusters := []string{} + + // operate on one or all clusters + if !c.Bool("all") { + clusters = append(clusters, c.String("name")) + } else { + clusterList, err := getClusterNames() + if err != nil { + log.Fatalf("ERROR: `--all` specified, but no clusters were found.") + } + clusters = append(clusters, clusterList...) + } + + // remove clusters one by one instead of appending all names to the docker command + // this allows for more granular error handling and logging + for _, cluster := range clusters { + log.Printf("Removing cluster [%s]", cluster) + args = append(args, cluster) if err := runCommand(true, cmd, args...); err != nil { - log.Fatalf("FAILURE: couldn't delete cluster [%s] -> %+v", c.String("name"), err) - return err + log.Printf("WARNING: couldn't delete cluster [%s], trying a force remove now.", cluster) + args = args[:len(args)-1] // pop last element from list (name of cluster) + args = append(args, "-f", cluster) + if err := runCommand(true, cmd, args...); err != nil { + log.Printf("FAILURE: couldn't delete cluster [%s] -> %+v", cluster, err) + } + args = args[:len(args)-1] // pop last element from list (-f flag) } + deleteClusterDir(cluster) + log.Printf("SUCCESS: removed cluster [%s]", cluster) + args = args[:len(args)-1] // pop last element from list (name of last cluster) } - deleteClusterDir(c.String("name")) - log.Printf("SUCCESS: deleted cluster [%s]", c.String("name")) + return nil } // StopCluster stops a running cluster container (restartable) func StopCluster(c *cli.Context) error { cmd := "docker" - args := []string{"stop", c.String("name")} - log.Printf("Stopping cluster [%s]", c.String("name")) - if err := runCommand(true, cmd, args...); err != nil { - log.Fatalf("FAILURE: couldn't stop cluster [%s] -> %+v", c.String("name"), err) - return err + args := []string{"stop"} + clusters := []string{} + + // operate on one or all clusters + if !c.Bool("all") { + clusters = append(clusters, c.String("name")) + } else { + clusterList, err := getClusterNames() + if err != nil { + log.Fatalf("ERROR: `--all` specified, but no clusters were found.") + } + clusters = append(clusters, clusterList...) } - log.Printf("SUCCESS: stopped cluster [%s]", c.String("name")) + + // stop clusters one by one instead of appending all names to the docker command + // this allows for more granular error handling and logging + for _, cluster := range clusters { + log.Printf("Starting cluster [%s]", cluster) + args = append(args, cluster) + if err := runCommand(true, cmd, args...); err != nil { + log.Printf("FAILURE: couldn't stop cluster [%s] -> %+v", cluster, err) + } + log.Printf("SUCCESS: stopped cluster [%s]", cluster) + args = args[:len(args)-1] // pop last element from list (name of last cluster) + } + return nil } // StartCluster starts a stopped cluster container func StartCluster(c *cli.Context) error { cmd := "docker" - args := []string{"start", c.String("name")} - log.Printf("Starting cluster [%s]", c.String("name")) - if err := runCommand(true, cmd, args...); err != nil { - log.Fatalf("FAILURE: couldn't start cluster [%s] -> %+v", c.String("name"), err) - return err + args := []string{"start"} + clusters := []string{} + + // operate on one or all clusters + if !c.Bool("all") { + clusters = append(clusters, c.String("name")) + } else { + clusterList, err := getClusterNames() + if err != nil { + log.Fatalf("ERROR: `--all` specified, but no clusters were found.") + } + clusters = append(clusters, clusterList...) } - log.Printf("SUCCESS: started cluster [%s]", c.String("name")) + + // start clusters one by one instead of appending all names to the docker command + // this allows for more granular error handling and logging + for _, cluster := range clusters { + log.Printf("Starting cluster [%s]", cluster) + args = append(args, cluster) + if err := runCommand(true, cmd, args...); err != nil { + log.Printf("FAILURE: couldn't start cluster [%s] -> %+v", cluster, err) + } + log.Printf("SUCCESS: started cluster [%s]", cluster) + args = args[:len(args)-1] // pop last element from list (name of last cluster) + } + return nil }