github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/command/arguments/add.go (about)

     1  package arguments
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/internal/addrs"
     7  	"github.com/hashicorp/terraform/internal/tfdiags"
     8  )
     9  
    10  // Add represents the command-line arguments for the Add command.
    11  type Add struct {
    12  	// Addr specifies which resource to generate configuration for.
    13  	Addr addrs.AbsResourceInstance
    14  
    15  	// FromState specifies that the configuration should be populated with
    16  	// values from state.
    17  	FromState bool
    18  
    19  	// OutPath contains an optional path to store the generated configuration.
    20  	OutPath string
    21  
    22  	// Optional specifies whether or not to include optional attributes in the
    23  	// generated configuration. Defaults to false.
    24  	Optional bool
    25  
    26  	// Provider specifies the provider for the target.
    27  	Provider *addrs.AbsProviderConfig
    28  
    29  	// State from the common extended flags.
    30  	State *State
    31  
    32  	// ViewType specifies which output format to use. ViewHuman is currently the
    33  	// only supported view type.
    34  	ViewType ViewType
    35  }
    36  
    37  func ParseAdd(args []string) (*Add, tfdiags.Diagnostics) {
    38  	add := &Add{State: &State{}, ViewType: ViewHuman}
    39  
    40  	var diags tfdiags.Diagnostics
    41  	var provider string
    42  
    43  	cmdFlags := extendedFlagSet("add", add.State, nil, nil)
    44  	cmdFlags.BoolVar(&add.FromState, "from-state", false, "fill attribute values from a resource already managed by terraform")
    45  	cmdFlags.BoolVar(&add.Optional, "optional", false, "include optional attributes")
    46  	cmdFlags.StringVar(&add.OutPath, "out", "", "out")
    47  	cmdFlags.StringVar(&provider, "provider", "", "provider")
    48  
    49  	if err := cmdFlags.Parse(args); err != nil {
    50  		diags = diags.Append(tfdiags.Sourceless(
    51  			tfdiags.Error,
    52  			"Failed to parse command-line flags",
    53  			err.Error(),
    54  		))
    55  		return add, diags
    56  	}
    57  
    58  	args = cmdFlags.Args()
    59  	if len(args) != 1 {
    60  		//var adj string
    61  		adj := "few"
    62  		if len(args) > 1 {
    63  			adj = "many"
    64  		}
    65  		diags = diags.Append(tfdiags.Sourceless(
    66  			tfdiags.Error,
    67  			fmt.Sprintf("Too %s command line arguments", adj),
    68  			"Expected exactly one positional argument, giving the address of the resource to generate configuration for.",
    69  		))
    70  		return add, diags
    71  	}
    72  
    73  	// parse address from the argument
    74  	addr, addrDiags := addrs.ParseAbsResourceInstanceStr(args[0])
    75  	if addrDiags.HasErrors() {
    76  		diags = diags.Append(tfdiags.Sourceless(
    77  			tfdiags.Error,
    78  			fmt.Sprintf("Error parsing resource address: %s", args[0]),
    79  			"This command requires that the address argument specifies one resource instance.",
    80  		))
    81  		return add, diags
    82  	}
    83  	add.Addr = addr
    84  
    85  	if provider != "" {
    86  		if add.FromState {
    87  			diags = diags.Append(tfdiags.Sourceless(
    88  				tfdiags.Error,
    89  				"Incompatible command-line options",
    90  				"Cannot use both -from-state and -provider. The provider will be determined from the resource's state.",
    91  			))
    92  			return add, diags
    93  		}
    94  
    95  		absProvider, providerDiags := addrs.ParseAbsProviderConfigStr(provider)
    96  		if providerDiags.HasErrors() {
    97  			// The diagnostics returned from ParseAbsProviderConfigStr are
    98  			// not always clear, so we wrap them in a single customized diagnostic.
    99  			diags = diags.Append(tfdiags.Sourceless(
   100  				tfdiags.Error,
   101  				fmt.Sprintf("Invalid provider string: %s", provider),
   102  				providerDiags.Err().Error(),
   103  			))
   104  			return add, diags
   105  		}
   106  		add.Provider = &absProvider
   107  	}
   108  
   109  	return add, diags
   110  }