backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-05-17 21:07:38 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
2022-01-11 00:16:12 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/command/format"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonplan"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonstate"
|
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2022-01-11 00:16:12 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/states/statefile"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2022-01-11 00:16:12 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Show interface {
|
2022-01-11 00:16:12 +01:00
|
|
|
// Display renders the plan, if it is available. If plan is nil, it renders the statefile.
|
|
|
|
Display(config *configs.Config, plan *plans.Plan, stateFile *statefile.File, schemas *terraform.Schemas) int
|
|
|
|
|
|
|
|
// Diagnostics renders early diagnostics, resulting from argument parsing.
|
|
|
|
Diagnostics(diags tfdiags.Diagnostics)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewShow(vt arguments.ViewType, view *View) Show {
|
|
|
|
switch vt {
|
2022-01-11 00:16:12 +01:00
|
|
|
case arguments.ViewJSON:
|
|
|
|
return &ShowJSON{view: view}
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
case arguments.ViewHuman:
|
2022-01-11 00:16:12 +01:00
|
|
|
return &ShowHuman{view: view}
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown view type %v", vt))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShowHuman struct {
|
2022-01-11 00:16:12 +01:00
|
|
|
view *View
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Show = (*ShowHuman)(nil)
|
|
|
|
|
2022-01-11 00:16:12 +01:00
|
|
|
func (v *ShowHuman) Display(config *configs.Config, plan *plans.Plan, stateFile *statefile.File, schemas *terraform.Schemas) int {
|
|
|
|
if plan != nil {
|
|
|
|
renderPlan(plan, schemas, v.view)
|
|
|
|
} else {
|
|
|
|
if stateFile == nil {
|
|
|
|
v.view.streams.Println("No state.")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
v.view.streams.Println(format.State(&format.StateOpts{
|
|
|
|
State: stateFile.State,
|
|
|
|
Color: v.view.colorize,
|
|
|
|
Schemas: schemas,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ShowHuman) Diagnostics(diags tfdiags.Diagnostics) {
|
|
|
|
v.view.Diagnostics(diags)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShowJSON struct {
|
|
|
|
view *View
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Show = (*ShowJSON)(nil)
|
|
|
|
|
|
|
|
func (v *ShowJSON) Display(config *configs.Config, plan *plans.Plan, stateFile *statefile.File, schemas *terraform.Schemas) int {
|
|
|
|
if plan != nil {
|
|
|
|
jsonPlan, err := jsonplan.Marshal(config, plan, stateFile, schemas)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
v.view.streams.Eprintf("Failed to marshal plan to json: %s", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
v.view.streams.Println(string(jsonPlan))
|
|
|
|
} else {
|
|
|
|
// It is possible that there is neither state nor a plan.
|
|
|
|
// That's ok, we'll just return an empty object.
|
|
|
|
jsonState, err := jsonstate.Marshal(stateFile, schemas)
|
|
|
|
if err != nil {
|
|
|
|
v.view.streams.Eprintf("Failed to marshal state to json: %s", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
v.view.streams.Println(string(jsonState))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagnostics should only be called if show cannot be executed.
|
|
|
|
// In this case, we choose to render human-readable diagnostic output,
|
|
|
|
// primarily for backwards compatibility.
|
|
|
|
func (v *ShowJSON) Diagnostics(diags tfdiags.Diagnostics) {
|
|
|
|
v.view.Diagnostics(diags)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
}
|