Skip to content

Commit

Permalink
Add a yes flag to skip prompts for CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hsachdev-px committed Nov 26, 2023
1 parent 58ae022 commit 60ed87f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
5 changes: 4 additions & 1 deletion cmd/cli/app/auth/auth_revoke_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var Auth_revokeproviderCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// check if we need to revoke all tokens or the user one
all := util.GetConfigValue(viper.GetViper(), "all", "all", cmd, false).(bool)
yesFlag := util.GetConfigValue(viper.GetViper(), "yes", "yes", cmd, false).(bool)
project := viper.GetString("project-id")
provider := util.GetConfigValue(viper.GetViper(), "provider", "provider", cmd, "").(string)

Expand All @@ -54,7 +55,7 @@ var Auth_revokeproviderCmd = &cobra.Command{
os.Exit(1)
}

if all {
if all && !yesFlag {
yes := cli.PrintYesNoPrompt(cmd,
"You are about to revoke the access tokens for your provider.",
"Are you sure?",
Expand All @@ -63,6 +64,7 @@ var Auth_revokeproviderCmd = &cobra.Command{
return
}
}

conn, err := util.GrpcForCommand(cmd, viper.GetViper())
util.ExitNicelyOnError(err, "Error getting grpc connection")
defer conn.Close()
Expand Down Expand Up @@ -91,4 +93,5 @@ func init() {
Auth_revokeproviderCmd.Flags().StringP("provider", "p", "", "Name for the provider to revoke tokens for")
Auth_revokeproviderCmd.Flags().StringP("project-id", "g", "", "ID of the project for repo registration")
Auth_revokeproviderCmd.Flags().BoolP("all", "a", false, "Revoke all tokens")
Auth_revokeproviderCmd.Flags().BoolP("yes", "y", false, "Bypass yes/no prompt when revoking all tokens")
}
17 changes: 11 additions & 6 deletions cmd/cli/app/provider/provider_enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,22 @@ actions such as adding repositories.`,
project := viper.GetString("project-id")
pat := util.GetConfigValue(viper.GetViper(), "token", "token", cmd, "").(string)
owner := util.GetConfigValue(viper.GetViper(), "owner", "owner", cmd, "").(string)
yesFlag := util.GetConfigValue(viper.GetViper(), "yes", "yes", cmd, false).(bool)

// Ask for confirmation if an owner is set on purpose
ownerPromptStr := "your personal account"
if owner != "" {
ownerPromptStr = fmt.Sprintf("the %s organisation", owner)
}
yes := cli.PrintYesNoPrompt(cmd,
fmt.Sprintf("You are about to enroll repositories from %s.", ownerPromptStr),
"Do you confirm?",
"Enroll operation cancelled.")
if !yes {
return

if !yesFlag {
yes := cli.PrintYesNoPrompt(cmd,
fmt.Sprintf("You are about to enroll repositories from %s.", ownerPromptStr),
"Do you confirm?",
"Enroll operation cancelled.")
if !yes {
return
}
}

conn, err := util.GrpcForCommand(cmd, viper.GetViper())
Expand Down Expand Up @@ -202,6 +206,7 @@ func init() {
enrollProviderCmd.Flags().StringP("project-id", "g", "", "ID of the project for enrolling the provider")
enrollProviderCmd.Flags().StringP("token", "t", "", "Personal Access Token (PAT) to use for enrollment")
enrollProviderCmd.Flags().StringP("owner", "o", "", "Owner to filter on for provider resources")
enrollProviderCmd.Flags().BoolP("yes", "y", false, "Bypass yes/no prompt when enrolling new provider")
if err := enrollProviderCmd.MarkFlagRequired("provider"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/cli/app/rule_type/rule_type_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ minder control plane.`,
// Delete the rule type via GRPC
id := viper.GetString("id")
deleteAll := viper.GetBool("all")
yesFlag := util.GetConfigValue(viper.GetViper(), "yes", "yes", cmd, false).(bool)

// If id is set, deleteAll cannot be set
if id != "" && deleteAll {
Expand All @@ -53,7 +54,8 @@ minder control plane.`,
fmt.Fprintf(os.Stderr, "Either id or deleteAll needs to be set")
return
}
if deleteAll {

if deleteAll && !yesFlag {
// Ask for confirmation if deleteAll is set on purpose
yes := cli.PrintYesNoPrompt(cmd,
"You are about to permanently delete all of your rule types.",
Expand Down Expand Up @@ -145,6 +147,7 @@ func init() {
ruleType_deleteCmd.Flags().StringP("provider", "p", "", "Provider to list rule types for")
ruleType_deleteCmd.Flags().StringP("id", "i", "", "ID of rule type to delete")
ruleType_deleteCmd.Flags().BoolP("all", "a", false, "Warning: Deletes all rule types")
ruleType_deleteCmd.Flags().BoolP("yes", "y", false, "Bypass yes/no prompt when deleting all rule types")
err := ruleType_deleteCmd.MarkFlagRequired("provider")
util.ExitNicelyOnError(err, "Error marking flag as required")
}

0 comments on commit 60ed87f

Please sign in to comment.