-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (133 loc) · 3.18 KB
/
main.go
File metadata and controls
154 lines (133 loc) · 3.18 KB
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
149
150
151
152
153
154
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path"
"runtime/debug"
"strings"
"gopkg.in/yaml.v3"
)
// -ldflags "-X main.version=v1.0.2"
var version string
type keys []string
func (k *keys) String() string {
return strings.Join(*k, " ")
}
func (k *keys) Set(value string) error {
*k = append(*k, value)
return nil
}
func main() {
var k keys
var kv keys
ver := flag.Bool("version", false, "show version")
noDash := flag.Bool("no-dash", false, "prepend dashes into output flag keys")
eq := flag.Bool("eq", false, "split flag and value with \"=\" mark")
file := flag.String("f", "cargs.yml", "source file")
encoding := flag.String("e", "", "source encoding; if not set then based on file ext")
flag.Var(&k, "v", "key to extract; -v example.a => ${example.a}")
flag.Var(&kv, "kv", "key to extract; return with flag name; -kv e=example.a => -e ${example.a}")
flag.Parse()
if *ver {
i, _ := debug.ReadBuildInfo()
fmt.Println(getBinVersion(i))
fmt.Println(i.GoVersion)
if len(i.Main.Sum) > 0 {
fmt.Println(i.Main.Sum)
}
return
}
fmt.Fprintln(os.Stdout, strings.Join(run(*file, *encoding, *noDash, *eq, k, kv), " "))
}
func run(file, encoding string, noDash, eq bool, k, kv keys) []string {
if len(encoding) == 0 {
encoding = path.Ext(file)[1:]
}
var unmarshal func([]byte, interface{}) error
switch encoding {
case "yml", "yaml":
unmarshal = yaml.Unmarshal
case "json":
unmarshal = json.Unmarshal
default:
log.Fatal("Unknown encoding " + encoding)
}
data, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
var source map[string]interface{} = make(map[string]interface{})
if err = unmarshal(data, &source); err != nil {
log.Fatal(err)
}
var args []string = make([]string, 0)
for _, key := range k {
vals := resolveMapKey(source, key)
args = append(args, vals...)
}
for _, keyvalue := range kv {
comps := strings.Split(keyvalue, "=")
if len(comps) != 2 {
log.Fatal(fmt.Sprintf("invalid kv syntax at %s; Must be separate by \"=\"", keyvalue))
}
for _, v := range resolveMapKey(source, comps[1]) {
separator := " "
if eq {
separator = "="
}
prefix := "-"
if len(comps[0]) > 1 {
prefix = "--"
}
if noDash {
prefix = ""
}
args = append(args, fmt.Sprintf(
"%s%s%s%s",
prefix,
comps[0],
separator,
v,
))
}
}
return args
}
func resolveMapKey(m map[string]interface{}, key string) []string {
currentMap := m
comonents := strings.Split(key, ".")
currentKey := 0
var result []string = make([]string, 0)
for _, k := range comonents {
val, exist := currentMap[comonents[currentKey]]
if !exist {
log.Fatal("map does not contain key " + k)
}
if currentKey != len(comonents)-1 {
mv, casted := val.(map[string]interface{})
if !casted {
log.Fatal(k + " is not a map")
}
currentMap = mv
currentKey++
continue
}
if av, casted := val.([]interface{}); casted {
for _, el := range av {
result = append(result, fmt.Sprintf("%v", el))
}
break
}
result = append(result, fmt.Sprintf("%v", val))
}
return result
}
func getBinVersion(bi *debug.BuildInfo) string {
if len(version) != 0 {
return version
}
return bi.Main.Version
}