Skip to content

Commit

Permalink
Adds ability to specify files to be applied positionally, as well as … (
Browse files Browse the repository at this point in the history
#5388)

Adds ability to specify files to be applied positionally, as well as via flag. Fixes: #5383
  • Loading branch information
mesembria authored Jan 31, 2025
1 parent 07e670c commit 3fd28e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
19 changes: 11 additions & 8 deletions cmd/cli/app/datasource/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@ import (

// applyCmd represents the datasource apply command
var applyCmd = &cobra.Command{
Use: "apply",
Use: "apply [files...]",
Short: "Apply a data source",
Long: `The datasource apply subcommand lets you create or update data sources for a project within Minder.`,
RunE: cli.GRPCClientWrapRunE(applyCommand),
Args: cobra.ArbitraryArgs,
}

func init() {
DataSourceCmd.AddCommand(applyCmd)
// Flags
applyCmd.Flags().StringArrayP("file", "f", []string{},
"Path to the YAML defining the data source (or - for stdin). Can be specified multiple times. Can be a directory.")
// Required
if err := applyCmd.MarkFlagRequired("file"); err != nil {
applyCmd.Printf("Error marking flag required: %s", err)
os.Exit(1)
}
}

// applyCommand is the datasource apply subcommand
func applyCommand(_ context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
func applyCommand(_ context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error {
client := minderv1.NewDataSourceServiceClient(conn)

project := viper.GetString("project")
Expand All @@ -51,11 +47,18 @@ func applyCommand(_ context.Context, cmd *cobra.Command, _ []string, conn *grpc.
return cli.MessageAndError("Error parsing file flag", err)
}

// Combine positional args with -f flag values
allFiles := append(fileFlag, args...)

if len(allFiles) == 0 {
return fmt.Errorf("no files specified: use positional arguments or the -f flag")
}

if err = validateFilesArg(fileFlag); err != nil {
return cli.MessageAndError("Error validating file flag", err)
}

files, err := util.ExpandFileArgs(fileFlag...)
files, err := util.ExpandFileArgs(allFiles...)
if err != nil {
return cli.MessageAndError("Error expanding file args", err)
}
Expand Down
24 changes: 15 additions & 9 deletions cmd/cli/app/profile/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package profile
import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -20,18 +19,30 @@ import (

// applyCmd represents the profile apply command
var applyCmd = &cobra.Command{
Use: "apply",
Use: "apply [file]",
Short: "Create or update a profile",
Long: `The profile apply subcommand lets you create or update new profiles for a project within Minder.`,
Args: cobra.MaximumNArgs(1),
RunE: cli.GRPCClientWrapRunE(applyCommand),
}

// applyCommand is the profile apply subcommand
func applyCommand(_ context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
func applyCommand(_ context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

project := viper.GetString("project")
f := viper.GetString("file")

// Get file from positional arg if provided, otherwise from -f flag
var f string
if len(args) > 0 {
f = args[0]
} else {
f = viper.GetString("file")
}

if f == "" {
return fmt.Errorf("file is required - provide as argument or via --file flag")
}

// No longer print usage on returned error, since we've parsed our inputs
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413
Expand Down Expand Up @@ -92,9 +103,4 @@ func init() {
ProfileCmd.AddCommand(applyCmd)
// Flags
applyCmd.Flags().StringP("file", "f", "", "Path to the YAML defining the profile (or - for stdin)")
// Required
if err := applyCmd.MarkFlagRequired("file"); err != nil {
applyCmd.Printf("Error marking flag required: %s", err)
os.Exit(1)
}
}
22 changes: 13 additions & 9 deletions cmd/cli/app/ruletype/ruletype_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
)

var applyCmd = &cobra.Command{
Use: "apply",
Use: "apply [files...]",
Short: "Apply a rule type",
Long: `The ruletype apply subcommand lets you create or update rule types for a project within Minder.`,
RunE: cli.GRPCClientWrapRunE(applyCommand),
Args: cobra.ArbitraryArgs,
}

// applyCommand is the "rule type" apply subcommand
func applyCommand(_ context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
func applyCommand(_ context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error {
client := minderv1.NewRuleTypeServiceClient(conn)

project := viper.GetString("project")
Expand All @@ -37,11 +38,18 @@ func applyCommand(_ context.Context, cmd *cobra.Command, _ []string, conn *grpc.
return cli.MessageAndError("Error parsing file flag", err)
}

// Combine positional args with -f flag values
allFiles := append(fileFlag, args...)

if len(allFiles) == 0 {
return fmt.Errorf("no files specified: use positional arguments or the -f flag")
}

if err = validateFilesArg(fileFlag); err != nil {
return cli.MessageAndError("Error validating file flag", err)
return cli.MessageAndError("Error validating files", err)
}

files, err := util.ExpandFileArgs(fileFlag...)
files, err := util.ExpandFileArgs(allFiles...)
if err != nil {
return cli.MessageAndError("Error expanding file args", err)
}
Expand Down Expand Up @@ -107,9 +115,5 @@ func init() {
// Flags
applyCmd.Flags().StringArrayP("file", "f", []string{},
"Path to the YAML defining the rule type (or - for stdin). Can be specified multiple times. Can be a directory.")
// Required
if err := applyCmd.MarkFlagRequired("file"); err != nil {
applyCmd.Printf("Error marking flag required: %s", err)
os.Exit(1)
}

}

0 comments on commit 3fd28e1

Please sign in to comment.