general purpose getClusters

This commit is contained in:
iwilltry42 2019-04-03 21:48:07 +02:00
parent 410f3a03ba
commit a1cdcc26c7
2 changed files with 23 additions and 8 deletions

View File

@ -46,22 +46,37 @@ func getClusterDir(name string) (string, error) {
return path.Join(homeDir, ".config", "k3d", name), nil 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) // printClusters prints the names of existing clusters
func listClusterDirs() { 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() homeDir, err := homedir.Dir()
if err != nil { 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") configDir := path.Join(homeDir, ".config", "k3d")
files, err := ioutil.ReadDir(configDir) files, err := ioutil.ReadDir(configDir)
if err != nil { 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") clusters := []string{}
// TODO: user docker client to get state of cluster
for _, file := range files { for _, file := range files {
if file.IsDir() { if file.IsDir() {
fmt.Println(file.Name()) clusters = append(clusters, file.Name())
} }
} }
return clusters, nil
} }

View File

@ -98,7 +98,7 @@ func startCluster(c *cli.Context) error {
// listClusters prints a list of created clusters // listClusters prints a list of created clusters
func listClusters(c *cli.Context) error { func listClusters(c *cli.Context) error {
listClusterDirs() printClusters()
return nil return nil
} }