Last active
April 13, 2023 10:31
-
-
Save ScMofeoluwa/31c559806722ab148bde355de74798e6 to your computer and use it in GitHub Desktop.
.env configs to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
env2json("test.env", "test.json") | |
} | |
func env2json(envFilePath string, jsonFilePath string) { | |
scanner := bufio.NewScanner(read(envFilePath)) | |
writer := bufio.NewWriter(write(jsonFilePath)) | |
res := make(map[string]string) | |
for scanner.Scan() { | |
line := scanner.Text() | |
if strings.HasPrefix(line, "#") { | |
continue | |
} | |
s := strings.Split(line, "=") | |
res[s[0]] = s[1] | |
} | |
obj := serialize(res, "_") | |
data, _ := json.MarshalIndent(obj, "", " ") | |
for _, line := range data { | |
writer.WriteByte(line) | |
} | |
writer.Flush() | |
fmt.Println("data written") | |
} | |
func read(path string) (f *os.File) { | |
f, err := os.Open(path) | |
if err != nil { | |
fmt.Println(err) | |
} | |
return | |
} | |
func write(path string) (f *os.File) { | |
f, err := os.Create(path) | |
if err != nil { | |
fmt.Println(err) | |
} | |
return | |
} | |
func serialize(obj map[string]string, delimiter string) map[string]interface{} { | |
output := make(map[string]interface{}) | |
for key, value := range obj { | |
res := make(map[string]string) | |
words := strings.Split(key, delimiter) | |
if len(words) == 2 { | |
res[words[1]] = value | |
output[words[0]] = res | |
} else { | |
output[key] = value | |
} | |
} | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment