github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/show.go (about)

     1  package views
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/internal/command/arguments"
     7  	"github.com/hashicorp/terraform/internal/command/jsonformat"
     8  	"github.com/hashicorp/terraform/internal/command/jsonplan"
     9  	"github.com/hashicorp/terraform/internal/command/jsonprovider"
    10  	"github.com/hashicorp/terraform/internal/command/jsonstate"
    11  	"github.com/hashicorp/terraform/internal/configs"
    12  	"github.com/hashicorp/terraform/internal/plans"
    13  	"github.com/hashicorp/terraform/internal/states/statefile"
    14  	"github.com/hashicorp/terraform/internal/terraform"
    15  	"github.com/hashicorp/terraform/internal/tfdiags"
    16  )
    17  
    18  type Show interface {
    19  	// Display renders the plan, if it is available. If plan is nil, it renders the statefile.
    20  	Display(config *configs.Config, plan *plans.Plan, stateFile *statefile.File, schemas *terraform.Schemas) int
    21  
    22  	// Diagnostics renders early diagnostics, resulting from argument parsing.
    23  	Diagnostics(diags tfdiags.Diagnostics)
    24  }
    25  
    26  func NewShow(vt arguments.ViewType, view *View) Show {
    27  	switch vt {
    28  	case arguments.ViewJSON:
    29  		return &ShowJSON{view: view}
    30  	case arguments.ViewHuman:
    31  		return &ShowHuman{view: view}
    32  	default:
    33  		panic(fmt.Sprintf("unknown view type %v", vt))
    34  	}
    35  }
    36  
    37  type ShowHuman struct {
    38  	view *View
    39  }
    40  
    41  var _ Show = (*ShowHuman)(nil)
    42  
    43  func (v *ShowHuman) Display(config *configs.Config, plan *plans.Plan, stateFile *statefile.File, schemas *terraform.Schemas) int {
    44  	renderer := jsonformat.Renderer{
    45  		Colorize:            v.view.colorize,
    46  		Streams:             v.view.streams,
    47  		RunningInAutomation: v.view.runningInAutomation,
    48  	}
    49  
    50  	if plan != nil {
    51  		outputs, changed, drift, attrs, err := jsonplan.MarshalForRenderer(plan, schemas)
    52  		if err != nil {
    53  			v.view.streams.Eprintf("Failed to marshal plan to json: %s", err)
    54  			return 1
    55  		}
    56  
    57  		jplan := jsonformat.Plan{
    58  			PlanFormatVersion:     jsonplan.FormatVersion,
    59  			ProviderFormatVersion: jsonprovider.FormatVersion,
    60  			OutputChanges:         outputs,
    61  			ResourceChanges:       changed,
    62  			ResourceDrift:         drift,
    63  			ProviderSchemas:       jsonprovider.MarshalForRenderer(schemas),
    64  			RelevantAttributes:    attrs,
    65  		}
    66  
    67  		var opts []jsonformat.PlanRendererOpt
    68  		if !plan.CanApply() {
    69  			opts = append(opts, jsonformat.CanNotApply)
    70  		}
    71  		if plan.Errored {
    72  			opts = append(opts, jsonformat.Errored)
    73  		}
    74  
    75  		renderer.RenderHumanPlan(jplan, plan.UIMode, opts...)
    76  	} else {
    77  		if stateFile == nil {
    78  			v.view.streams.Println("No state.")
    79  			return 0
    80  		}
    81  
    82  		root, outputs, err := jsonstate.MarshalForRenderer(stateFile, schemas)
    83  		if err != nil {
    84  			v.view.streams.Eprintf("Failed to marshal state to json: %s", err)
    85  			return 1
    86  		}
    87  
    88  		jstate := jsonformat.State{
    89  			StateFormatVersion:    jsonstate.FormatVersion,
    90  			ProviderFormatVersion: jsonprovider.FormatVersion,
    91  			RootModule:            root,
    92  			RootModuleOutputs:     outputs,
    93  			ProviderSchemas:       jsonprovider.MarshalForRenderer(schemas),
    94  		}
    95  
    96  		renderer.RenderHumanState(jstate)
    97  	}
    98  	return 0
    99  }
   100  
   101  func (v *ShowHuman) Diagnostics(diags tfdiags.Diagnostics) {
   102  	v.view.Diagnostics(diags)
   103  }
   104  
   105  type ShowJSON struct {
   106  	view *View
   107  }
   108  
   109  var _ Show = (*ShowJSON)(nil)
   110  
   111  func (v *ShowJSON) Display(config *configs.Config, plan *plans.Plan, stateFile *statefile.File, schemas *terraform.Schemas) int {
   112  	if plan != nil {
   113  		jsonPlan, err := jsonplan.Marshal(config, plan, stateFile, schemas)
   114  
   115  		if err != nil {
   116  			v.view.streams.Eprintf("Failed to marshal plan to json: %s", err)
   117  			return 1
   118  		}
   119  		v.view.streams.Println(string(jsonPlan))
   120  	} else {
   121  		// It is possible that there is neither state nor a plan.
   122  		// That's ok, we'll just return an empty object.
   123  		jsonState, err := jsonstate.Marshal(stateFile, schemas)
   124  		if err != nil {
   125  			v.view.streams.Eprintf("Failed to marshal state to json: %s", err)
   126  			return 1
   127  		}
   128  		v.view.streams.Println(string(jsonState))
   129  	}
   130  	return 0
   131  }
   132  
   133  // Diagnostics should only be called if show cannot be executed.
   134  // In this case, we choose to render human-readable diagnostic output,
   135  // primarily for backwards compatibility.
   136  func (v *ShowJSON) Diagnostics(diags tfdiags.Diagnostics) {
   137  	v.view.Diagnostics(diags)
   138  }