Skip to content

Commit

Permalink
Support tfenv export (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
pecigonzalo authored and nickatsegment committed Feb 23, 2019
1 parent 059f1d1 commit 063d1b0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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"}
Expand Down

0 comments on commit 063d1b0

Please sign in to comment.