-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
148 lines (119 loc) · 3.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/tidwall/jsonc"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
var (
app = "dotnet-appsettings-env"
version = "dev"
description = "Convert .NET appsettings.json file to Kubernetes, Docker, Docker-Compose and Bicep environment variables."
site = "https://github.com/dassump/dotnet-appsettings-env"
file = flag.String("file", "./appsettings.json", "Path to file appsettings.json")
output = flag.String("type", "k8s", "Output to Kubernetes (k8s) / Docker (docker) / Docker Compose (compose) / Bicep (bicep)")
separator = flag.String("separator", "__", "Separator character(s)")
format = map[string]string{
"k8s": "- name: %q\n value: %q\n",
"docker": "%s=%q\n",
"compose": "%s: %q\n",
"bicep": "{\nname: '%s'\nvalue: '%s'\n}\n",
}
objects = make(map[string]any)
variables = make(map[string]string)
)
func init() {
log.SetFlags(log.Lmsgprefix)
log.SetPrefix("Error: ")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "%s (%s)\n\n%s\n%s\n\n", app, version, description, site)
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if _, ok := format[*output]; !ok {
log.Fatalln(*output, "is not valid type")
}
if len(*separator) < 1 {
log.Fatalln("separator cannot be an empty string")
}
}
func main() {
content, err := os.ReadFile(*file)
if err != nil {
log.Fatalln(err)
}
if content, _, err = transform.Bytes(
unicode.BOMOverride(unicode.UTF8.NewDecoder()), content,
); err != nil {
log.Fatalln(err)
}
reader := bytes.NewReader(jsonc.ToJSON(content))
decoder := json.NewDecoder(reader)
decoder.UseNumber()
if err := decoder.Decode(&objects); err != nil {
switch err := err.(type) {
case *json.SyntaxError:
newline := []byte("\n")
line := bytes.Count(content[:err.Offset], newline) + 1
column := int(err.Offset) - bytes.LastIndex(content[:err.Offset], newline) - len(newline)
lenght := int64(len(content))
near := int64(60)
before := err.Offset - near
if err.Offset-near < 0 {
before = 0
}
after := err.Offset + near
if err.Offset+near > lenght {
after = lenght
}
log.Fatalf(
"%s in %s\n\n... line %d, column %d\n%s >>> %s <<< %s\n...\n",
err, *file, line, column,
content[before:err.Offset-1], content[err.Offset-1:err.Offset], content[err.Offset:after],
)
default:
log.Fatalf("%s in %s\n", err, *file)
}
}
parser(objects, variables, nil)
keys := make([]string, 0, len(variables))
for key := range variables {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return strings.ToLower(keys[i]) < strings.ToLower(keys[j])
})
for _, key := range keys {
fmt.Printf(format[*output], key, variables[key])
}
}
func parser(in map[string]any, out map[string]string, root []string) {
for key, value := range in {
keys := append(root, key)
switch any(value).(type) {
case []any:
for key, value := range value.([]any) {
switch any(value).(type) {
case []any:
parser(map[string]any{fmt.Sprint(key): value}, out, keys)
case map[string]any:
parser(value.(map[string]any), out, append(keys, fmt.Sprint(key)))
default:
out[fmt.Sprintf("%s%s%d", strings.Join(keys, *separator), *separator, key)] = fmt.Sprint(value)
}
}
case map[string]any:
parser(value.(map[string]any), out, keys)
default:
out[strings.Join(keys, *separator)] = fmt.Sprint(value)
}
}
}