Files
opentf/command/views/refresh.go
Martin Atkins b402fd9d3a command/views: Remove command-specific runningInAutomation
We now have RunningInAutomation has a general concern in views.View, so
we no longer need to specify it for each command-specific constructor
separately.

For this initial change I focused only on changing the exported interface
of the views package and let the command-specific views go on having their
own unexported fields containing a copy of the flag because it made this
change less invasive and I wasn't feeling sure yet about whether we
ought to have code within command-specific views directly access the
internals of views.View. However, maybe we'll simplify this further in
a later commit if we conclude that these copies of the flag are
burdensome.

The general version of this gets set directly inside the main package,
which might at some future point allow us to make the command package
itself unaware of this "running in automation" idea and thus reinforce
that it's intended as a presentation-only thing rather than as a
behavioral thing, but we'll save more invasive refactoring for another
day.
2021-05-10 10:50:05 -07:00

113 lines
2.5 KiB
Go

package views
import (
"fmt"
"github.com/hashicorp/terraform/command/arguments"
"github.com/hashicorp/terraform/command/views/json"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags"
)
// The Refresh view is used for the refresh command.
type Refresh interface {
Outputs(outputValues map[string]*states.OutputValue)
Operation() Operation
Hooks() []terraform.Hook
Diagnostics(diags tfdiags.Diagnostics)
HelpPrompt()
}
// NewRefresh returns an initialized Refresh implementation for the given ViewType.
func NewRefresh(vt arguments.ViewType, view *View) Refresh {
switch vt {
case arguments.ViewJSON:
return &RefreshJSON{
view: NewJSONView(view),
}
case arguments.ViewHuman:
return &RefreshHuman{
view: view,
inAutomation: view.RunningInAutomation(),
countHook: &countHook{},
}
default:
panic(fmt.Sprintf("unknown view type %v", vt))
}
}
// The RefreshHuman implementation renders human-readable text logs, suitable for
// a scrolling terminal.
type RefreshHuman struct {
view *View
inAutomation bool
countHook *countHook
}
var _ Refresh = (*RefreshHuman)(nil)
func (v *RefreshHuman) Outputs(outputValues map[string]*states.OutputValue) {
if len(outputValues) > 0 {
v.view.streams.Print(v.view.colorize.Color("[reset][bold][green]\nOutputs:\n\n"))
NewOutput(arguments.ViewHuman, v.view).Output("", outputValues)
}
}
func (v *RefreshHuman) Operation() Operation {
return NewOperation(arguments.ViewHuman, v.inAutomation, v.view)
}
func (v *RefreshHuman) Hooks() []terraform.Hook {
return []terraform.Hook{
v.countHook,
NewUiHook(v.view),
}
}
func (v *RefreshHuman) Diagnostics(diags tfdiags.Diagnostics) {
v.view.Diagnostics(diags)
}
func (v *RefreshHuman) HelpPrompt() {
v.view.HelpPrompt("refresh")
}
// The RefreshJSON implementation renders streaming JSON logs, suitable for
// integrating with other software.
type RefreshJSON struct {
view *JSONView
}
var _ Refresh = (*RefreshJSON)(nil)
func (v *RefreshJSON) Outputs(outputValues map[string]*states.OutputValue) {
outputs, diags := json.OutputsFromMap(outputValues)
if diags.HasErrors() {
v.Diagnostics(diags)
} else {
v.view.Outputs(outputs)
}
}
func (v *RefreshJSON) Operation() Operation {
return &OperationJSON{view: v.view}
}
func (v *RefreshJSON) Hooks() []terraform.Hook {
return []terraform.Hook{
newJSONHook(v.view),
}
}
func (v *RefreshJSON) Diagnostics(diags tfdiags.Diagnostics) {
v.view.Diagnostics(diags)
}
func (v *RefreshJSON) HelpPrompt() {
}