From 886f559ec247c700f9a8461f19b92b13a580ef1f Mon Sep 17 00:00:00 2001 From: Benjamin Elder Date: Fri, 1 Nov 2019 11:31:19 -0700 Subject: [PATCH] move commands into pkg/ --- Makefile | 2 +- cmd/kind/app/main.go | 21 +++++++++++ cmd/kind/main.go | 9 +++++ main.go | 4 +-- .../cmd}/kind/build/baseimage/baseimage.go | 0 {cmd => pkg/cmd}/kind/build/build.go | 4 +-- .../cmd}/kind/build/nodeimage/nodeimage.go | 0 {cmd => pkg/cmd}/kind/completion/bash/bash.go | 0 .../cmd}/kind/completion/completion.go | 4 +-- {cmd => pkg/cmd}/kind/completion/zsh/zsh.go | 0 .../cmd}/kind/create/cluster/createcluster.go | 0 {cmd => pkg/cmd}/kind/create/create.go | 2 +- .../cmd}/kind/delete/cluster/deletecluster.go | 0 {cmd => pkg/cmd}/kind/delete/delete.go | 2 +- {cmd => pkg/cmd}/kind/export/export.go | 2 +- {cmd => pkg/cmd}/kind/export/logs/logs.go | 0 .../cmd}/kind/get/clusters/clusters.go | 0 {cmd => pkg/cmd}/kind/get/get.go | 6 ++-- .../cmd}/kind/get/kubeconfig/kubeconfig.go | 0 {cmd => pkg/cmd}/kind/get/nodes/nodes.go | 0 .../kind/load/docker-image/docker-image.go | 0 .../kind/load/image-archive/image-archive.go | 0 {cmd => pkg/cmd}/kind/load/load.go | 4 +-- cmd/kind/kind.go => pkg/cmd/kind/root.go | 35 +++++++------------ {cmd => pkg/cmd}/kind/version/version.go | 0 {cmd => pkg/cmd}/kind/version/version_test.go | 0 26 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 cmd/kind/app/main.go create mode 100644 cmd/kind/main.go rename {cmd => pkg/cmd}/kind/build/baseimage/baseimage.go (100%) rename {cmd => pkg/cmd}/kind/build/build.go (91%) rename {cmd => pkg/cmd}/kind/build/nodeimage/nodeimage.go (100%) rename {cmd => pkg/cmd}/kind/completion/bash/bash.go (100%) rename {cmd => pkg/cmd}/kind/completion/completion.go (94%) rename {cmd => pkg/cmd}/kind/completion/zsh/zsh.go (100%) rename {cmd => pkg/cmd}/kind/create/cluster/createcluster.go (100%) rename {cmd => pkg/cmd}/kind/create/create.go (94%) rename {cmd => pkg/cmd}/kind/delete/cluster/deletecluster.go (100%) rename {cmd => pkg/cmd}/kind/delete/delete.go (94%) rename {cmd => pkg/cmd}/kind/export/export.go (95%) rename {cmd => pkg/cmd}/kind/export/logs/logs.go (100%) rename {cmd => pkg/cmd}/kind/get/clusters/clusters.go (100%) rename {cmd => pkg/cmd}/kind/get/get.go (89%) rename {cmd => pkg/cmd}/kind/get/kubeconfig/kubeconfig.go (100%) rename {cmd => pkg/cmd}/kind/get/nodes/nodes.go (100%) rename {cmd => pkg/cmd}/kind/load/docker-image/docker-image.go (100%) rename {cmd => pkg/cmd}/kind/load/image-archive/image-archive.go (100%) rename {cmd => pkg/cmd}/kind/load/load.go (89%) rename cmd/kind/kind.go => pkg/cmd/kind/root.go (86%) rename {cmd => pkg/cmd}/kind/version/version.go (100%) rename {cmd => pkg/cmd}/kind/version/version_test.go (100%) diff --git a/Makefile b/Makefile index d08882a438..08357fd627 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ INSTALL?=install INSTALL_DIR?=$(shell hack/build/goinstalldir.sh) # record the source commit in the binary COMMIT?=$(shell git rev-parse HEAD 2>/dev/null) -LD_FLAGS:=-X sigs.k8s.io/kind/cmd/kind/version.GitCommit=$(COMMIT) +LD_FLAGS:=-X sigs.k8s.io/kind/pkg/cmd/kind/version.GitCommit=$(COMMIT) # the output binary name, overridden when cross compiling KIND_BINARY_NAME?=kind diff --git a/cmd/kind/app/main.go b/cmd/kind/app/main.go new file mode 100644 index 0000000000..9a7deaa79e --- /dev/null +++ b/cmd/kind/app/main.go @@ -0,0 +1,21 @@ +package app + +import ( + "os" + + "sigs.k8s.io/kind/pkg/cmd/kind" +) + +// Main is the kind main(), it will invoke Run(), if an error is returned +// it will then call os.Exit +func Main() { + if err := Run(); err != nil { + os.Exit(1) + } +} + +// Run invokes the kind root command, returning the error. +// See: sigs.k8s.io/kind/pkg/cmd/kind +func Run() error { + return kind.NewCommand().Execute() +} diff --git a/cmd/kind/main.go b/cmd/kind/main.go new file mode 100644 index 0000000000..fe639c2249 --- /dev/null +++ b/cmd/kind/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "sigs.k8s.io/kind/cmd/kind/app" +) + +func main() { + app.Main() +} diff --git a/main.go b/main.go index a78c130a84..402fdc873b 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,9 @@ limitations under the License. package main import ( - "sigs.k8s.io/kind/cmd/kind" + "sigs.k8s.io/kind/cmd/kind/app" ) func main() { - kind.Main() + app.Main() } diff --git a/cmd/kind/build/baseimage/baseimage.go b/pkg/cmd/kind/build/baseimage/baseimage.go similarity index 100% rename from cmd/kind/build/baseimage/baseimage.go rename to pkg/cmd/kind/build/baseimage/baseimage.go diff --git a/cmd/kind/build/build.go b/pkg/cmd/kind/build/build.go similarity index 91% rename from cmd/kind/build/build.go rename to pkg/cmd/kind/build/build.go index a3dbd602f4..2b8670e763 100644 --- a/cmd/kind/build/build.go +++ b/pkg/cmd/kind/build/build.go @@ -20,8 +20,8 @@ package build import ( "github.com/spf13/cobra" - "sigs.k8s.io/kind/cmd/kind/build/baseimage" - "sigs.k8s.io/kind/cmd/kind/build/nodeimage" + "sigs.k8s.io/kind/pkg/cmd/kind/build/baseimage" + "sigs.k8s.io/kind/pkg/cmd/kind/build/nodeimage" ) // NewCommand returns a new cobra.Command for building diff --git a/cmd/kind/build/nodeimage/nodeimage.go b/pkg/cmd/kind/build/nodeimage/nodeimage.go similarity index 100% rename from cmd/kind/build/nodeimage/nodeimage.go rename to pkg/cmd/kind/build/nodeimage/nodeimage.go diff --git a/cmd/kind/completion/bash/bash.go b/pkg/cmd/kind/completion/bash/bash.go similarity index 100% rename from cmd/kind/completion/bash/bash.go rename to pkg/cmd/kind/completion/bash/bash.go diff --git a/cmd/kind/completion/completion.go b/pkg/cmd/kind/completion/completion.go similarity index 94% rename from cmd/kind/completion/completion.go rename to pkg/cmd/kind/completion/completion.go index f9547a8e3f..5b186c9140 100644 --- a/cmd/kind/completion/completion.go +++ b/pkg/cmd/kind/completion/completion.go @@ -20,8 +20,8 @@ package completion import ( "github.com/spf13/cobra" - "sigs.k8s.io/kind/cmd/kind/completion/bash" - "sigs.k8s.io/kind/cmd/kind/completion/zsh" + "sigs.k8s.io/kind/pkg/cmd/kind/completion/bash" + "sigs.k8s.io/kind/pkg/cmd/kind/completion/zsh" ) // NewCommand returns a new cobra.Command for cluster creation diff --git a/cmd/kind/completion/zsh/zsh.go b/pkg/cmd/kind/completion/zsh/zsh.go similarity index 100% rename from cmd/kind/completion/zsh/zsh.go rename to pkg/cmd/kind/completion/zsh/zsh.go diff --git a/cmd/kind/create/cluster/createcluster.go b/pkg/cmd/kind/create/cluster/createcluster.go similarity index 100% rename from cmd/kind/create/cluster/createcluster.go rename to pkg/cmd/kind/create/cluster/createcluster.go diff --git a/cmd/kind/create/create.go b/pkg/cmd/kind/create/create.go similarity index 94% rename from cmd/kind/create/create.go rename to pkg/cmd/kind/create/create.go index e99a303fdf..2bb30dc52f 100644 --- a/cmd/kind/create/create.go +++ b/pkg/cmd/kind/create/create.go @@ -20,7 +20,7 @@ package create import ( "github.com/spf13/cobra" - createcluster "sigs.k8s.io/kind/cmd/kind/create/cluster" + createcluster "sigs.k8s.io/kind/pkg/cmd/kind/create/cluster" ) // NewCommand returns a new cobra.Command for cluster creation diff --git a/cmd/kind/delete/cluster/deletecluster.go b/pkg/cmd/kind/delete/cluster/deletecluster.go similarity index 100% rename from cmd/kind/delete/cluster/deletecluster.go rename to pkg/cmd/kind/delete/cluster/deletecluster.go diff --git a/cmd/kind/delete/delete.go b/pkg/cmd/kind/delete/delete.go similarity index 94% rename from cmd/kind/delete/delete.go rename to pkg/cmd/kind/delete/delete.go index 6711050647..fdef49f4ae 100644 --- a/cmd/kind/delete/delete.go +++ b/pkg/cmd/kind/delete/delete.go @@ -20,7 +20,7 @@ package delete import ( "github.com/spf13/cobra" - deletecluster "sigs.k8s.io/kind/cmd/kind/delete/cluster" + deletecluster "sigs.k8s.io/kind/pkg/cmd/kind/delete/cluster" ) // NewCommand returns a new cobra.Command for cluster creation diff --git a/cmd/kind/export/export.go b/pkg/cmd/kind/export/export.go similarity index 95% rename from cmd/kind/export/export.go rename to pkg/cmd/kind/export/export.go index 9ce1945483..dd3a7a29b9 100644 --- a/cmd/kind/export/export.go +++ b/pkg/cmd/kind/export/export.go @@ -20,7 +20,7 @@ package export import ( "github.com/spf13/cobra" - "sigs.k8s.io/kind/cmd/kind/export/logs" + "sigs.k8s.io/kind/pkg/cmd/kind/export/logs" ) // NewCommand returns a new cobra.Command for export diff --git a/cmd/kind/export/logs/logs.go b/pkg/cmd/kind/export/logs/logs.go similarity index 100% rename from cmd/kind/export/logs/logs.go rename to pkg/cmd/kind/export/logs/logs.go diff --git a/cmd/kind/get/clusters/clusters.go b/pkg/cmd/kind/get/clusters/clusters.go similarity index 100% rename from cmd/kind/get/clusters/clusters.go rename to pkg/cmd/kind/get/clusters/clusters.go diff --git a/cmd/kind/get/get.go b/pkg/cmd/kind/get/get.go similarity index 89% rename from cmd/kind/get/get.go rename to pkg/cmd/kind/get/get.go index 93f3ac786e..5528f601d0 100644 --- a/cmd/kind/get/get.go +++ b/pkg/cmd/kind/get/get.go @@ -20,9 +20,9 @@ package get import ( "github.com/spf13/cobra" - "sigs.k8s.io/kind/cmd/kind/get/clusters" - "sigs.k8s.io/kind/cmd/kind/get/kubeconfig" - "sigs.k8s.io/kind/cmd/kind/get/nodes" + "sigs.k8s.io/kind/pkg/cmd/kind/get/clusters" + "sigs.k8s.io/kind/pkg/cmd/kind/get/kubeconfig" + "sigs.k8s.io/kind/pkg/cmd/kind/get/nodes" ) // NewCommand returns a new cobra.Command for get diff --git a/cmd/kind/get/kubeconfig/kubeconfig.go b/pkg/cmd/kind/get/kubeconfig/kubeconfig.go similarity index 100% rename from cmd/kind/get/kubeconfig/kubeconfig.go rename to pkg/cmd/kind/get/kubeconfig/kubeconfig.go diff --git a/cmd/kind/get/nodes/nodes.go b/pkg/cmd/kind/get/nodes/nodes.go similarity index 100% rename from cmd/kind/get/nodes/nodes.go rename to pkg/cmd/kind/get/nodes/nodes.go diff --git a/cmd/kind/load/docker-image/docker-image.go b/pkg/cmd/kind/load/docker-image/docker-image.go similarity index 100% rename from cmd/kind/load/docker-image/docker-image.go rename to pkg/cmd/kind/load/docker-image/docker-image.go diff --git a/cmd/kind/load/image-archive/image-archive.go b/pkg/cmd/kind/load/image-archive/image-archive.go similarity index 100% rename from cmd/kind/load/image-archive/image-archive.go rename to pkg/cmd/kind/load/image-archive/image-archive.go diff --git a/cmd/kind/load/load.go b/pkg/cmd/kind/load/load.go similarity index 89% rename from cmd/kind/load/load.go rename to pkg/cmd/kind/load/load.go index 19ea7c0ad8..1188c0ebd3 100644 --- a/cmd/kind/load/load.go +++ b/pkg/cmd/kind/load/load.go @@ -20,8 +20,8 @@ package load import ( "github.com/spf13/cobra" - dockerimage "sigs.k8s.io/kind/cmd/kind/load/docker-image" - imagearchive "sigs.k8s.io/kind/cmd/kind/load/image-archive" + dockerimage "sigs.k8s.io/kind/pkg/cmd/kind/load/docker-image" + imagearchive "sigs.k8s.io/kind/pkg/cmd/kind/load/image-archive" ) // NewCommand returns a new cobra.Command for get diff --git a/cmd/kind/kind.go b/pkg/cmd/kind/root.go similarity index 86% rename from cmd/kind/kind.go rename to pkg/cmd/kind/root.go index df4253b274..e1b5b1292e 100644 --- a/cmd/kind/kind.go +++ b/pkg/cmd/kind/root.go @@ -22,14 +22,14 @@ import ( "github.com/spf13/cobra" - "sigs.k8s.io/kind/cmd/kind/build" - "sigs.k8s.io/kind/cmd/kind/completion" - "sigs.k8s.io/kind/cmd/kind/create" - "sigs.k8s.io/kind/cmd/kind/delete" - "sigs.k8s.io/kind/cmd/kind/export" - "sigs.k8s.io/kind/cmd/kind/get" - "sigs.k8s.io/kind/cmd/kind/load" - "sigs.k8s.io/kind/cmd/kind/version" + "sigs.k8s.io/kind/pkg/cmd/kind/build" + "sigs.k8s.io/kind/pkg/cmd/kind/completion" + "sigs.k8s.io/kind/pkg/cmd/kind/create" + "sigs.k8s.io/kind/pkg/cmd/kind/delete" + "sigs.k8s.io/kind/pkg/cmd/kind/export" + "sigs.k8s.io/kind/pkg/cmd/kind/get" + "sigs.k8s.io/kind/pkg/cmd/kind/load" + "sigs.k8s.io/kind/pkg/cmd/kind/version" "sigs.k8s.io/kind/pkg/errors" "sigs.k8s.io/kind/pkg/exec" "sigs.k8s.io/kind/pkg/globals" @@ -52,7 +52,11 @@ func NewCommand() *cobra.Command { Short: "kind is a tool for managing local Kubernetes clusters", Long: "kind creates and manages local Kubernetes clusters using Docker container 'nodes'", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return runE(flags, cmd) + err := runE(flags, cmd) + if err != nil { + logError(err) + } + return err }, SilenceUsage: true, SilenceErrors: true, @@ -115,19 +119,6 @@ func runE(flags *Flags, cmd *cobra.Command) error { return nil } -// Run runs the `kind` root command -func Run() error { - return NewCommand().Execute() -} - -// Main wraps Run and sets the log formatter -func Main() { - if err := Run(); err != nil { - logError(err) - os.Exit(1) - } -} - // logError logs the error and the root stacktrace if there is one func logError(err error) { globals.GetLogger().Errorf("ERROR: %v", err) diff --git a/cmd/kind/version/version.go b/pkg/cmd/kind/version/version.go similarity index 100% rename from cmd/kind/version/version.go rename to pkg/cmd/kind/version/version.go diff --git a/cmd/kind/version/version_test.go b/pkg/cmd/kind/version/version_test.go similarity index 100% rename from cmd/kind/version/version_test.go rename to pkg/cmd/kind/version/version_test.go