-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (51 loc) · 1.19 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/potproject/goenvgen/gen"
)
var (
pkg = flag.String("p", "envgen", "the name of the package for the generated code")
types = flag.String("t", "", "Manually type definition setting (example: \"-t ENV_BOOL=bool,ENV_S_INT=[]int\")")
required = flag.String("r", "", "Required Type setting (example: \"-r ENV_REQ_BOOL,ENV_REQ_STRING\")")
)
func main() {
flag.Parse()
err := run(flag.Args())
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
fmt.Println("Generated " + *pkg + " Packages.")
os.Exit(0)
}
func run(args []string) error {
file := ".env"
if len(args) == 1 {
file = args[0]
}
if len(args) > 1 {
return errors.New(".env File is not specified")
}
mapmtds := map[string]string{}
if *types != "" {
mtds := strings.Split(*types, ",")
for _, v := range mtds {
mtd := strings.Split(v, "=")
if len(mtd) != 2 {
return errors.New("Manually type definition invalid value: " + v)
}
mapmtds[mtd[0]] = mtd[1]
}
}
reqs := []string{}
if required != nil && *required != "" {
reqs = strings.Split(*required, ",")
}
err := gen.GenerateFile(file, *pkg, mapmtds, reqs)
return err
}