From ff14de16cf00183fe2f41c4fe146008b3b6f05d1 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 13 Apr 2023 19:52:03 +0900 Subject: [PATCH] support remove whitespaces --- README.md | 4 +++- main.go | 34 +++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 99d4d96..6ff7aa5 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ gcpsecretmanager2env -output ./.output.env ./.env ./.output.env ``` -AN_ENV_FROM_SECRET_MANAGER= +AN_ENV_FROM_SECRET_MANAGER='' ``` ## Usage @@ -33,4 +33,6 @@ Note: is a required positional argument. show help -output string output file + -remove-whitespace + remove whitespaces {\n,\t} ``` \ No newline at end of file diff --git a/main.go b/main.go index 4f237d6..60f2534 100644 --- a/main.go +++ b/main.go @@ -49,13 +49,20 @@ func main() { if err != nil { log.Fatalf("failed to decode secret (%s): %v", value, err) } - envs[key] = string(env) + escaped := string(env) + if flags.removeWhitespace { + escaped = strings.ReplaceAll(escaped, "'", "'\"'\"'") + escaped = strings.ReplaceAll(escaped, "\n", "\\n") + escaped = strings.ReplaceAll(escaped, "\t", "\\t") + } + envs[key] = escaped } output := bytes.NewBuffer(nil) for key, value := range envs { - output.WriteString(fmt.Sprintf("%s=%s\n", key, value)) + output.WriteString(fmt.Sprintf("%s='%s'\n", key, value)) } + outputBytes := output.Bytes() file := os.Stdout if flags.output != "" { @@ -65,17 +72,24 @@ func main() { } } writer := bufio.NewWriter(file) - _, err = writer.Write(output.Bytes()) + _, err = writer.Write(outputBytes) if err != nil { log.Fatalf("failed to write output: %v", err) } writer.Flush() + + envs, err = envparse.Parse(bytes.NewReader(outputBytes)) + if err != nil { + log.Fatalf("failed to parse output: %v", err) + } + fmt.Printf("TEST: %s", strings.ReplaceAll(envs["TEST"], "\n", "NN")) } type flags struct { - output string - input string - credential string + output string + input string + credential string + removeWhitespace bool } func parseFlags() flags { @@ -83,6 +97,7 @@ func parseFlags() flags { output := flag.String("output", "", "output file") help := flag.Bool("help", false, "show help") credential := flag.String("credential", defaultCredential, "gcp credential file") + removeWhitespace := flag.Bool("remove-whitespace", false, "remove whitespaces {\\n,\\t}") flag.Parse() flag.Usage = func() { @@ -98,8 +113,9 @@ func parseFlags() flags { inputFilename := flag.Args()[0] return flags{ - output: *output, - input: inputFilename, - credential: *credential, + output: *output, + input: inputFilename, + credential: *credential, + removeWhitespace: *removeWhitespace, } }