github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/command/flat_earth_graph.go (about)

     1  package command
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/internal/backend"
     5  	"github.com/hashicorp/terraform/internal/configs"
     6  	"github.com/hashicorp/terraform/internal/plans"
     7  	"github.com/hashicorp/terraform/internal/plans/planfile"
     8  	"github.com/hashicorp/terraform/internal/tfdiags"
     9  )
    10  
    11  type FlatEarthGraphCommand struct {
    12  	Meta
    13  }
    14  
    15  // this is a command only in name, we don't want to waste our time writing for a terminal's UI
    16  func (c *FlatEarthGraphCommand) Run() (map[string]*configs.Resource, tfdiags.Diagnostics) {
    17  	var diags tfdiags.Diagnostics
    18  	var planPath string
    19  	var planFile *planfile.Reader
    20  
    21  	cmdFlags := c.Meta.defaultFlagSet("flat-earth-graph")
    22  	configPath, err := ModulePath(cmdFlags.Args())
    23  
    24  	if err != nil {
    25  		diags.Append(err)
    26  		return nil, diags
    27  	}
    28  
    29  	if planPath != "" {
    30  		planFile, err = c.PlanFile(planPath)
    31  		if err != nil {
    32  			diags.Append(err)
    33  			return nil, diags
    34  		}
    35  	}
    36  
    37  	backendConfig, backendDiags := c.loadBackendConfig(configPath)
    38  	diags = diags.Append(backendDiags)
    39  	if diags.HasErrors() {
    40  		return nil, diags
    41  	}
    42  
    43  	b, backendDiags := c.Backend(&BackendOpts{
    44  		Config: backendConfig,
    45  	})
    46  	diags = diags.Append(backendDiags)
    47  	if diags.HasErrors() {
    48  		return nil, diags
    49  	}
    50  
    51  	local, ok := b.(backend.Local)
    52  	if !ok {
    53  		if diags.HasErrors() {
    54  			return nil, diags
    55  		}
    56  	}
    57  
    58  	c.ignoreRemoteVersionConflict(b)
    59  
    60  	opReq := c.Operation(b)
    61  	opReq.ConfigDir = configPath
    62  	opReq.ConfigLoader, err = c.initConfigLoader()
    63  	opReq.PlanFile = planFile
    64  	opReq.AllowUnsetVariables = true
    65  	if err != nil {
    66  		diags = diags.Append(err)
    67  		return nil, diags
    68  	}
    69  
    70  	lr, _, ctxDiags := local.LocalRun(opReq)
    71  	diags = diags.Append(ctxDiags)
    72  	if ctxDiags.HasErrors() {
    73  		return nil, diags
    74  	}
    75  
    76  	// TODO: we should probably consider extending this to other plan modes, see graph command
    77  	graph := lr.Core.PlanFlatEarthGraph(lr.Config, lr.InputState, plans.NormalMode)
    78  	return graph, diags
    79  }