Skip to content

Commit

Permalink
Introduce cfn command template
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Jan 10, 2024
1 parent 71692f4 commit 5be8b9d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ data
localstack
/s3hub
/spare
/cfn
.envrc
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3HUB = s3hub
SPARE = spare
CFN = cfn
VERSION = $(shell git describe --tags --abbrev=0)
GO = go
GO_BUILD = $(GO) build
Expand All @@ -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 ./...
Expand Down
16 changes: 16 additions & 0 deletions cmd/cfn/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions cmd/subcmd/cfn/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cfn

// commandName returns the cfn command name.
func commandName() string {
return "cfn"
}
30 changes: 30 additions & 0 deletions cmd/subcmd/cfn/root.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions cmd/subcmd/cfn/version.go
Original file line number Diff line number Diff line change
@@ -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())
}
30 changes: 30 additions & 0 deletions cmd/subcmd/cfn/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

0 comments on commit 5be8b9d

Please sign in to comment.