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