From bf396b5f1b89536725a3be35d7500a17cb1045b3 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 7 May 2021 15:53:47 -0700 Subject: [PATCH] command/views: main View type aware if it's running in automation This "running in automation" idea is a best effort thing where we try to avoid printing out specific suggestions of commands to run in the main workflow when the user is running Terraform inside a wrapper script or other automation, because they probably don't want to bypass that automation. This just makes that information available to the main views.View type, so we can then make use of it in the implementation of more specialized view types that embed views.View. However, nothing is using it as of this commit. We'll use it in later commits. --- command/views/view.go | 19 +++++++++++++++++++ commands.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/command/views/view.go b/command/views/view.go index c7ee1c7202..eb8a3bd796 100644 --- a/command/views/view.go +++ b/command/views/view.go @@ -17,6 +17,13 @@ type View struct { compactWarnings bool + // When this is true it's a hint that Terraform is being run indirectly + // via a wrapper script or other automation and so we may wish to replace + // direct examples of commands to run with more conceptual directions. + // However, we only do this on a best-effort basis, typically prioritizing + // the messages that users are most likely to see. + runningInAutomation bool + // This unfortunate wart is required to enable rendering of diagnostics which // have associated source code in the configuration. This function pointer // will be dereferenced as late as possible when rendering diagnostics in @@ -38,6 +45,18 @@ func NewView(streams *terminal.Streams) *View { } } +// SetRunningInAutomation modifies the view's "running in automation" flag, +// which causes some slight adjustments to certain messages that would normally +// suggest specific Terraform commands to run, to make more conceptual gestures +// instead for situations where the user isn't running Terraform directly. +// +// For convenient use during initialization (in conjunction with NewView), +// SetRunningInAutomation returns the reciever after modifying it. +func (v *View) SetRunningInAutomation(new bool) *View { + v.runningInAutomation = new + return v +} + // Configure applies the global view configuration flags. func (v *View) Configure(view *arguments.View) { v.colorize.Disable = view.NoColor diff --git a/commands.go b/commands.go index e0f6f5797d..dcb8d1c1a7 100644 --- a/commands.go +++ b/commands.go @@ -82,7 +82,7 @@ func initCommands( meta := command.Meta{ OriginalWorkingDir: originalWorkingDir, Streams: streams, - View: views.NewView(streams), + View: views.NewView(streams).SetRunningInAutomation(inAutomation), Color: true, GlobalPluginDirs: globalPluginDirs(),