From 3d79811839c04af3519641401da9bdecf984e27c Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 2 Apr 2019 14:36:13 +0200 Subject: [PATCH] initial version --- go.mod | 5 ++ go.sum | 2 + main.go | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..883651e9 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/iwilltry42/k3d-go + +go 1.12 + +require github.com/urfave/cli v1.20.0 diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..03e08fc4 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +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 new file mode 100644 index 00000000..d8709b10 --- /dev/null +++ b/main.go @@ -0,0 +1,175 @@ +package main + +import ( + "fmt" + "log" + "os" + "os/exec" + + "github.com/urfave/cli" +) + +func checkTools(c *cli.Context) error { + fmt.Println("TEST check") + return nil +} + +func createCluster(c *cli.Context) error { + fmt.Println("TEST create") + return nil +} + +func deleteCluster(c *cli.Context) error { + fmt.Println("TEST delete") + return nil +} + +func stopCluster(c *cli.Context) error { + fmt.Println("TEST stop") + return nil +} + +func startCluster(c *cli.Context) error { + fmt.Println("TEST start") + return nil +} + +func listClusters(c *cli.Context) error { + fmt.Println("TEST list") + return nil +} + +func getConfig(c *cli.Context) error { + fmt.Println("TEST get") + return nil +} + +func main() { + + var clusterName string + var serverPort int + var volume string + + app := cli.NewApp() + app.Name = "k3d" + app.Usage = "Run k3s in Docker!" + + app.Commands = []cli.Command{ + { + Name: "check-tools", + Aliases: []string{"ct"}, + Usage: "Check if docker is running", + Action: func(c *cli.Context) error { + log.Print("Checking docker...") + cmd := "docker" + args := []string{"version"} + if err := exec.Command(cmd, args...).Run(); err != nil { + log.Fatalf("Checking docker: FAILED") + return err + } + log.Println("Checking docker: SUCCESS") + return nil + }, + }, + { + Name: "create", + Aliases: []string{"c"}, + 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, + }, + cli.StringFlag{ + Name: "volume, v", + Usage: "Mount a volume into the cluster node (Docker notation: `source:destination`", + Destination: &volume, + }, + cli.IntFlag{ + Name: "port, p", + Value: 6443, + Usage: "Set a port on which the ApiServer will listen", + Destination: &serverPort, + }, + }, + Action: createCluster, + }, + { + Name: "delete", + Aliases: []string{"d"}, + Usage: "Delete cluster", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", + Destination: &clusterName, + }, + }, + Action: deleteCluster, + }, + { + Name: "stop", + Usage: "Stop cluster", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", + Destination: &clusterName, + }, + }, + Action: func(c *cli.Context) error { + fmt.Println("Stopping cluster") + return nil + }, + }, + { + Name: "start", + Usage: "Start a stopped cluster", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", + Destination: &clusterName, + }, + }, + Action: func(c *cli.Context) error { + fmt.Println("Starting stopped cluster") + return nil + }, + }, + { + Name: "list", + Usage: "List all clusters", + Action: func(c *cli.Context) error { + fmt.Println("Listing clusters") + return nil + }, + }, + { + Name: "get-config", + Usage: "Get kubeconfig location for cluster", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name, n", + Value: "k3s_default", + Usage: "name of the cluster", + Destination: &clusterName, + }, + }, + Action: func(c *cli.Context) error { + fmt.Println("Starting stopped cluster") + return nil + }, + }, + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +}