From 811a9e28534d9f6d68b4d444fb3441439d5f4f6f Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Wed, 27 Jan 2021 02:29:01 -0300 Subject: [PATCH] Add --no-input flag --- internal/cli/apis.go | 2 +- internal/cli/cli.go | 15 +++++++++++---- internal/cli/root.go | 3 +++ internal/cli/rules.go | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/cli/apis.go b/internal/cli/apis.go index 9c33009b5..c190051e5 100644 --- a/internal/cli/apis.go +++ b/internal/cli/apis.go @@ -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 } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 933439f7b..05aec894b 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -60,6 +60,7 @@ type cli struct { tenant string format string force bool + noInput bool // config state management. initOnce sync.Once @@ -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"}) }) diff --git a/internal/cli/root.go b/internal/cli/root.go index 72b5b4245..2936f141c 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -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)) diff --git a/internal/cli/rules.go b/internal/cli/rules.go index 32d5665d8..f7fc98b0b 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -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 }