diff --git a/config.go b/config.go index b5562188..55a1b11f 100644 --- a/config.go +++ b/config.go @@ -46,22 +46,37 @@ func getClusterDir(name string) (string, error) { return path.Join(homeDir, ".config", "k3d", name), nil } -// listClusterDirs prints the names of the directories in the config folder (which should be the existing clusters) -func listClusterDirs() { +// printClusters prints the names of existing clusters +func printClusters() { + clusters, err := getClusters() + if err != nil { + log.Fatalf("ERROR: Couldn't list clusters -> %+v", err) + } + fmt.Println("NAME") + // TODO: user docker client to get state of cluster + for _, cluster := range clusters { + fmt.Println(cluster) + } +} + +// getClusters returns a list of cluster names which are folder names in the config directory +func getClusters() ([]string, error) { homeDir, err := homedir.Dir() if err != nil { - log.Fatalf("ERROR: Couldn't get user's home directory") + log.Printf("ERROR: Couldn't get user's home directory") + return nil, err } configDir := path.Join(homeDir, ".config", "k3d") files, err := ioutil.ReadDir(configDir) if err != nil { - log.Fatalf("ERROR: Couldn't list files in [%s]", configDir) + log.Printf("ERROR: Couldn't list files in [%s]", configDir) + return nil, err } - fmt.Println("NAME") - // TODO: user docker client to get state of cluster + clusters := []string{} for _, file := range files { if file.IsDir() { - fmt.Println(file.Name()) + clusters = append(clusters, file.Name()) } } + return clusters, nil } diff --git a/main.go b/main.go index f71228c6..7aeabcb8 100644 --- a/main.go +++ b/main.go @@ -98,7 +98,7 @@ func startCluster(c *cli.Context) error { // listClusters prints a list of created clusters func listClusters(c *cli.Context) error { - listClusterDirs() + printClusters() return nil }