-
Notifications
You must be signed in to change notification settings - Fork 37
chore: introduce flags helper for consistency #533
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
Draft
rhamzeh
wants to merge
3
commits into
main
Choose a base branch
from
chore/introduce-flags-helper-for-consistency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,101 @@ | ||
// Package flags provides utility functions for working with cobra command flags. | ||
// It simplifies the process of marking flags as required and handling related errors. | ||
package flags | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// ErrFlagRequired is returned when a flag cannot be marked as required. | ||
ErrFlagRequired = errors.New("error setting flag as required") | ||
|
||
// ErrInvalidInput is returned when invalid input is provided. | ||
ErrInvalidInput = errors.New("invalid input") | ||
) | ||
|
||
// buildFlagRequiredError creates a consistent error message for flag requirement failures. | ||
// It wraps the original error with context about which flag and location failed. | ||
func buildFlagRequiredError(flag, location string, err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%w - (flag: %s, file: %s): %v", ErrFlagRequired, flag, location, err) | ||
} | ||
|
||
// SetFlagRequired marks a single flag as required for a cobra command. | ||
// | ||
// Parameters: | ||
// - cmd: The cobra command to modify | ||
// - flag: The name of the flag to mark as required | ||
// - location: A string identifying the calling location (for error context) | ||
// - isPersistent: If true, marks the persistent flag as required; otherwise marks the regular flag | ||
// | ||
// Returns an error if: | ||
// - cmd is nil | ||
// - flag is empty | ||
// - the flag cannot be marked as required (e.g., flag doesn't exist) | ||
func SetFlagRequired(cmd *cobra.Command, flag string, location string, isPersistent bool) error { | ||
if cmd == nil { | ||
return fmt.Errorf("%w: command cannot be nil", ErrInvalidInput) | ||
} | ||
|
||
if strings.TrimSpace(flag) == "" { | ||
return fmt.Errorf("%w: flag name cannot be empty", ErrInvalidInput) | ||
} | ||
|
||
if isPersistent { | ||
if err := cmd.MarkPersistentFlagRequired(flag); err != nil { | ||
return buildFlagRequiredError(flag, location, err) | ||
} | ||
} else { | ||
if err := cmd.MarkFlagRequired(flag); err != nil { | ||
return buildFlagRequiredError(flag, location, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// SetFlagsRequired marks multiple flags as required for a cobra command. | ||
// | ||
// Parameters: | ||
// - cmd: The cobra command to modify | ||
// - flags: A slice of flag names to mark as required | ||
// - location: A string identifying the calling location (for error context) | ||
// - isPersistent: If true, marks the persistent flags as required; otherwise marks the regular flags | ||
// | ||
// Returns a joined error containing all individual flag requirement failures. | ||
// If no flags are provided or all succeed, returns nil. | ||
// | ||
// Note: This function continues processing all flags even if some fail, | ||
// allowing you to see all failures at once rather than stopping at the first error. | ||
func SetFlagsRequired(cmd *cobra.Command, flags []string, location string, isPersistent bool) error { | ||
if cmd == nil { | ||
return fmt.Errorf("%w: command cannot be nil", ErrInvalidInput) | ||
} | ||
|
||
if len(flags) == 0 { | ||
return nil | ||
} | ||
|
||
// Pre-allocate slice with exact capacity needed | ||
flagErrors := make([]error, 0, len(flags)) | ||
|
||
for _, flag := range flags { | ||
if err := SetFlagRequired(cmd, flag, location, isPersistent); err != nil { | ||
flagErrors = append(flagErrors, err) | ||
} | ||
} | ||
|
||
if len(flagErrors) > 0 { | ||
return errors.Join(flagErrors...) | ||
} | ||
|
||
return nil | ||
} |
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.