github.com/pulumi/terraform@v1.4.0/pkg/command/metadata_functions.go (about)

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