2021-02-18 23:23:34 +01:00
package views
import (
"fmt"
"github.com/hashicorp/terraform/command/arguments"
"github.com/hashicorp/terraform/command/format"
2021-02-23 16:16:09 +01:00
"github.com/hashicorp/terraform/command/views/json"
2021-02-18 23:23:34 +01:00
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags"
)
// The Apply view is used for the apply command.
type Apply interface {
ResourceCount ( stateOutPath string )
Outputs ( outputValues map [ string ] * states . OutputValue )
Operation ( ) Operation
Hooks ( ) [ ] terraform . Hook
Diagnostics ( diags tfdiags . Diagnostics )
HelpPrompt ( )
}
// NewApply returns an initialized Apply implementation for the given ViewType.
func NewApply ( vt arguments . ViewType , destroy bool , runningInAutomation bool , view * View ) Apply {
switch vt {
2021-02-23 16:16:09 +01:00
case arguments . ViewJSON :
return & ApplyJSON {
view : NewJSONView ( view ) ,
destroy : destroy ,
countHook : & countHook { } ,
}
2021-02-18 23:23:34 +01:00
case arguments . ViewHuman :
return & ApplyHuman {
2021-02-26 22:31:12 +01:00
view : view ,
2021-02-18 23:23:34 +01:00
destroy : destroy ,
inAutomation : runningInAutomation ,
countHook : & countHook { } ,
}
default :
panic ( fmt . Sprintf ( "unknown view type %v" , vt ) )
}
}
// The ApplyHuman implementation renders human-readable text logs, suitable for
// a scrolling terminal.
type ApplyHuman struct {
2021-02-26 22:31:12 +01:00
view * View
2021-02-18 23:23:34 +01:00
destroy bool
inAutomation bool
countHook * countHook
}
var _ Apply = ( * ApplyHuman ) ( nil )
func ( v * ApplyHuman ) ResourceCount ( stateOutPath string ) {
if v . destroy {
2021-02-26 22:31:12 +01:00
v . view . streams . Printf (
v . view . colorize . Color ( "[reset][bold][green]\nDestroy complete! Resources: %d destroyed.\n" ) ,
2021-02-18 23:23:34 +01:00
v . countHook . Removed ,
)
} else {
2021-02-26 22:31:12 +01:00
v . view . streams . Printf (
v . view . colorize . Color ( "[reset][bold][green]\nApply complete! Resources: %d added, %d changed, %d destroyed.\n" ) ,
2021-02-18 23:23:34 +01:00
v . countHook . Added ,
v . countHook . Changed ,
v . countHook . Removed ,
)
}
if ( v . countHook . Added > 0 || v . countHook . Changed > 0 ) && stateOutPath != "" {
2021-02-26 22:31:12 +01:00
v . view . streams . Printf ( "\n%s\n\n" , format . WordWrap ( stateOutPathPostApply , v . view . outputColumns ( ) ) )
v . view . streams . Printf ( "State path: %s\n" , stateOutPath )
2021-02-18 23:23:34 +01:00
}
}
func ( v * ApplyHuman ) Outputs ( outputValues map [ string ] * states . OutputValue ) {
if len ( outputValues ) > 0 {
2021-02-26 22:31:12 +01:00
v . view . streams . Print ( v . view . colorize . Color ( "[reset][bold][green]\nOutputs:\n\n" ) )
NewOutput ( arguments . ViewHuman , v . view ) . Output ( "" , outputValues )
2021-02-18 23:23:34 +01:00
}
}
func ( v * ApplyHuman ) Operation ( ) Operation {
2021-02-26 22:31:12 +01:00
return NewOperation ( arguments . ViewHuman , v . inAutomation , v . view )
2021-02-18 23:23:34 +01:00
}
func ( v * ApplyHuman ) Hooks ( ) [ ] terraform . Hook {
return [ ] terraform . Hook {
v . countHook ,
2021-02-26 22:31:12 +01:00
NewUiHook ( v . view ) ,
2021-02-18 23:23:34 +01:00
}
}
2021-02-26 22:31:12 +01:00
func ( v * ApplyHuman ) Diagnostics ( diags tfdiags . Diagnostics ) {
v . view . Diagnostics ( diags )
}
2021-02-18 23:23:34 +01:00
func ( v * ApplyHuman ) HelpPrompt ( ) {
command := "apply"
if v . destroy {
command = "destroy"
}
2021-02-26 22:31:12 +01:00
v . view . HelpPrompt ( command )
2021-02-18 23:23:34 +01:00
}
const stateOutPathPostApply = "The state of your infrastructure has been saved to the path below. This state is required to modify and destroy your infrastructure, so keep it safe. To inspect the complete state use the `terraform show` command."
2021-02-23 16:16:09 +01:00
// The ApplyJSON implementation renders streaming JSON logs, suitable for
// integrating with other software.
type ApplyJSON struct {
view * JSONView
destroy bool
countHook * countHook
}
var _ Apply = ( * ApplyJSON ) ( nil )
func ( v * ApplyJSON ) ResourceCount ( stateOutPath string ) {
operation := json . OperationApplied
if v . destroy {
operation = json . OperationDestroyed
}
v . view . ChangeSummary ( & json . ChangeSummary {
Add : v . countHook . Added ,
Change : v . countHook . Changed ,
Remove : v . countHook . Removed ,
Operation : operation ,
} )
}
func ( v * ApplyJSON ) Outputs ( outputValues map [ string ] * states . OutputValue ) {
outputs , diags := json . OutputsFromMap ( outputValues )
if diags . HasErrors ( ) {
v . Diagnostics ( diags )
} else {
v . view . Outputs ( outputs )
}
}
func ( v * ApplyJSON ) Operation ( ) Operation {
return & OperationJSON { view : v . view }
}
func ( v * ApplyJSON ) Hooks ( ) [ ] terraform . Hook {
return [ ] terraform . Hook {
v . countHook ,
newJSONHook ( v . view ) ,
}
}
func ( v * ApplyJSON ) Diagnostics ( diags tfdiags . Diagnostics ) {
v . view . Diagnostics ( diags )
}
func ( v * ApplyJSON ) HelpPrompt ( ) {
}