-
Notifications
You must be signed in to change notification settings - Fork 2
chore(modular-cmds): flags helper, docs #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6231027
chore(modular-cmds): flags helper, docs
ajaskolski 737e480
move loading data before executing in the command, move verification …
ajaskolski e5d8621
update: return error when building new cmds
ajaskolski d1bb809
add more comment fixes
ajaskolski 84445d3
nit updates
ajaskolski e35c4c9
cleaned flags approach
ajaskolski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| // Package flags provides reusable flag helpers for CLI commands. | ||
| // | ||
| // This package should only contain common flags that can be used by multiple commands | ||
| // to ensure unified naming and consistent behavior across the CLI. | ||
| // Command-specific flags should be defined locally in the command file. | ||
| package flags | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| // MustString returns the string value, ignoring the error. | ||
| // Safe to use with registered flags where GetString cannot fail. | ||
| func MustString(s string, _ error) string { return s } | ||
|
|
||
| // MustBool returns the bool value, ignoring the error. | ||
| // Safe to use with registered flags where GetBool cannot fail. | ||
| func MustBool(b bool, _ error) bool { return b } | ||
|
|
||
| // Environment adds the required --environment/-e flag to a command. | ||
| // Retrieve the value with cmd.Flags().GetString("environment"). | ||
| // | ||
| // Usage: | ||
| // | ||
| // flags.Environment(cmd) | ||
| // // later in RunE: | ||
| // env, _ := cmd.Flags().GetString("environment") | ||
| func Environment(cmd *cobra.Command) { | ||
| cmd.Flags().StringP("environment", "e", "", "Deployment environment (required)") | ||
| _ = cmd.MarkFlagRequired("environment") | ||
| } | ||
|
|
||
| // Print adds the --print flag for printing output to stdout (default: true). | ||
| // Retrieve the value with cmd.Flags().GetBool("print"). | ||
| // | ||
| // Usage: | ||
| // | ||
| // flags.Print(cmd) | ||
| // // later in RunE: | ||
| // shouldPrint, _ := cmd.Flags().GetBool("print") | ||
| func Print(cmd *cobra.Command) { | ||
| cmd.Flags().Bool("print", true, "Print output to stdout") | ||
| } | ||
|
|
||
| // Output adds the --out/-o flag for specifying output file path. | ||
| // Also supports deprecated --outputPath alias for backwards compatibility. | ||
| // Retrieve the value with cmd.Flags().GetString("out"). | ||
| // | ||
| // Usage: | ||
| // | ||
| // flags.Output(cmd, "") | ||
| // // later in RunE: | ||
| // outPath, _ := cmd.Flags().GetString("out") | ||
| func Output(cmd *cobra.Command, defaultValue string) { | ||
| cmd.Flags().StringP("out", "o", defaultValue, "Output file path") | ||
|
|
||
| // Normalize --outputPath to --out for backward compatibility (silent) | ||
| existingNormalize := cmd.Flags().GetNormalizeFunc() | ||
| cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { | ||
| if name == "outputPath" { | ||
| return pflag.NormalizedName("out") | ||
| } | ||
| if existingNormalize != nil { | ||
| return existingNormalize(f, name) | ||
| } | ||
|
|
||
| return pflag.NormalizedName(name) | ||
| }) | ||
| } | ||
This file contains hidden or 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,146 @@ | ||
| package flags | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEnvironment(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("flag properties", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test"} | ||
| Environment(cmd) | ||
|
|
||
| f := cmd.Flags().Lookup("environment") | ||
| require.NotNil(t, f) | ||
| assert.Equal(t, "e", f.Shorthand) | ||
| assert.Empty(t, f.DefValue) | ||
| }) | ||
|
|
||
| t.Run("is required", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test"} | ||
| Environment(cmd) | ||
|
|
||
| err := cmd.ValidateRequiredFlags() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "environment") | ||
| }) | ||
|
|
||
| t.Run("value retrieval", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test", Run: func(cmd *cobra.Command, _ []string) {}} | ||
| Environment(cmd) | ||
|
|
||
| cmd.SetArgs([]string{"-e", "staging"}) | ||
| err := cmd.Execute() | ||
|
|
||
| require.NoError(t, err) | ||
| env, _ := cmd.Flags().GetString("environment") | ||
| assert.Equal(t, "staging", env) | ||
| }) | ||
| } | ||
|
|
||
| func TestPrint(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("default true", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test"} | ||
| Print(cmd) | ||
|
|
||
| f := cmd.Flags().Lookup("print") | ||
| require.NotNil(t, f) | ||
| assert.Empty(t, f.Shorthand) | ||
| assert.Equal(t, "true", f.DefValue) | ||
| }) | ||
|
|
||
| t.Run("value retrieval", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test", Run: func(cmd *cobra.Command, _ []string) {}} | ||
| Print(cmd) | ||
|
|
||
| cmd.SetArgs([]string{"--print"}) | ||
| err := cmd.Execute() | ||
|
|
||
| require.NoError(t, err) | ||
| shouldPrint, _ := cmd.Flags().GetBool("print") | ||
| assert.True(t, shouldPrint) | ||
| }) | ||
| } | ||
|
|
||
| func TestOutput(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("new flag name", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test", Run: func(cmd *cobra.Command, _ []string) {}} | ||
| Output(cmd, "") | ||
|
|
||
| f := cmd.Flags().Lookup("out") | ||
| require.NotNil(t, f) | ||
| assert.Equal(t, "o", f.Shorthand) | ||
|
|
||
| cmd.SetArgs([]string{"-o", "/new/path.json"}) | ||
| err := cmd.Execute() | ||
|
|
||
| require.NoError(t, err) | ||
| out, _ := cmd.Flags().GetString("out") | ||
| assert.Equal(t, "/new/path.json", out) | ||
| }) | ||
|
|
||
| t.Run("deprecated alias still works", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test", Run: func(cmd *cobra.Command, _ []string) {}} | ||
| Output(cmd, "") | ||
|
|
||
| // --outputPath is normalized to --out, so Lookup finds the same flag | ||
| cmd.SetArgs([]string{"--outputPath", "/old/path.json"}) | ||
| err := cmd.Execute() | ||
|
|
||
| require.NoError(t, err) | ||
| out, _ := cmd.Flags().GetString("out") | ||
| assert.Equal(t, "/old/path.json", out) | ||
| }) | ||
|
|
||
| t.Run("default value", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "test"} | ||
| Output(cmd, "default.json") | ||
|
|
||
| f := cmd.Flags().Lookup("out") | ||
| require.NotNil(t, f) | ||
| assert.Equal(t, "default.json", f.DefValue) | ||
| }) | ||
| } | ||
|
|
||
| func TestFlagsAreLocal(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| parent := &cobra.Command{Use: "parent", Run: func(cmd *cobra.Command, _ []string) {}} | ||
| child := &cobra.Command{Use: "child", Run: func(cmd *cobra.Command, _ []string) {}} | ||
|
|
||
| Environment(parent) | ||
| parent.AddCommand(child) | ||
|
|
||
| // Child should NOT have the environment flag | ||
| f := child.Flags().Lookup("environment") | ||
| assert.Nil(t, f, "local flags should not be inherited by subcommands") | ||
|
|
||
| // But parent should have it | ||
| pf := parent.Flags().Lookup("environment") | ||
| assert.NotNil(t, pf) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.