diff --git a/config.go b/config.go new file mode 100644 index 00000000..b5562188 --- /dev/null +++ b/config.go @@ -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/. +// 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/ +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/ +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()) + } + } +} diff --git a/go.mod b/go.mod index 883651e9..29d7160f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/iwilltry42/k3d-go go 1.12 -require github.com/urfave/cli v1.20.0 +require ( + github.com/mitchellh/go-homedir v1.1.0 + github.com/urfave/cli v1.20.0 +) diff --git a/go.sum b/go.sum index 03e08fc4..690b10b2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index fa3ee6eb..844ca74e 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( ) func createCluster(c *cli.Context) error { + createClusterDir(c.String("name")) port := fmt.Sprintf("%s:%s", c.String("port"), c.String("port")) image := fmt.Sprintf("rancher/k3s:%s", c.String("version")) cmd := "docker" @@ -38,7 +39,7 @@ func createCluster(c *cli.Context) error { log.Printf("Creating cluster [%s]", c.String("name")) log.Printf("Running command: %+v", exec.Command(cmd, args...).Args) if err := exec.Command(cmd, args...).Run(); err != nil { - log.Fatalf("FAILURE: couldn't create cluster [%s] Err: %+v", c.String("name"), err) + log.Fatalf("FAILURE: couldn't create cluster [%s] -> %+v", c.String("name"), err) return err } log.Printf("SUCCESS: created cluster [%s]", c.String("name")) @@ -55,10 +56,11 @@ func deleteCluster(c *cli.Context) error { args = append(args, "-f") log.Printf("Running command: %+v", exec.Command(cmd, args...).Args) if err := exec.Command(cmd, args...).Run(); err != nil { - log.Fatalf("FAILURE: couldn't delete cluster [%s] Err: %+v", c.String("name"), err) + log.Fatalf("FAILURE: couldn't delete cluster [%s] -> %+v", c.String("name"), err) return err } } + deleteClusterDir(c.String("name")) log.Printf("SUCCESS: deleted cluster [%s]", c.String("name")) return nil } @@ -69,7 +71,7 @@ func stopCluster(c *cli.Context) error { log.Printf("Stopping cluster [%s]", c.String("name")) log.Printf("Running command: %+v", exec.Command(cmd, args...).Args) if err := exec.Command(cmd, args...).Run(); err != nil { - log.Fatalf("FAILURE: couldn't stop cluster [%s] Err: %+v", c.String("name"), err) + log.Fatalf("FAILURE: couldn't stop cluster [%s] -> %+v", c.String("name"), err) return err } log.Printf("SUCCESS: stopped cluster [%s]", c.String("name")) @@ -82,7 +84,7 @@ func startCluster(c *cli.Context) error { log.Printf("Starting cluster [%s]", c.String("name")) log.Printf("Running command: %+v", exec.Command(cmd, args...).Args) if err := exec.Command(cmd, args...).Run(); err != nil { - log.Fatalf("FAILURE: couldn't start cluster [%s] Err: %+v", c.String("name"), err) + log.Fatalf("FAILURE: couldn't start cluster [%s] -> %+v", c.String("name"), err) return err } log.Printf("SUCCESS: started cluster [%s]", c.String("name")) @@ -90,19 +92,19 @@ func startCluster(c *cli.Context) error { } func listClusters(c *cli.Context) error { - fmt.Println("TEST list") + listClusterDirs() return nil } -func getConfig(c *cli.Context) error { +func getKubeConfig(c *cli.Context) error { sourcePath := fmt.Sprintf("%s:/output/kubeconfig.yaml", c.String("name")) - destPath := fmt.Sprintf("./kubeconfig-%s.yaml", c.String("name")) + destPath, _ := getClusterDir(c.String("name")) cmd := "docker" args := []string{"cp", sourcePath, destPath} log.Printf("Grabbing kubeconfig for cluster [%s]", c.String("name")) log.Printf("Running command: %+v", exec.Command(cmd, args...).Args) if err := exec.Command(cmd, args...).Run(); err != nil { - log.Fatalf("FAILURE: couldn't get kubeconfig for cluster [%s] Err: %+v", c.String("name"), err) + log.Fatalf("FAILURE: couldn't get kubeconfig for cluster [%s] -> %+v", c.String("name"), err) return err } log.Printf("SUCCESS: retrieved kubeconfig for cluster [%s]", c.String("name")) @@ -111,11 +113,6 @@ func getConfig(c *cli.Context) error { func main() { - var clusterName string - var serverPort int - var volume string - var k3sVersion string - app := cli.NewApp() app.Name = "k3d" app.Usage = "Run k3s in Docker!" @@ -133,6 +130,7 @@ func main() { Aliases: []string{"ct"}, Usage: "Check if docker is running", Action: func(c *cli.Context) error { + //TODO: own function with version check log.Print("Checking docker...") cmd := "docker" args := []string{"version"} @@ -150,27 +148,23 @@ func main() { Usage: "Create a single node k3s cluster in a container", Flags: []cli.Flag{ cli.StringFlag{ - Name: "name, n", - Value: "k3s_default", - Usage: "Set a name for the cluster", - Destination: &clusterName, + Name: "name, n", + Value: "k3s_default", + Usage: "Set a name for the cluster", }, cli.StringFlag{ - Name: "volume, v", - Usage: "Mount a volume into the cluster node (Docker notation: `source:destination`", - Destination: &volume, + Name: "volume, v", + Usage: "Mount a volume into the cluster node (Docker notation: `source:destination`", }, cli.StringFlag{ - Name: "version", - Value: "v0.1.0", - Usage: "Choose the k3s image version", - Destination: &k3sVersion, + Name: "version", + Value: "v0.1.0", + Usage: "Choose the k3s image version", }, cli.IntFlag{ - Name: "port, p", - Value: 6443, - Usage: "Set a port on which the ApiServer will listen", - Destination: &serverPort, + Name: "port, p", + Value: 6443, + Usage: "Set a port on which the ApiServer will listen", }, }, Action: createCluster, @@ -181,10 +175,9 @@ func main() { Usage: "Delete cluster", Flags: []cli.Flag{ cli.StringFlag{ - Name: "name, n", - Value: "k3s_default", - Usage: "name of the cluster", - Destination: &clusterName, + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", }, }, Action: deleteCluster, @@ -194,10 +187,9 @@ func main() { Usage: "Stop cluster", Flags: []cli.Flag{ cli.StringFlag{ - Name: "name, n", - Value: "k3s_default", - Usage: "name of the cluster", - Destination: &clusterName, + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", }, }, Action: func(c *cli.Context) error { @@ -210,10 +202,9 @@ func main() { Usage: "Start a stopped cluster", Flags: []cli.Flag{ cli.StringFlag{ - Name: "name, n", - Value: "k3s_default", - Usage: "name of the cluster", - Destination: &clusterName, + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", }, }, Action: func(c *cli.Context) error { @@ -222,22 +213,18 @@ func main() { }, }, { - Name: "list", - Usage: "List all clusters", - Action: func(c *cli.Context) error { - fmt.Println("Listing clusters") - return nil - }, + Name: "list", + Usage: "List all clusters", + Action: listClusters, }, { - Name: "get-config", + Name: "get-kubeconfig", Usage: "Get kubeconfig location for cluster", Flags: []cli.Flag{ cli.StringFlag{ - Name: "name, n", - Value: "k3s_default", - Usage: "name of the cluster", - Destination: &clusterName, + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", }, }, Action: func(c *cli.Context) error {