Skip to content

Commit c0885da

Browse files
authored
Merge pull request #582 from open-policy-agent/show-usage-when-no-pos-params
Show usage when required parameters are not supplied
2 parents d865902 + 1bcf5ac commit c0885da

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

internal/commands/plugin_install.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ func NewPluginInstallCommand(ctx context.Context) *cobra.Command {
4040
Use: "install <path|url>",
4141
Short: "Install a plugin from the given path or url",
4242
Long: installDesc,
43-
Args: cobra.MinimumNArgs(1),
44-
4543
RunE: func(cmd *cobra.Command, args []string) error {
44+
if len(args) < 1 {
45+
cmd.Usage() //nolint
46+
return fmt.Errorf("missing required arguments")
47+
}
48+
4649
if err := plugin.Install(ctx, args[0]); err != nil {
4750
return fmt.Errorf("install: %v", err)
4851
}

internal/commands/pull.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ func NewPullCommand(ctx context.Context) *cobra.Command {
5151
Use: "pull <repository>",
5252
Short: "Download individual policies",
5353
Long: pullDesc,
54-
Args: cobra.MinimumNArgs(1),
55-
5654
PreRunE: func(cmd *cobra.Command, args []string) error {
5755
if err := viper.BindPFlag("policy", cmd.Flags().Lookup("policy")); err != nil {
5856
return fmt.Errorf("bind flag: %w", err)
@@ -61,6 +59,11 @@ func NewPullCommand(ctx context.Context) *cobra.Command {
6159
return nil
6260
},
6361
RunE: func(cmd *cobra.Command, args []string) error {
62+
if len(args) < 1 {
63+
cmd.Usage() //nolint
64+
return fmt.Errorf("missing required arguments")
65+
}
66+
6467
ctx = orascontext.Background()
6568

6669
policyDir := filepath.Join(".", viper.GetString("policy"))

internal/commands/push.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func NewPushCommand(ctx context.Context, logger *log.Logger) *cobra.Command {
5757
Use: "push <repository>",
5858
Short: "Push OPA bundles to an OCI registry",
5959
Long: pushDesc,
60-
Args: cobra.ExactArgs(1),
6160
PreRunE: func(cmd *cobra.Command, args []string) error {
6261
if err := viper.BindPFlag("policy", cmd.Flags().Lookup("policy")); err != nil {
6362
return fmt.Errorf("bind flag: %w", err)
@@ -67,6 +66,11 @@ func NewPushCommand(ctx context.Context, logger *log.Logger) *cobra.Command {
6766
},
6867

6968
RunE: func(cmd *cobra.Command, args []string) error {
69+
if len(args) < 1 {
70+
cmd.Usage() //nolint
71+
return fmt.Errorf("missing required arguments")
72+
}
73+
7074
ctx = orascontext.Background()
7175

7276
repository := args[0]

internal/commands/test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
8282
Use: "test <path> [path [...]]",
8383
Short: "Test your configuration files using Open Policy Agent",
8484
Long: testDesc,
85-
Args: cobra.MinimumNArgs(1),
8685
PreRunE: func(cmd *cobra.Command, args []string) error {
8786
flagNames := []string{"all-namespaces", "combine", "data", "fail-on-warn", "ignore", "namespace", "no-color", "no-fail", "suppress-exceptions", "output", "parser", "policy", "trace", "update"}
8887
for _, name := range flagNames {
@@ -95,6 +94,11 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
9594
},
9695

9796
RunE: func(cmd *cobra.Command, fileList []string) error {
97+
if len(fileList) < 1 {
98+
cmd.Usage() //nolint
99+
return fmt.Errorf("missing required arguments")
100+
}
101+
98102
var runner runner.TestRunner
99103
if err := viper.Unmarshal(&runner); err != nil {
100104
return fmt.Errorf("unmarshal parameters: %w", err)

0 commit comments

Comments
 (0)