Skip to content

Commit

Permalink
feat: new cmd directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Mar 25, 2023
1 parent 1deda62 commit 92ac213
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 180 deletions.
36 changes: 0 additions & 36 deletions cmd/bar.go

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/foo.go

This file was deleted.

41 changes: 41 additions & 0 deletions cmd/foo/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package foo

import (
"github.com/bilalcaliskan/golang-cli-template/cmd/foo/options"

rootopts "github.com/bilalcaliskan/golang-cli-template/cmd/root/options"

"github.com/bilalcaliskan/golang-cli-template/internal/logging"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)

func init() {
logger = logging.GetLogger()
fooOpts = options.GetFooOptions()
fooOpts.InitFlags(FooCmd)
}

var (
logger zerolog.Logger
fooOpts *options.FooOptions
// FooCmd represents the bar command
FooCmd = &cobra.Command{
Use: "foo",
Short: "",
PreRunE: func(cmd *cobra.Command, args []string) error {
logger = cmd.Context().Value(rootopts.LoggerKey{}).(zerolog.Logger)
rootOpts := cmd.Context().Value(rootopts.OptsKey{}).(*rootopts.RootOptions)
fooOpts.RootOptions = rootOpts

// flag validation logic here

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
logger.Info().Str("rootOptsKey", fooOpts.RootOptions.Key).Msg("hello guys")

return nil
},
}
)
24 changes: 24 additions & 0 deletions cmd/foo/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package options

import (
"github.com/bilalcaliskan/golang-cli-template/cmd/root/options"
"github.com/spf13/cobra"
)

var fooOptions = &FooOptions{}

// FooOptions contains frequent command line and application options.
type FooOptions struct {
// Bar is the dummy option
*options.RootOptions
Bar string
}

// GetFooOptions returns the pointer of GolangCliTemplateOptions
func GetFooOptions() *FooOptions {
return fooOptions
}

func (opts *FooOptions) InitFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&opts.Bar, "bar", "", "", "")
}
19 changes: 19 additions & 0 deletions cmd/foo/options/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package options

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestGetFooOptions(t *testing.T) {
opts := GetFooOptions()
assert.NotNil(t, opts)
}

func TestFooOptions_InitFlags(t *testing.T) {
cmd := cobra.Command{}
opts := GetFooOptions()
opts.InitFlags(&cmd)
}
52 changes: 0 additions & 52 deletions cmd/root.go

This file was deleted.

25 changes: 25 additions & 0 deletions cmd/root/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package options

import "github.com/spf13/cobra"

var rootOptions = &RootOptions{}

type (
OptsKey struct{}
LoggerKey struct{}
)

// RootOptions contains frequent command line and application options.
type RootOptions struct {
// Key is the dummy option
Key string
}

// GetRootOptions returns the pointer of GolangCliTemplateOptions
func GetRootOptions() *RootOptions {
return rootOptions
}

func (opts *RootOptions) InitFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&opts.Key, "key", "", "", "")
}
19 changes: 19 additions & 0 deletions cmd/root/options/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package options

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestGetRootOptions(t *testing.T) {
opts := GetRootOptions()
assert.NotNil(t, opts)
}

func TestRootOptions_InitFlags(t *testing.T) {
cmd := cobra.Command{}
opts := GetRootOptions()
opts.InitFlags(&cmd)
}
61 changes: 61 additions & 0 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package root

import (
"context"
"os"

"github.com/bilalcaliskan/golang-cli-template/cmd/foo"
"github.com/bilalcaliskan/golang-cli-template/cmd/root/options"

"github.com/bilalcaliskan/golang-cli-template/internal/logging"
"github.com/bilalcaliskan/golang-cli-template/internal/version"
"github.com/spf13/cobra"
)

var (
opts *options.RootOptions
ver = version.Get()
//bannerFilePath = "build/ci/banner.txt"
)

func init() {
opts = options.GetRootOptions()
opts.InitFlags(rootCmd)

rootCmd.AddCommand(foo.FooCmd)
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "golang-cli-template",
Short: "",
Long: ``,
Version: ver.GitVersion,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
//if _, err := os.Stat("build/ci/banner.txt"); err == nil {
// bannerBytes, _ := os.ReadFile("build/ci/banner.txt")
// banner.Init(os.Stdout, true, false, strings.NewReader(string(bannerBytes)))
//}

logger := logging.GetLogger()
logger.Info().Str("appVersion", ver.GitVersion).Str("goVersion", ver.GoVersion).Str("goOS", ver.GoOs).
Str("goArch", ver.GoArch).Str("gitCommit", ver.GitCommit).Str("buildDate", ver.BuildDate).
Msg("golang-cli-template is started!")

cmd.SetContext(context.WithValue(cmd.Context(), options.LoggerKey{}, logger))
cmd.SetContext(context.WithValue(cmd.Context(), options.OptsKey{}, opts))

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ module github.com/bilalcaliskan/golang-cli-template
go 1.20

require (
github.com/rs/zerolog v1.29.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.2
go.uber.org/zap v1.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
27 changes: 18 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand All @@ -13,9 +14,19 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
Expand All @@ -28,13 +39,11 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
Loading

0 comments on commit 92ac213

Please sign in to comment.