From 95c28f298cc815d498adcf35a005708da080d81b Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 3 Sep 2019 14:04:32 +0200 Subject: [PATCH] add subcommands --- cmd/{ => create}/create.go | 42 +++++++++++++++------------------ cmd/create/createCluster.go | 47 +++++++++++++++++++++++++++++++++++++ cmd/create/createNode.go | 45 +++++++++++++++++++++++++++++++++++ cmd/{ => delete}/delete.go | 46 +++++++++++++++--------------------- cmd/delete/deleteCluster.go | 47 +++++++++++++++++++++++++++++++++++++ cmd/delete/deleteNode.go | 47 +++++++++++++++++++++++++++++++++++++ cmd/{ => get}/get.go | 47 ++++++++++++++++--------------------- cmd/get/getCluster.go | 47 +++++++++++++++++++++++++++++++++++++ cmd/get/getKubeconfig.go | 47 +++++++++++++++++++++++++++++++++++++ cmd/get/getNode.go | 47 +++++++++++++++++++++++++++++++++++++ cmd/root.go | 9 +++++++ internal/types.go | 20 ++++++++++++++++ 12 files changed, 414 insertions(+), 77 deletions(-) rename cmd/{ => create}/create.go (62%) create mode 100644 cmd/create/createCluster.go create mode 100644 cmd/create/createNode.go rename cmd/{ => delete}/delete.go (54%) create mode 100644 cmd/delete/deleteCluster.go create mode 100644 cmd/delete/deleteNode.go rename cmd/{ => get}/get.go (55%) create mode 100644 cmd/get/getCluster.go create mode 100644 cmd/get/getKubeconfig.go create mode 100644 cmd/get/getNode.go create mode 100644 internal/types.go diff --git a/cmd/create.go b/cmd/create/create.go similarity index 62% rename from cmd/create.go rename to cmd/create/create.go index 7c43e4a5..ea88fb6e 100644 --- a/cmd/create.go +++ b/cmd/create/create.go @@ -19,7 +19,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package cmd +package create import ( log "github.com/sirupsen/logrus" @@ -27,26 +27,22 @@ import ( "github.com/spf13/cobra" ) -// createCmd represents the create command -var createCmd = &cobra.Command{ - Use: "create", - Short: "Create a resource.", - Long: `Create a resource.`, - Run: func(cmd *cobra.Command, args []string) { - log.Debugln("create called") - }, -} - -func init() { - rootCmd.AddCommand(createCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // createCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +// NewCmdCreate returns a new cobra command +func NewCmdCreate() *cobra.Command { + + // create new cobra command + cmd := &cobra.Command{ + Use: "create", + Short: "Create a resource.", + Long: `Create a resource.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("create called") + }, + } + + // add subcommands + cmd.AddCommand(NewCmdCreateCluster()) + + // done + return cmd } diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go new file mode 100644 index 00000000..df9edf97 --- /dev/null +++ b/cmd/create/createCluster.go @@ -0,0 +1,47 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package create + +import ( + "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" +) + +// NewCmdCreateCluster returns a new cobra command +func NewCmdCreateCluster() *cobra.Command { + + // create new command + cmd := &cobra.Command{ + Use: "cluster", + Short: "Create a new k3s cluster in docker", + Long: `Create a new k3s cluster with containerized nodes (k3s in docker).`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("create cluster called") + }, + } + + // add subcommands + + // done + return cmd +} diff --git a/cmd/create/createNode.go b/cmd/create/createNode.go new file mode 100644 index 00000000..3925185e --- /dev/null +++ b/cmd/create/createNode.go @@ -0,0 +1,45 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package create + +import ( + "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" +) + +// NewCmdCreateNode returns a new cobra command +func NewCmdCreateNode() *cobra.Command { + + // create new command + cmd := &cobra.Command{ + Use: "node", + Short: "Create a new k3s node in docker", + Long: `Create a new containerized k3s node (k3s in docker).`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("create node called") + }, + } + + // done + return cmd +} diff --git a/cmd/delete.go b/cmd/delete/delete.go similarity index 54% rename from cmd/delete.go rename to cmd/delete/delete.go index 1b72bdb8..0e2c40a0 100644 --- a/cmd/delete.go +++ b/cmd/delete/delete.go @@ -19,7 +19,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package cmd +package delete import ( log "github.com/sirupsen/logrus" @@ -27,31 +27,23 @@ import ( "github.com/spf13/cobra" ) -// deleteCmd represents the delete command -var deleteCmd = &cobra.Command{ - Use: "delete", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: +// NewCmdDelete returns a new cobra command +func NewCmdDelete() *cobra.Command { -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - log.Debugln("delete called") - }, -} - -func init() { - rootCmd.AddCommand(deleteCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // deleteCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + // create new cobra command + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a resource.", + Long: `Delete a resource.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("delete called") + }, + } + + // add subcommands + cmd.AddCommand(NewCmdDeleteCluster()) + cmd.AddCommand(NewCmdDeleteNode()) + + // done + return cmd } diff --git a/cmd/delete/deleteCluster.go b/cmd/delete/deleteCluster.go new file mode 100644 index 00000000..0cc13055 --- /dev/null +++ b/cmd/delete/deleteCluster.go @@ -0,0 +1,47 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package delete + +import ( + log "github.com/sirupsen/logrus" + + "github.com/spf13/cobra" +) + +// NewCmdDeleteCluster returns a new cobra command +func NewCmdDeleteCluster() *cobra.Command { + + // create new cobra command + cmd := &cobra.Command{ + Use: "cluster", + Short: "Delete a cluster.", + Long: `Delete a cluster.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("delete cluster called") + }, + } + + // add subcommands + + // done + return cmd +} diff --git a/cmd/delete/deleteNode.go b/cmd/delete/deleteNode.go new file mode 100644 index 00000000..035907fe --- /dev/null +++ b/cmd/delete/deleteNode.go @@ -0,0 +1,47 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package delete + +import ( + log "github.com/sirupsen/logrus" + + "github.com/spf13/cobra" +) + +// NewCmdDeleteNode returns a new cobra command +func NewCmdDeleteNode() *cobra.Command { + + // create new cobra command + cmd := &cobra.Command{ + Use: "node", + Short: "Delete a node.", + Long: `Delete a node.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("delete node called") + }, + } + + // add subcommands + + // done + return cmd +} diff --git a/cmd/get.go b/cmd/get/get.go similarity index 55% rename from cmd/get.go rename to cmd/get/get.go index e115f565..d4336939 100644 --- a/cmd/get.go +++ b/cmd/get/get.go @@ -19,7 +19,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package cmd +package delete import ( log "github.com/sirupsen/logrus" @@ -27,31 +27,24 @@ import ( "github.com/spf13/cobra" ) -// getCmd represents the get command -var getCmd = &cobra.Command{ - Use: "get", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: +// NewCmdGet returns a new cobra command +func NewCmdGet() *cobra.Command { -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - log.Debugln("get called") - }, -} - -func init() { - rootCmd.AddCommand(getCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // getCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + // create new cobra command + cmd := &cobra.Command{ + Use: "get", + Short: "Get a resource.", + Long: `Get a resource.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("get called") + }, + } + + // add subcommands + cmd.AddCommand(NewCmdGetCluster()) + cmd.AddCommand(NewCmdGetNode()) + cmd.AddCommand(NewCmdGetKubeconfig()) + + // done + return cmd } diff --git a/cmd/get/getCluster.go b/cmd/get/getCluster.go new file mode 100644 index 00000000..4fafe2f8 --- /dev/null +++ b/cmd/get/getCluster.go @@ -0,0 +1,47 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package get + +import ( + "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" +) + +// NewCmdGetCluster returns a new cobra command +func NewCmdGetCluster() *cobra.Command { + + // create new command + cmd := &cobra.Command{ + Use: "cluster", + Short: "Get cluster", + Long: `Get cluster.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("get cluster called") + }, + } + + // add subcommands + + // done + return cmd +} diff --git a/cmd/get/getKubeconfig.go b/cmd/get/getKubeconfig.go new file mode 100644 index 00000000..d1529f9d --- /dev/null +++ b/cmd/get/getKubeconfig.go @@ -0,0 +1,47 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package get + +import ( + "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" +) + +// NewCmdGetKubeconfig returns a new cobra command +func NewCmdGetKubeconfig() *cobra.Command { + + // create new command + cmd := &cobra.Command{ + Use: "kubeconfig", + Short: "Get kubeconfig", + Long: `Get kubeconfig.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("get kubeconfig called") + }, + } + + // add subcommands + + // done + return cmd +} diff --git a/cmd/get/getNode.go b/cmd/get/getNode.go new file mode 100644 index 00000000..4b11369c --- /dev/null +++ b/cmd/get/getNode.go @@ -0,0 +1,47 @@ +/* +Copyright © 2019 Thorsten Klein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package get + +import ( + "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" +) + +// NewCmdGetNode returns a new cobra command +func NewCmdGetNode() *cobra.Command { + + // create new command + cmd := &cobra.Command{ + Use: "node", + Short: "Get node", + Long: `Get node.`, + Run: func(cmd *cobra.Command, args []string) { + log.Debugln("get node called") + }, + } + + // add subcommands + + // done + return cmd +} diff --git a/cmd/root.go b/cmd/root.go index 3dc65716..95f9e09c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,10 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/rancher/k3d/cmd/get" + + "github.com/rancher/k3d/cmd/create" + "github.com/rancher/k3d/cmd/delete" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" @@ -68,6 +72,11 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. // rootCmd.Flags().BoolP("nope", "n", false, "nope description") + + // add subcommands + rootCmd.AddCommand(create.NewCmdCreate()) + rootCmd.AddCommand(delete.NewCmdDelete()) + rootCmd.AddCommand(get.NewCmdGet()) } // initConfig reads in config file and ENV variables if set. diff --git a/internal/types.go b/internal/types.go new file mode 100644 index 00000000..d18bcb79 --- /dev/null +++ b/internal/types.go @@ -0,0 +1,20 @@ +package types + +// Cluster describes a k3d cluster +type Cluster struct { + Name string + Network string + Nodes []Node +} + +// Node describes a k3d node +type Node struct { + Name string + Role string + Image string + Volumes []string + Env []string + Args []string + Ports []string + Restart bool +}