Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"fmt"

cli "github.com/spf13/cobra"
)

// mustMarkRequired marks each named flag as required. It panics when a flag
// is not defined on the command, so a mistyped or renamed flag name fails on
// the first run instead of silently making the flag optional.
func mustMarkRequired(cmd *cli.Command, names ...string) {
for _, name := range names {
if err := cmd.MarkFlagRequired(name); err != nil {
panic(fmt.Sprintf("marking --%s required on %q: %v", name, cmd.Name(), err))
}
}
}
24 changes: 24 additions & 0 deletions cmd/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"testing"

cli "github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestMustMarkRequired(t *testing.T) {
t.Run("should mark an existing flag as required", func(t *testing.T) {
cmd := &cli.Command{Use: "test"}
cmd.Flags().String("file", "", "")

assert.NotPanics(t, func() { mustMarkRequired(cmd, "file") })
assert.Error(t, cmd.ValidateRequiredFlags())
})

t.Run("should panic on an unknown flag name", func(t *testing.T) {
cmd := &cli.Command{Use: "test"}

assert.Panics(t, func() { mustMarkRequired(cmd, "no-such-flag") })
})
}
12 changes: 6 additions & 6 deletions cmd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func createGroupCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the group body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -140,9 +140,9 @@ func editGroupCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the group body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func viewGroupCommand(cliConfig *Config) *cli.Command {

cmd.Flags().BoolVarP(&metadata, "metadata", "m", false, "Set this flag to see metadata")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func listGroupCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
4 changes: 2 additions & 2 deletions cmd/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func viewNamespaceCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func listNamespaceCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
14 changes: 7 additions & 7 deletions cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func createOrganizationCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the organization body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -141,9 +141,9 @@ func editOrganizationCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the organization body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func viewOrganizationCommand(cliConfig *Config) *cli.Command {

cmd.Flags().BoolVarP(&metadata, "metadata", "m", false, "Set this flag to see metadata")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func listOrganizationCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -340,7 +340,7 @@ func admlistOrganizationCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
12 changes: 6 additions & 6 deletions cmd/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func createPermissionCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the permission body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -140,9 +140,9 @@ func editPermissionCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the permission body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func viewPermissionCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func listPermissionCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
10 changes: 5 additions & 5 deletions cmd/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func createPolicyCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the policy body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -138,9 +138,9 @@ func editPolicyCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the policy body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func viewPolicyCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
6 changes: 3 additions & 3 deletions cmd/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func preferencesListCommand(cliConfig *Config) *cobra.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func preferencesSetCommand(cliConfig *Config) *cobra.Command {
cmd.Flags().StringVarP(&name, "name", "n", "", "Name of the preference")
cmd.Flags().StringVarP(&value, "value", "v", "", "Value of the preference")

cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")
return cmd
}

Expand Down Expand Up @@ -182,7 +182,7 @@ func preferencesGetCommand(cliConfig *Config) *cobra.Command {
},
}
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func createProjectCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the project body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -140,9 +140,9 @@ func editProjectCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the project body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func viewProjectCommand(cliConfig *Config) *cli.Command {

cmd.Flags().BoolVarP(&metadata, "metadata", "m", false, "Set this flag to see metadata")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func listProjectCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
2 changes: 1 addition & 1 deletion cmd/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func ReconcileCommand(cliConfig *Config) *cli.Command {
},
}
cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the desired-state YAML file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the plan without applying changes")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value> for auth, e.g. 'Authorization:Basic <base64>'")
bindFlagsFromClientConfig(cmd)
Expand Down
12 changes: 6 additions & 6 deletions cmd/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func createRoleCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the role body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -141,9 +141,9 @@ func editRoleCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the role body file")
cmd.MarkFlagRequired("file")
mustMarkRequired(cmd, "file")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func viewRoleCommand(cliConfig *Config) *cli.Command {

cmd.Flags().BoolVarP(&metadata, "metadata", "m", false, "Set this flag to see metadata")
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func listRoleCommand(cliConfig *Config) *cli.Command {
}

cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")

return cmd
}
2 changes: 1 addition & 1 deletion cmd/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func SeedCommand(cliConfig *Config) *cli.Command {

bindFlagsFromClientConfig(cmd)
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>")
cmd.MarkFlagRequired("header")
mustMarkRequired(cmd, "header")
cmd.Flags().StringVarP(&configFile, "config", "c", "", "config file path")
return cmd
}
Expand Down
Loading
Loading