From ee767cd18bf6e750317128efd5d058aa2a26bd2c Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Sun, 14 Jan 2024 03:02:56 +0300 Subject: [PATCH] feat: add options --- cmd/foo/foo.go | 38 --------------------------------- cmd/foo/options/options.go | 24 --------------------- cmd/foo/options/options_test.go | 19 ----------------- cmd/root/options/options.go | 9 +++++--- cmd/root/root.go | 14 ++++-------- 5 files changed, 10 insertions(+), 94 deletions(-) delete mode 100644 cmd/foo/foo.go delete mode 100644 cmd/foo/options/options.go delete mode 100644 cmd/foo/options/options_test.go diff --git a/cmd/foo/foo.go b/cmd/foo/foo.go deleted file mode 100644 index cdb30cc..0000000 --- a/cmd/foo/foo.go +++ /dev/null @@ -1,38 +0,0 @@ -package foo - -import ( - "github.com/bilalcaliskan/split-the-tunnel/cmd/foo/options" - - rootopts "github.com/bilalcaliskan/split-the-tunnel/cmd/root/options" - - "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 { - - return nil - }, - } -) diff --git a/cmd/foo/options/options.go b/cmd/foo/options/options.go deleted file mode 100644 index b271a49..0000000 --- a/cmd/foo/options/options.go +++ /dev/null @@ -1,24 +0,0 @@ -package options - -import ( - "github.com/bilalcaliskan/split-the-tunnel/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 FooOptions -func GetFooOptions() *FooOptions { - return fooOptions -} - -func (opts *FooOptions) InitFlags(cmd *cobra.Command) { - cmd.PersistentFlags().StringVarP(&opts.Bar, "bar", "", "", "") -} diff --git a/cmd/foo/options/options_test.go b/cmd/foo/options/options_test.go deleted file mode 100644 index 24058d8..0000000 --- a/cmd/foo/options/options_test.go +++ /dev/null @@ -1,19 +0,0 @@ -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) -} diff --git a/cmd/root/options/options.go b/cmd/root/options/options.go index 1821b4a..d24a0c2 100644 --- a/cmd/root/options/options.go +++ b/cmd/root/options/options.go @@ -7,12 +7,14 @@ var rootOptions = &RootOptions{} type ( OptsKey struct{} LoggerKey struct{} + DomainKey struct{} + DnsKey struct{} ) // RootOptions contains frequent command line and application options. type RootOptions struct { - // Key is the dummy option - Key string + Domain string + DnsServers string } // GetRootOptions returns the pointer of RootOptions @@ -21,5 +23,6 @@ func GetRootOptions() *RootOptions { } func (opts *RootOptions) InitFlags(cmd *cobra.Command) { - cmd.PersistentFlags().StringVarP(&opts.Key, "key", "", "", "") + cmd.Flags().StringVarP(&opts.Domain, "domain", "", "", "domain to be used for split tunneling") + cmd.Flags().StringVarP(&opts.DnsServers, "dns-servers", "", "", "comma separated dns servers to be used for split tunneling") } diff --git a/cmd/root/root.go b/cmd/root/root.go index aee4db1..2cc4887 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -4,7 +4,6 @@ import ( "context" "os" - "github.com/bilalcaliskan/split-the-tunnel/cmd/foo" "github.com/bilalcaliskan/split-the-tunnel/cmd/root/options" "github.com/bilalcaliskan/split-the-tunnel/internal/logging" @@ -15,14 +14,11 @@ import ( 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 @@ -32,15 +28,13 @@ var rootCmd = &cobra.Command{ 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!") + Msg("split-the-tunnel is started!") + + logger.Info().Msg(opts.Domain) + logger.Info().Msg(opts.DnsServers) cmd.SetContext(context.WithValue(cmd.Context(), options.LoggerKey{}, logger)) cmd.SetContext(context.WithValue(cmd.Context(), options.OptsKey{}, opts))