github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/flag_kv.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // FlagStringKV is a flag.Value implementation for parsing user variables
     9  // from the command-line in the format of '-var key=value', where value is
    10  // only ever a primitive.
    11  type FlagStringKV map[string]string
    12  
    13  func (v *FlagStringKV) String() string {
    14  	return ""
    15  }
    16  
    17  func (v *FlagStringKV) Set(raw string) error {
    18  	idx := strings.Index(raw, "=")
    19  	if idx == -1 {
    20  		return fmt.Errorf("No '=' value in arg: %s", raw)
    21  	}
    22  
    23  	if *v == nil {
    24  		*v = make(map[string]string)
    25  	}
    26  
    27  	key, value := raw[0:idx], raw[idx+1:]
    28  	(*v)[key] = value
    29  	return nil
    30  }
    31  
    32  // FlagStringSlice is a flag.Value implementation for parsing targets from the
    33  // command line, e.g. -target=aws_instance.foo -target=aws_vpc.bar
    34  type FlagStringSlice []string
    35  
    36  func (v *FlagStringSlice) String() string {
    37  	return ""
    38  }
    39  func (v *FlagStringSlice) Set(raw string) error {
    40  	*v = append(*v, raw)
    41  
    42  	return nil
    43  }