Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (c *ApplyCommand) Run(rawArgs []string) int {
}

// Build the operation request
opReq, opDiags := c.OperationRequest(be, view, planFile, args.Operation, args.AutoApprove)
opReq, opDiags := c.OperationRequest(be, view, planFile, args.Operation, args.AutoApprove, args.ViewType)
diags = diags.Append(opDiags)

// Collect variable value and add them to the operation request
Expand Down Expand Up @@ -248,6 +248,7 @@ func (c *ApplyCommand) OperationRequest(
planFile *planfile.Reader,
args *arguments.Operation,
autoApprove bool,
format arguments.ViewType,
Comment thread
zetHannes marked this conversation as resolved.
Outdated
) (*backend.Operation, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

Expand All @@ -257,7 +258,7 @@ func (c *ApplyCommand) OperationRequest(
diags = diags.Append(c.providerDevOverrideRuntimeWarnings())

// Build the operation
opReq := c.Operation(be)
opReq := c.Operation(be, format)
opReq.AutoApprove = autoApprove
opReq.ConfigDir = "."
opReq.PlanMode = args.PlanMode
Expand Down
3 changes: 2 additions & 1 deletion internal/command/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/repl"
"github.com/hashicorp/terraform/internal/terraform"
"github.com/hashicorp/terraform/internal/tfdiags"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *ConsoleCommand) Run(args []string) int {
c.ignoreRemoteVersionConflict(b)

// Build the operation
opReq := c.Operation(b)
opReq := c.Operation(b, arguments.ViewHuman)
opReq.ConfigDir = configPath
opReq.ConfigLoader, err = c.initConfigLoader()
opReq.AllowUnsetVariables = true // we'll just evaluate them as unknown
Expand Down
3 changes: 2 additions & 1 deletion internal/command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/dag"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/plans/planfile"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (c *GraphCommand) Run(args []string) int {
c.ignoreRemoteVersionConflict(b)

// Build the operation
opReq := c.Operation(b)
opReq := c.Operation(b, arguments.ViewHuman)
opReq.ConfigDir = configPath
opReq.ConfigLoader, err = c.initConfigLoader()
opReq.PlanFile = planFile
Expand Down
2 changes: 1 addition & 1 deletion internal/command/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *ImportCommand) Run(args []string) int {
}

// Build the operation
opReq := c.Operation(b)
opReq := c.Operation(b, arguments.ViewHuman)
opReq.ConfigDir = configPath
opReq.ConfigLoader, err = c.initConfigLoader()
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ type Meta struct {
//
// compactWarnings (-compact-warnings) selects a more compact presentation
// of warnings in the output when they are not accompanied by errors.
//
Comment thread
zetHannes marked this conversation as resolved.
Outdated
statePath string
stateOutPath string
backupPath string
Expand Down Expand Up @@ -553,9 +554,13 @@ func (m *Meta) extendedFlagSet(n string) *flag.FlagSet {
return f
}

// process will process any -no-color entries out of the arguments. This
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still some unwanted changes left over in this file.

// process will a:
// process any -no-color entries out of the arguments. This
// will potentially modify the args in-place. It will return the resulting
// slice, and update the Meta and Ui.
// and b:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here as well

// process any --json flag. If it is true, any outputs will be made in JSON format,
// otherwise it will be in human-readable format.
func (m *Meta) process(args []string) []string {
// We do this so that we retain the ability to technically call
// process multiple times, even if we have no plans to do so
Expand Down
4 changes: 2 additions & 2 deletions internal/command/meta_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (m *Meta) backendCLIOpts() (*backend.CLIOpts, error) {
// This prepares the operation. After calling this, the caller is expected
// to modify fields of the operation such as Sequence to specify what will
// be called.
func (m *Meta) Operation(b backend.Backend) *backend.Operation {
func (m *Meta) Operation(b backend.Backend, vt arguments.ViewType) *backend.Operation {
schema := b.ConfigSchema()
workspace, err := m.Workspace()
if err != nil {
Expand All @@ -411,7 +411,7 @@ func (m *Meta) Operation(b backend.Backend) *backend.Operation {

stateLocker := clistate.NewNoopLocker()
if m.stateLock {
view := views.NewStateLocker(arguments.ViewHuman, m.View)
view := views.NewStateLocker(vt, m.View)
stateLocker = clistate.NewLocker(m.stateLockTimeout, view)
}

Expand Down
5 changes: 3 additions & 2 deletions internal/command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *PlanCommand) Run(rawArgs []string) int {
}

// Build the operation request
opReq, opDiags := c.OperationRequest(be, view, args.Operation, args.OutPath)
opReq, opDiags := c.OperationRequest(be, view, args.Operation, args.OutPath, args.ViewType)
diags = diags.Append(opDiags)
if diags.HasErrors() {
view.Diagnostics(diags)
Expand Down Expand Up @@ -139,11 +139,12 @@ func (c *PlanCommand) OperationRequest(
view views.Plan,
args *arguments.Operation,
planOutPath string,
format arguments.ViewType,
Comment thread
zetHannes marked this conversation as resolved.
Outdated
) (*backend.Operation, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

// Build the operation
opReq := c.Operation(be)
opReq := c.Operation(be, format)
opReq.ConfigDir = "."
opReq.PlanMode = args.PlanMode
opReq.Hooks = view.Hooks()
Expand Down
3 changes: 2 additions & 1 deletion internal/command/providers_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/command/jsonprovider"
"github.com/hashicorp/terraform/internal/tfdiags"
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func (c *ProvidersSchemaCommand) Run(args []string) int {
}

// Build the operation
opReq := c.Operation(b)
opReq := c.Operation(b, arguments.ViewJSON)
opReq.ConfigDir = cwd
opReq.ConfigLoader, err = c.initConfigLoader()
opReq.AllowUnsetVariables = true
Expand Down
6 changes: 3 additions & 3 deletions internal/command/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *RefreshCommand) Run(rawArgs []string) int {
}

// Build the operation request
opReq, opDiags := c.OperationRequest(be, view, args.Operation)
opReq, opDiags := c.OperationRequest(be, view, args.Operation, args.ViewType)
diags = diags.Append(opDiags)
if diags.HasErrors() {
view.Diagnostics(diags)
Expand Down Expand Up @@ -131,12 +131,12 @@ func (c *RefreshCommand) PrepareBackend(args *arguments.State) (backend.Enhanced
return be, diags
}

func (c *RefreshCommand) OperationRequest(be backend.Enhanced, view views.Refresh, args *arguments.Operation,
func (c *RefreshCommand) OperationRequest(be backend.Enhanced, view views.Refresh, args *arguments.Operation, format arguments.ViewType,
Comment thread
zetHannes marked this conversation as resolved.
Outdated
) (*backend.Operation, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

// Build the operation
opReq := c.Operation(be)
opReq := c.Operation(be, format)
opReq.ConfigDir = "."
opReq.Hooks = view.Hooks()
opReq.Targets = args.Targets
Expand Down
3 changes: 2 additions & 1 deletion internal/command/state_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/command/format"
"github.com/hashicorp/terraform/internal/states"
"github.com/mitchellh/cli"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (c *StateShowCommand) Run(args []string) int {
}

// Build the operation (required to get the schemas)
opReq := c.Operation(b)
opReq := c.Operation(b, arguments.ViewHuman)
opReq.AllowUnsetVariables = true
opReq.ConfigDir = cwd

Expand Down
40 changes: 40 additions & 0 deletions internal/command/views/state_locker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package views

import (
"encoding/json"
"fmt"
"time"

"github.com/hashicorp/terraform/internal/command/arguments"
)
Expand All @@ -18,6 +20,8 @@ func NewStateLocker(vt arguments.ViewType, view *View) StateLocker {
switch vt {
case arguments.ViewHuman:
return &StateLockerHuman{view: view}
case arguments.ViewJSON:
return &StateLockerJSON{view: view}
default:
panic(fmt.Sprintf("unknown view type %v", vt))
}
Expand All @@ -29,6 +33,8 @@ type StateLockerHuman struct {
view *View
}

// StateLockerHuman and StateLockerJSON share the same methods so we only have to check
// whether StateLockerHuman conforms the interface
Comment thread
zetHannes marked this conversation as resolved.
Outdated
var _ StateLocker = (*StateLockerHuman)(nil)

func (v *StateLockerHuman) Locking() {
Expand All @@ -38,3 +44,37 @@ func (v *StateLockerHuman) Locking() {
func (v *StateLockerHuman) Unlocking() {
v.view.streams.Println("Releasing state lock. This may take a few moments...")
}

// StateLockerJSON is an implementation of StateLocker which prints the state lock status
// to a terminal in machine-readable JSON form.
type StateLockerJSON struct {
view *View
}

func (v *StateLockerJSON) Locking() {
current_timestamp := time.Now().Format(time.RFC3339)

json_data := map[string]string{
"@level": "info",
"@message": "Acquiring state lock. This may take a few moments...",
"@module": "terraform.ui",
"@timestamp": current_timestamp,
"type": "state_lock_acquire"}

lock_info_message, _ := json.Marshal(json_data)
v.view.streams.Println(string(lock_info_message))
}

func (v *StateLockerJSON) Unlocking() {
current_timestamp := time.Now().Format(time.RFC3339)

json_data := map[string]string{
"@level": "info",
"@message": "Releasing state lock. This may take a few moments...",
"@module": "terraform.ui",
"@timestamp": current_timestamp,
"type": "state_lock_release"}

lock_info_message, _ := json.Marshal(json_data)
v.view.streams.Println(string(lock_info_message))
}