mirror of https://github.com/k3d-io/k3d
parent
2f4c128545
commit
609be6c84c
@ -0,0 +1,67 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"log" |
||||
"os" |
||||
"path" |
||||
|
||||
"github.com/mitchellh/go-homedir" |
||||
) |
||||
|
||||
// createDirIfNotExists checks for the existence of a directory and creates it along with all required parents if not.
|
||||
// It returns an error if the directory (or parents) couldn't be created and nil if it worked fine or if the path already exists.
|
||||
func createDirIfNotExists(path string) error { |
||||
if _, err := os.Stat(path); os.IsNotExist(err) { |
||||
return os.MkdirAll(path, os.ModePerm) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// createClusterDir creates a directory with the cluster name under $HOME/.config/k3d/<cluster_name>.
|
||||
// The cluster directory will be used e.g. to store the kubeconfig file.
|
||||
func createClusterDir(name string) { |
||||
clusterPath, _ := getClusterDir(name) |
||||
if err := createDirIfNotExists(clusterPath); err != nil { |
||||
log.Fatalf("ERROR: couldn't create cluster directory [%s] -> %+v", clusterPath, err) |
||||
} |
||||
} |
||||
|
||||
// deleteClusterDir contrary to createClusterDir, this deletes the cluster directory under $HOME/.config/k3d/<cluster_name>
|
||||
func deleteClusterDir(name string) { |
||||
clusterPath, _ := getClusterDir(name) |
||||
if err := os.RemoveAll(clusterPath); err != nil { |
||||
log.Printf("WARNING: couldn't delete cluster directory [%s]. You might want to delete it manually.", clusterPath) |
||||
} |
||||
} |
||||
|
||||
// getClusterDir returns the path to the cluster directory which is $HOME/.config/k3d/<cluster_name>
|
||||
func getClusterDir(name string) (string, error) { |
||||
homeDir, err := homedir.Dir() |
||||
if err != nil { |
||||
log.Printf("ERROR: Couldn't get user's home directory") |
||||
return "", err |
||||
} |
||||
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() { |
||||
homeDir, err := homedir.Dir() |
||||
if err != nil { |
||||
log.Fatalf("ERROR: Couldn't get user's home directory") |
||||
} |
||||
configDir := path.Join(homeDir, ".config", "k3d") |
||||
files, err := ioutil.ReadDir(configDir) |
||||
if err != nil { |
||||
log.Fatalf("ERROR: Couldn't list files in [%s]", configDir) |
||||
} |
||||
fmt.Println("NAME") |
||||
// TODO: user docker client to get state of cluster
|
||||
for _, file := range files { |
||||
if file.IsDir() { |
||||
fmt.Println(file.Name()) |
||||
} |
||||
} |
||||
} |
@ -1,2 +1,4 @@ |
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= |
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= |
||||
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= |
||||
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= |
||||
|
Loading…
Reference in new issue