Skip to content

Commit

Permalink
feat: improve error message of when a set of flags is required to use…
Browse files Browse the repository at this point in the history
… together

Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 committed Apr 18, 2024
1 parent 2edebad commit 245ac1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions cmd/oras/internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,19 @@ func CheckMutuallyExclusiveFlags(fs *pflag.FlagSet, exclusiveFlagSets ...[]strin
}
return nil
}

// CheckRequiredTogetherFlags checks if any flags required together are all set,
// returns an error when detecting any of the required flags not set.
func CheckRequiredTogetherFlags(fs *pflag.FlagSet, requiredTogetherFlags ...string) error {
var unchangedFlags []string
for _, flagName := range requiredTogetherFlags {
if !fs.Changed(flagName) {
unchangedFlags = append(unchangedFlags, fmt.Sprintf("--%s", flagName))
}
}
if len(unchangedFlags) > 0 {
flags := strings.Join(unchangedFlags, ", ")
return fmt.Errorf("%s required but not provided", flags)
}
return nil
}
6 changes: 3 additions & 3 deletions cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ func (opts *Remote) Parse(cmd *cobra.Command) error {
if err := opts.parseCustomHeaders(); err != nil {
return err
}

cmd.MarkFlagsRequiredTogether(certFileAndKeyFileFlags...)

if err := oerrors.CheckRequiredTogetherFlags(cmd.Flags(), certFileAndKeyFileFlags...); err != nil {
return err
}
return opts.readSecret(cmd)
}

Expand Down

0 comments on commit 245ac1e

Please sign in to comment.