-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1deda62
commit 92ac213
Showing
16 changed files
with
233 additions
and
180 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "", "", "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "", "", "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.