diff --git a/cmd/destroy.go b/cmd/destroy.go index ec3d15849..bc741031f 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -40,7 +40,6 @@ func newDestroyCmd() *cobra.Command { var showConfig bool var showReplacementSteps bool var showSames bool - var nonInteractive bool var skipPreview bool var yes bool @@ -58,7 +57,7 @@ func newDestroyCmd() *cobra.Command { "is generally irreversible and should be used with great care.", Args: cmdutil.NoArgs, Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error { - interactive := isInteractive(nonInteractive) + interactive := isInteractive() if !interactive { yes = true // auto-approve changes, since we cannot prompt. } @@ -130,8 +129,6 @@ func newDestroyCmd() *cobra.Command { cmd.PersistentFlags().BoolVar( &diffDisplay, "diff", false, "Display operation as a rich diff showing the overall change") - cmd.PersistentFlags().BoolVar( - &nonInteractive, "non-interactive", false, "Disable interactive mode") cmd.PersistentFlags().IntVarP( ¶llel, "parallel", "p", defaultParallel, "Allow P resource operations to run in parallel at once (<=1 for no parallelism)") diff --git a/cmd/new.go b/cmd/new.go index 1a8152cfa..c5b5396cb 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -57,7 +57,6 @@ func newNewCmd() *cobra.Command { var offline bool var generateOnly bool var dir string - var nonInteractive bool cmd := &cobra.Command{ Use: "new [template]", @@ -65,7 +64,7 @@ func newNewCmd() *cobra.Command { Short: "Create a new Pulumi project", Args: cmdutil.MaximumNArgs(1), Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error { - interactive := isInteractive(nonInteractive) + interactive := isInteractive() if !interactive { yes = true // auto-approve changes, since we cannot prompt. } @@ -335,8 +334,6 @@ func newNewCmd() *cobra.Command { cmd.PersistentFlags().StringVar( &dir, "dir", "", "The location to place the generated project; if not specified, the current directory is used") - cmd.PersistentFlags().BoolVar( - &nonInteractive, "non-interactive", false, "Disable interactive mode") return cmd } diff --git a/cmd/preview.go b/cmd/preview.go index 965580f54..86c5a8d85 100644 --- a/cmd/preview.go +++ b/cmd/preview.go @@ -33,7 +33,6 @@ func newPreviewCmd() *cobra.Command { // Flags for engine.UpdateOptions. var analyzers []string var diffDisplay bool - var nonInteractive bool var parallel int var showConfig bool var showReplacementSteps bool @@ -68,7 +67,7 @@ func newPreviewCmd() *cobra.Command { ShowConfig: showConfig, ShowReplacementSteps: showReplacementSteps, ShowSameResources: showSames, - IsInteractive: isInteractive(nonInteractive), + IsInteractive: isInteractive(), DiffDisplay: diffDisplay, Debug: debug, }, @@ -128,8 +127,6 @@ func newPreviewCmd() *cobra.Command { cmd.PersistentFlags().BoolVar( &diffDisplay, "diff", false, "Display operation as a rich diff showing the overall change") - cmd.PersistentFlags().BoolVar( - &nonInteractive, "non-interactive", false, "Disable interactive mode") cmd.PersistentFlags().IntVarP( ¶llel, "parallel", "p", defaultParallel, "Allow P resource operations to run in parallel at once (<=1 for no parallelism)") diff --git a/cmd/pulumi.go b/cmd/pulumi.go index ae86cbea5..9ed92c589 100644 --- a/cmd/pulumi.go +++ b/cmd/pulumi.go @@ -134,6 +134,8 @@ func NewPulumiCmd() *cobra.Command { "Flow log settings to child processes (like plugins)") cmd.PersistentFlags().BoolVar(&logToStderr, "logtostderr", false, "Log to stderr instead of to files") + cmd.PersistentFlags().BoolVar(&nonInteractive, "non-interactive", false, + "Disable interactive mode for all commands") cmd.PersistentFlags().StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint") cmd.PersistentFlags().StringVar(&profiling, "profiling", "", diff --git a/cmd/refresh.go b/cmd/refresh.go index 39569b146..06cb01154 100644 --- a/cmd/refresh.go +++ b/cmd/refresh.go @@ -39,7 +39,6 @@ func newRefreshCmd() *cobra.Command { var showConfig bool var showReplacementSteps bool var showSames bool - var nonInteractive bool var skipPreview bool var yes bool @@ -57,7 +56,7 @@ func newRefreshCmd() *cobra.Command { "`--cwd` flag to use a different directory.", Args: cmdutil.NoArgs, Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error { - interactive := isInteractive(nonInteractive) + interactive := isInteractive() if !interactive { yes = true // auto-approve changes, since we cannot prompt. } @@ -139,8 +138,6 @@ func newRefreshCmd() *cobra.Command { cmd.PersistentFlags().BoolVar( &diffDisplay, "diff", false, "Display operation as a rich diff showing the overall change") - cmd.PersistentFlags().BoolVar( - &nonInteractive, "non-interactive", false, "Disable interactive mode") cmd.PersistentFlags().IntVarP( ¶llel, "parallel", "p", defaultParallel, "Allow P resource operations to run in parallel at once (<=1 for no parallelism)") diff --git a/cmd/up.go b/cmd/up.go index 884596062..0d97eef58 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -50,7 +50,6 @@ func newUpCmd() *cobra.Command { // Flags for engine.UpdateOptions. var analyzers []string var diffDisplay bool - var nonInteractive bool var parallel int var refresh bool var showConfig bool @@ -273,7 +272,7 @@ func newUpCmd() *cobra.Command { "`--cwd` flag to use a different directory.", Args: cmdutil.MaximumNArgs(1), Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error { - interactive := isInteractive(nonInteractive) + interactive := isInteractive() if !interactive { yes = true // auto-approve changes, since we cannot prompt. } @@ -325,8 +324,6 @@ func newUpCmd() *cobra.Command { cmd.PersistentFlags().BoolVar( &diffDisplay, "diff", false, "Display operation as a rich diff showing the overall change") - cmd.PersistentFlags().BoolVar( - &nonInteractive, "non-interactive", false, "Disable interactive mode") cmd.PersistentFlags().IntVarP( ¶llel, "parallel", "p", defaultParallel, "Allow P resource operations to run in parallel at once (<=1 for no parallelism)") diff --git a/cmd/util.go b/cmd/util.go index 04f8603ed..119131cea 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -578,9 +578,13 @@ func printJSON(v interface{}) error { return nil } -// isInteractive returns true if the environment and command line options indicate we should -// do things interactively -func isInteractive(nonInteractive bool) bool { +// nonInteractive may be set to true in order to disable prompts. This is useful when running in a non-attended +// scenario, such as in continuous integration, or when using the Pulumi CLI/SDK in a programmatic way. +var nonInteractive bool + +// isInteractive returns true if the environment and command line options indicate we should do things interactively. +// It respects the nonInteractive flag as an override, but otherwise defaults by reading the terminal and CI settings. +func isInteractive() bool { return !nonInteractive && terminal.IsTerminal(int(os.Stdout.Fd())) && !testutil.IsCI() }