Skip to content

Commit

Permalink
Merge pull request #5 from nao1215/feat/s3hub
Browse files Browse the repository at this point in the history
Add: s3hub subcommand entrypoint. all subcommnads are not implemented yet
  • Loading branch information
nao1215 authored Dec 24, 2023
2 parents 9b1d5a3 + cc86628 commit 4070010
Show file tree
Hide file tree
Showing 20 changed files with 320 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ go.work
cover.*
data
localstack
/s3hub
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
.PHONY: build test clean changelog tools help

APP = dummy
S3HUB = s3hub
VERSION = $(shell git describe --tags --abbrev=0)
GO = go
GO_BUILD = $(GO) build
GO_FORMAT = $(GO) fmt
GOFMT = gofmt
GO_LIST = $(GO) list
GO_INSTALL = $(GO) install
GO_TEST = $(GO) test -v
GO_TOOL = $(GO) tool
GO_VET = $(GO) vet
GO_DEP = $(GO) mod
GOOS = ""
GOARCH = ""
Expand All @@ -19,11 +15,10 @@ GO_PACKAGES = $(shell $(GO_LIST) $(GO_PKGROOT))
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 $(APP) main.go
echo "build"
env GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) $(GO_LDFLAGS) -o $(S3HUB) cmd/s3hub/main.go

clean: ## Clean project
-rm -rf $(APP) cover.out cover.html
-rm -rf $(S3HUB) cover.out cover.html

test: ## Start test
env GOOS=$(GOOS) $(GO_TEST) -cover $(GO_PKGROOT) -coverprofile=cover.out
Expand Down
Empty file removed cmd/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions cmd/s3hub/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/s3hub"
)

func main() {
if err := s3hub.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
18 changes: 18 additions & 0 deletions cmd/subcmd/s3hub/cp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package s3hub

import "github.com/spf13/cobra"

// newCpCmd return cp command.
func newCpCmd() *cobra.Command {
return &cobra.Command{
Use: "cp",
Short: "Copy file from local(S3 bucket) to S3 bucket(local)",
RunE: cp,
}
}

// cp is the entrypoint of cp command.
func cp(cmd *cobra.Command, _ []string) error {
cmd.Println("cp is not implemented yet")
return nil
}
24 changes: 24 additions & 0 deletions cmd/subcmd/s3hub/cp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package s3hub

import (
"bytes"
"testing"
)

func Test_cp(t *testing.T) {
t.Run("Copy file from local(S3 bucket) to S3 bucket(local)", func(t *testing.T) {
cmd := newCpCmd()
stdout := bytes.NewBufferString("")
cmd.SetOutput(stdout)

if err := cmd.RunE(cmd, []string{}); err != nil {
t.Errorf("got %v, want nil", err)
}

want := "cp is not implemented yet\n"
got := stdout.String()
if got != want {
t.Errorf("got %s, want %s", got, want)
}
})
}
8 changes: 8 additions & 0 deletions cmd/subcmd/s3hub/interactive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package s3hub

import "fmt"

func interactive() error {
fmt.Println("interactive mode is not implemented yet")
return nil
}
11 changes: 11 additions & 0 deletions cmd/subcmd/s3hub/interactive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package s3hub

import "testing"

func Test_interactive(t *testing.T) {
t.Run("Interactive mode", func(t *testing.T) {
if err := interactive(); err != nil {
t.Errorf("got %v, want nil", err)
}
})
}
18 changes: 18 additions & 0 deletions cmd/subcmd/s3hub/ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package s3hub

import "github.com/spf13/cobra"

// newLsCmd return ls command.
func newLsCmd() *cobra.Command {
return &cobra.Command{
Use: "ls",
Short: "List S3 buckets",
RunE: ls,
}
}

// ls is the entrypoint of ls command.
func ls(cmd *cobra.Command, _ []string) error {
cmd.Println("ls is not implemented yet")
return nil
}
24 changes: 24 additions & 0 deletions cmd/subcmd/s3hub/ls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package s3hub

import (
"bytes"
"testing"
)

func Test_ls(t *testing.T) {
t.Run("List S3 buckets", func(t *testing.T) {
cmd := newLsCmd()
stdout := bytes.NewBufferString("")
cmd.SetOutput(stdout)

if err := cmd.RunE(cmd, []string{}); err != nil {
t.Errorf("got %v, want nil", err)
}

want := "ls is not implemented yet\n"
got := stdout.String()
if got != want {
t.Errorf("got %s, want %s", got, want)
}
})
}
18 changes: 18 additions & 0 deletions cmd/subcmd/s3hub/mb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package s3hub

import "github.com/spf13/cobra"

// newMbCmd return mb command. mb means make bucket.
func newMbCmd() *cobra.Command {
return &cobra.Command{
Use: "mb",
Short: "Make S3 bucket",
RunE: mb,
}
}

// mb is the entrypoint of mb command.
func mb(cmd *cobra.Command, _ []string) error {
cmd.Println("mb is not implemented yet")
return nil
}
24 changes: 24 additions & 0 deletions cmd/subcmd/s3hub/mb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package s3hub

import (
"bytes"
"testing"
)

func Test_mb(t *testing.T) {
t.Run("Make S3 bucket", func(t *testing.T) {
cmd := newMbCmd()
stdout := bytes.NewBufferString("")
cmd.SetOutput(stdout)

if err := cmd.RunE(cmd, []string{}); err != nil {
t.Errorf("got %v, want nil", err)
}

want := "mb is not implemented yet\n"
got := stdout.String()
if got != want {
t.Errorf("got %s, want %s", got, want)
}
})
}
18 changes: 18 additions & 0 deletions cmd/subcmd/s3hub/rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package s3hub

import "github.com/spf13/cobra"

// newRmCmd return rm command.
func newRmCmd() *cobra.Command {
return &cobra.Command{
Use: "rm",
Short: "Remove contents from S3 bucket (or remove S3 bucket)",
RunE: rm,
}
}

// rm is the entrypoint of rm command.
func rm(cmd *cobra.Command, _ []string) error {
cmd.Println("rm is not implemented yet")
return nil
}
24 changes: 24 additions & 0 deletions cmd/subcmd/s3hub/rm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package s3hub

import (
"bytes"
"testing"
)

func Test_rm(t *testing.T) {
t.Run("Remove contents from S3 bucket (or remove S3 bucket)", func(t *testing.T) {
cmd := newRmCmd()
stdout := bytes.NewBufferString("")
cmd.SetOutput(stdout)

if err := cmd.RunE(cmd, []string{}); err != nil {
t.Errorf("got %v, want nil", err)
}

want := "rm is not implemented yet\n"
got := stdout.String()
if got != want {
t.Errorf("got %s, want %s", got, want)
}
})
}
41 changes: 41 additions & 0 deletions cmd/subcmd/s3hub/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Package s3hub is the root command of s3hub.
package s3hub

import (
"os"

"github.com/spf13/cobra"
)

// Execute starts the root command of s3hub.
func Execute() error {
if len(os.Args) == 1 {
return interactive()
}
return newRootCmd().Execute()
}

// newRootCmd returns a root command for s3hub.
func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "s3hub",
Short: "user-friendly S3 buckets management tool",
}
cmd.CompletionOptions.DisableDefaultCmd = true
cmd.SilenceUsage = true
cmd.SilenceErrors = true
cmd.DisableFlagParsing = true

cmd.AddCommand(newVersionCmd())
cmd.AddCommand(newMbCmd())
cmd.AddCommand(newLsCmd())
cmd.AddCommand(newRmCmd())
cmd.AddCommand(newCpCmd())

return cmd
}

// commandName returns the s3hub command name.
func commandName() string {
return "s3hub"
}
22 changes: 22 additions & 0 deletions cmd/subcmd/s3hub/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package s3hub

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/s3hub/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package s3hub

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)
}
})
}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/nao1215/rainbow

go 1.19

require github.com/google/go-cmp v0.6.0
require (
github.com/google/go-cmp v0.6.0
github.com/spf13/cobra v1.8.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions ui/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 4070010

Please sign in to comment.