github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/metadata_functions.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 9 "github.com/terramate-io/tf/command/jsonfunction" 10 "github.com/terramate-io/tf/lang" 11 "github.com/zclconf/go-cty/cty/function" 12 ) 13 14 var ( 15 ignoredFunctions = []string{"map", "list"} 16 ) 17 18 // MetadataFunctionsCommand is a Command implementation that prints out information 19 // about the available functions in Terraform. 20 type MetadataFunctionsCommand struct { 21 Meta 22 } 23 24 func (c *MetadataFunctionsCommand) Help() string { 25 return metadataFunctionsCommandHelp 26 } 27 28 func (c *MetadataFunctionsCommand) Synopsis() string { 29 return "Show signatures and descriptions for the available functions" 30 } 31 32 func (c *MetadataFunctionsCommand) Run(args []string) int { 33 args = c.Meta.process(args) 34 cmdFlags := c.Meta.defaultFlagSet("metadata functions") 35 var jsonOutput bool 36 cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output") 37 38 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 39 if err := cmdFlags.Parse(args); err != nil { 40 c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) 41 return 1 42 } 43 44 if !jsonOutput { 45 c.Ui.Error( 46 "The `terraform metadata functions` command requires the `-json` flag.\n") 47 cmdFlags.Usage() 48 return 1 49 } 50 51 scope := &lang.Scope{} 52 funcs := scope.Functions() 53 filteredFuncs := make(map[string]function.Function) 54 for k, v := range funcs { 55 if isIgnoredFunction(k) { 56 continue 57 } 58 filteredFuncs[k] = v 59 } 60 61 jsonFunctions, marshalDiags := jsonfunction.Marshal(filteredFuncs) 62 if marshalDiags.HasErrors() { 63 c.showDiagnostics(marshalDiags) 64 return 1 65 } 66 c.Ui.Output(string(jsonFunctions)) 67 68 return 0 69 } 70 71 const metadataFunctionsCommandHelp = ` 72 Usage: terraform [global options] metadata functions -json 73 74 Prints out a json representation of the available function signatures. 75 ` 76 77 func isIgnoredFunction(name string) bool { 78 for _, i := range ignoredFunctions { 79 if i == name { 80 return true 81 } 82 } 83 return false 84 }