Skip to content

Commit

Permalink
support remove whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Apr 13, 2023
1 parent 29db589 commit ff14de1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gcpsecretmanager2env -output ./.output.env ./.env
./.output.env

```
AN_ENV_FROM_SECRET_MANAGER=<VALUE_FROM_SECRET_MANAGEr>
AN_ENV_FROM_SECRET_MANAGER='<VALUE_FROM_SECRET_MANAGEr>'
```

## Usage
Expand All @@ -33,4 +33,6 @@ Note: <input-file> is a required positional argument.
show help
-output string
output file
-remove-whitespace
remove whitespaces {\n,\t}
```
34 changes: 25 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -65,24 +72,32 @@ 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 {
defaultCredential := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
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() {
Expand All @@ -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,
}
}

0 comments on commit ff14de1

Please sign in to comment.