From 5be8b9d28e4998d2a6dfe9071b4ba03dc052644d Mon Sep 17 00:00:00 2001 From: Naohiro CHIKAMATSU Date: Wed, 10 Jan 2024 17:32:54 +0900 Subject: [PATCH] Introduce cfn command template --- .gitignore | 1 + Makefile | 4 +++- cmd/cfn/main.go | 16 ++++++++++++++++ cmd/subcmd/cfn/common.go | 6 ++++++ cmd/subcmd/cfn/root.go | 30 ++++++++++++++++++++++++++++++ cmd/subcmd/cfn/version.go | 22 ++++++++++++++++++++++ cmd/subcmd/cfn/version_test.go | 30 ++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 cmd/cfn/main.go create mode 100644 cmd/subcmd/cfn/common.go create mode 100644 cmd/subcmd/cfn/root.go create mode 100644 cmd/subcmd/cfn/version.go create mode 100644 cmd/subcmd/cfn/version_test.go diff --git a/.gitignore b/.gitignore index 779ea08..72b27cf 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ data localstack /s3hub /spare +/cfn .envrc diff --git a/Makefile b/Makefile index 432bdff..fb0cb20 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ S3HUB = s3hub SPARE = spare +CFN = cfn VERSION = $(shell git describe --tags --abbrev=0) GO = go GO_BUILD = $(GO) build @@ -18,9 +19,10 @@ GO_LDFLAGS = -ldflags '-X github.com/nao1215/rainbow/version.Version=${VERSION} build: ## Build binary env GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) $(GO_LDFLAGS) -o $(S3HUB) cmd/s3hub/main.go env GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) $(GO_LDFLAGS) -o $(SPARE) cmd/spare/main.go + env GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) $(GO_LDFLAGS) -o $(CFN) cmd/cfn/main.go clean: ## Clean project - -rm -rf $(S3HUB) $(SPARE) cover.out cover.html + -rm -rf $(S3HUB) $(SPARE) $(CFN) cover.out cover.html test: ## Start unit test env GOOS=$(GOOS) $(GO_TEST) -coverpkg=./... -coverprofile=cover.out.tmp -cover ./... diff --git a/cmd/cfn/main.go b/cmd/cfn/main.go new file mode 100644 index 0000000..dc56161 --- /dev/null +++ b/cmd/cfn/main.go @@ -0,0 +1,16 @@ +// Package main is the entrypoint of s3hub. +package main + +import ( + "fmt" + "os" + + "github.com/nao1215/rainbow/cmd/subcmd/cfn" +) + +func main() { + if err := cfn.Execute(); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } +} diff --git a/cmd/subcmd/cfn/common.go b/cmd/subcmd/cfn/common.go new file mode 100644 index 0000000..30e0908 --- /dev/null +++ b/cmd/subcmd/cfn/common.go @@ -0,0 +1,6 @@ +package cfn + +// commandName returns the cfn command name. +func commandName() string { + return "cfn" +} diff --git a/cmd/subcmd/cfn/root.go b/cmd/subcmd/cfn/root.go new file mode 100644 index 0000000..0779ad8 --- /dev/null +++ b/cmd/subcmd/cfn/root.go @@ -0,0 +1,30 @@ +// Package cfn is the root command of cfn. +package cfn + +import ( + "github.com/spf13/cobra" +) + +// Execute starts the root command of cfn command. +func Execute() error { + if err := newRootCmd().Execute(); err != nil { + return err + } + return nil +} + +// newRootCmd returns a root command for cfn. +func newRootCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "cfn", + Short: `cfn is a command line tool for AWS CloudFormation.`, + } + + cmd.CompletionOptions.DisableDefaultCmd = true + cmd.SilenceUsage = true + cmd.SilenceErrors = true + cmd.DisableFlagParsing = true + + cmd.AddCommand(newVersionCmd()) + return cmd +} diff --git a/cmd/subcmd/cfn/version.go b/cmd/subcmd/cfn/version.go new file mode 100644 index 0000000..ea23262 --- /dev/null +++ b/cmd/subcmd/cfn/version.go @@ -0,0 +1,22 @@ +package cfn + +import ( + "fmt" + + ver "github.com/nao1215/rainbow/version" + "github.com/spf13/cobra" +) + +// newVersionCmd return version command. +func newVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: fmt.Sprintf("Print %s version", commandName()), + Run: version, + } +} + +// version return s3hub command version. +func version(cmd *cobra.Command, _ []string) { + cmd.Printf("%s %s (under MIT LICENSE)\n", commandName(), ver.GetVersion()) +} diff --git a/cmd/subcmd/cfn/version_test.go b/cmd/subcmd/cfn/version_test.go new file mode 100644 index 0000000..578cb87 --- /dev/null +++ b/cmd/subcmd/cfn/version_test.go @@ -0,0 +1,30 @@ +package cfn + +import ( + "bytes" + "testing" + + ver "github.com/nao1215/rainbow/version" +) + +func Test_version(t *testing.T) { + t.Run("Get version information", func(t *testing.T) { + cmd := newVersionCmd() + stdout := bytes.NewBufferString("") + cmd.SetOutput(stdout) + + orgVersion := ver.Version + ver.Version = "v0.0.0" + t.Cleanup(func() { + ver.Version = orgVersion + }) + + cmd.Run(cmd, []string{}) + + want := "s3hub v0.0.0 (under MIT LICENSE)\n" + got := stdout.String() + if got != want { + t.Errorf("got %s, want %s", got, want) + } + }) +}