From 063d1b046d21abfc680f5ed16d90bb52401d8d8c Mon Sep 17 00:00:00 2001 From: Gonzalo Peci Date: Sat, 23 Feb 2019 02:56:14 +0100 Subject: [PATCH] Support tfenv export (#188) --- cmd/export.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/export.go b/cmd/export.go index 51a61da4..97a5da54 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -32,7 +32,7 @@ var ( ) func init() { - exportCmd.Flags().StringVarP(&exportFormat, "format", "f", "json", "Output format (json, java-properties, csv, tsv, dotenv)") + exportCmd.Flags().StringVarP(&exportFormat, "format", "f", "json", "Output format (json, java-properties, csv, tsv, dotenv, tfvars)") exportCmd.Flags().StringVarP(&exportOutput, "output-file", "o", "", "Output file (default is standard output)") RootCmd.AddCommand(exportCmd) } @@ -97,6 +97,8 @@ func runExport(cmd *cobra.Command, args []string) error { err = exportAsTsv(params, w) case "dotenv": err = exportAsEnvFile(params, w) + case "tfvars": + err = exportAsTfvars(params, w) default: err = errors.Errorf("Unsupported export format: %s", exportFormat) } @@ -120,6 +122,15 @@ func exportAsEnvFile(params map[string]string, w io.Writer) error { return nil } +func exportAsTfvars(params map[string]string, w io.Writer) error { + // Terraform Variables is like dotenv, but removes the TF_VAR and keeps lowercase + for _, k := range sortedKeys(params) { + key := strings.TrimPrefix(k, "tf_var_") + w.Write([]byte(fmt.Sprintf(`%s = "%s"`+"\n", key, doubleQuoteEscape(params[k])))) + } + return nil +} + func exportAsJson(params map[string]string, w io.Writer) error { // JSON like: // {"param1":"value1","param2":"value2"}