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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/hashicorp/terraform/internal/backend"
     8  	"github.com/hashicorp/terraform/internal/command/arguments"
     9  	"github.com/hashicorp/terraform/internal/command/jsonprovider"
    10  	"github.com/hashicorp/terraform/internal/tfdiags"
    11  )
    12  
    13  // ProvidersCommand is a Command implementation that prints out information
    14  // about the providers used in the current configuration/state.
    15  type ProvidersSchemaCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *ProvidersSchemaCommand) Help() string {
    20  	return providersSchemaCommandHelp
    21  }
    22  
    23  func (c *ProvidersSchemaCommand) Synopsis() string {
    24  	return "Show schemas for the providers used in the configuration"
    25  }
    26  
    27  func (c *ProvidersSchemaCommand) Run(args []string) int {
    28  	args = c.Meta.process(args)
    29  	cmdFlags := c.Meta.defaultFlagSet("providers schema")
    30  	var jsonOutput bool
    31  	cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output")
    32  
    33  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    34  	if err := cmdFlags.Parse(args); err != nil {
    35  		c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
    36  		return 1
    37  	}
    38  
    39  	if !jsonOutput {
    40  		c.Ui.Error(
    41  			"The `terraform providers schema` command requires the `-json` flag.\n")
    42  		cmdFlags.Usage()
    43  		return 1
    44  	}
    45  
    46  	// Check for user-supplied plugin path
    47  	var err error
    48  	if c.pluginPath, err = c.loadPluginPath(); err != nil {
    49  		c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
    50  		return 1
    51  	}
    52  
    53  	var diags tfdiags.Diagnostics
    54  
    55  	// Load the backend
    56  	b, backendDiags := c.Backend(nil)
    57  	diags = diags.Append(backendDiags)
    58  	if backendDiags.HasErrors() {
    59  		c.showDiagnostics(diags)
    60  		return 1
    61  	}
    62  
    63  	// We require a local backend
    64  	local, ok := b.(backend.Local)
    65  	if !ok {
    66  		c.showDiagnostics(diags) // in case of any warnings in here
    67  		c.Ui.Error(ErrUnsupportedLocalOp)
    68  		return 1
    69  	}
    70  
    71  	// This is a read-only command
    72  	c.ignoreRemoteVersionConflict(b)
    73  
    74  	// we expect that the config dir is the cwd
    75  	cwd, err := os.Getwd()
    76  	if err != nil {
    77  		c.Ui.Error(fmt.Sprintf("Error getting cwd: %s", err))
    78  		return 1
    79  	}
    80  
    81  	// Build the operation
    82  	opReq := c.Operation(b, arguments.ViewJSON)
    83  	opReq.ConfigDir = cwd
    84  	opReq.ConfigLoader, err = c.initConfigLoader()
    85  	opReq.AllowUnsetVariables = true
    86  	if err != nil {
    87  		diags = diags.Append(err)
    88  		c.showDiagnostics(diags)
    89  		return 1
    90  	}
    91  
    92  	// Get the context
    93  	lr, _, ctxDiags := local.LocalRun(opReq)
    94  	diags = diags.Append(ctxDiags)
    95  	if ctxDiags.HasErrors() {
    96  		c.showDiagnostics(diags)
    97  		return 1
    98  	}
    99  
   100  	schemas, moreDiags := lr.Core.Schemas(lr.Config, lr.InputState)
   101  	diags = diags.Append(moreDiags)
   102  	if moreDiags.HasErrors() {
   103  		c.showDiagnostics(diags)
   104  		return 1
   105  	}
   106  
   107  	jsonSchemas, err := jsonprovider.Marshal(schemas)
   108  	if err != nil {
   109  		c.Ui.Error(fmt.Sprintf("Failed to marshal provider schemas to json: %s", err))
   110  		return 1
   111  	}
   112  	c.Ui.Output(string(jsonSchemas))
   113  
   114  	return 0
   115  }
   116  
   117  const providersSchemaCommandHelp = `
   118  Usage: terraform [global options] providers schema -json
   119  
   120    Prints out a json representation of the schemas for all providers used 
   121    in the current configuration.
   122  `