Skip to content

Commit

Permalink
Add --no-input flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Jan 27, 2021
1 parent c205e83 commit 811a9e2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ auth0 apis delete --id id
}
}

if !cli.force && canPrompt() {
if !cli.force && canPrompt(cmd) {
if confirmed := prompt.Confirm("Are you sure you want to proceed?"); !confirmed {
return nil
}
Expand Down
15 changes: 11 additions & 4 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type cli struct {
tenant string
format string
force bool
noInput bool

// config state management.
initOnce sync.Once
Expand Down Expand Up @@ -231,16 +232,22 @@ func mustRequireFlags(cmd *cobra.Command, flags ...string) {
}
}

func canPrompt() bool {
return ansi.IsTerminal()
func canPrompt(cmd *cobra.Command) bool {
noInput, err := cmd.Root().Flags().GetBool("no-input")

if err != nil {
return false
}

return ansi.IsTerminal() && !noInput
}

func shouldPrompt(cmd *cobra.Command, flag string) bool {
return canPrompt() && !cmd.Flags().Changed(flag)
return canPrompt(cmd) && !cmd.Flags().Changed(flag)
}

func prepareInteractivity(cmd *cobra.Command) {
if canPrompt() {
if canPrompt(cmd) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
cmd.Flags().SetAnnotation(flag.Name, cobra.BashCompOneRequiredFlag, []string{"false"})
})
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func Execute() {

rootCmd.PersistentFlags().BoolVar(&cli.force,
"force", false, "Skip confirmation.")

rootCmd.PersistentFlags().BoolVar(&cli.noInput,
"no-input", false, "Disable interactivity.")

rootCmd.AddCommand(loginCmd(cli))
rootCmd.AddCommand(clientsCmd(cli))
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func deleteRulesCmd(cli *cli) *cobra.Command {
}
}

if !cli.force && canPrompt() {
if !cli.force && canPrompt(cmd) {
if confirmed := prompt.Confirm("Are you sure you want to proceed?"); !confirmed {
return nil
}
Expand Down

0 comments on commit 811a9e2

Please sign in to comment.