2014-07-18 20:37:27 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-19 18:56:50 +02:00
|
|
|
"io/ioutil"
|
2014-07-18 20:37:27 +02:00
|
|
|
"strings"
|
2014-07-18 23:00:40 +02:00
|
|
|
|
2014-08-19 18:56:50 +02:00
|
|
|
"github.com/hashicorp/hcl"
|
2014-09-09 05:43:59 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2014-07-18 20:37:27 +02:00
|
|
|
)
|
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
// FlagKV is a flag.Value implementation for parsing user variables
|
2014-07-18 20:37:27 +02:00
|
|
|
// from the command-line in the format of '-var key=value'.
|
2015-02-22 03:30:41 +01:00
|
|
|
type FlagKV map[string]string
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
func (v *FlagKV) String() string {
|
2014-07-18 20:37:27 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
func (v *FlagKV) Set(raw string) error {
|
2014-07-18 20:37:27 +02:00
|
|
|
idx := strings.Index(raw, "=")
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf("No '=' value in arg: %s", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *v == nil {
|
|
|
|
*v = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value := raw[0:idx], raw[idx+1:]
|
|
|
|
(*v)[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
// FlagKVFile is a flag.Value implementation for parsing user variables
|
2014-07-18 20:37:27 +02:00
|
|
|
// from the command line in the form of files. i.e. '-var-file=foo'
|
2015-02-22 03:30:41 +01:00
|
|
|
type FlagKVFile map[string]string
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
func (v *FlagKVFile) String() string {
|
2014-07-18 20:37:27 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
func (v *FlagKVFile) Set(raw string) error {
|
|
|
|
vs, err := loadKVFile(raw)
|
2014-07-18 23:00:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if *v == nil {
|
|
|
|
*v = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range vs {
|
|
|
|
(*v)[key] = value
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-18 23:00:40 +02:00
|
|
|
|
2015-02-22 03:30:41 +01:00
|
|
|
func loadKVFile(rawPath string) (map[string]string, error) {
|
2014-09-09 05:43:59 +02:00
|
|
|
path, err := homedir.Expand(rawPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error expanding path: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-08-19 18:56:50 +02:00
|
|
|
// Read the HCL file and prepare for parsing
|
|
|
|
d, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading %s: %s", path, err)
|
2014-07-18 23:00:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 18:56:50 +02:00
|
|
|
// Parse it
|
|
|
|
obj, err := hcl.Parse(string(d))
|
2014-07-18 23:00:40 +02:00
|
|
|
if err != nil {
|
2014-08-19 18:56:50 +02:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error parsing %s: %s", path, err)
|
2014-07-18 23:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var result map[string]string
|
2014-08-19 18:56:50 +02:00
|
|
|
if err := hcl.DecodeObject(&result, obj); err != nil {
|
2015-06-26 02:56:37 +02:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error decoding Terraform vars file: %s\n\n"+
|
|
|
|
"The vars file should be in the format of `key = \"value\"`.\n"+
|
|
|
|
"Decoding errors are usually caused by an invalid format.",
|
|
|
|
err)
|
2014-07-18 23:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2015-03-24 17:18:15 +01:00
|
|
|
|
|
|
|
// FlagStringSlice is a flag.Value implementation for parsing targets from the
|
|
|
|
// command line, e.g. -target=aws_instance.foo -target=aws_vpc.bar
|
|
|
|
|
|
|
|
type FlagStringSlice []string
|
|
|
|
|
|
|
|
func (v *FlagStringSlice) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
func (v *FlagStringSlice) Set(raw string) error {
|
|
|
|
*v = append(*v, raw)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|