-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (91 loc) · 2.8 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
package main
import (
"fmt"
"log"
"strings"
"path/filepath"
"io/ioutil"
"os"
)
var (
// Configuration from CLI args
DirToParse string
OutFileName string
GeneratorFileName string
)
func PathExists(path string) bool {
if _, e := os.Stat(path); e != nil {
// either non existent or smth wrong with the fs
return false
}
return true
}
func ParseCLI() {
var e error
for idx, arg := range os.Args {
//Skip first arg (process name)
if idx>0 && arg[0] != '-' {
DirToParse, e = filepath.Abs(arg)
if e != nil {
log.Fatal("filepath.Abs err:", e)
}
//TODO handle case where path to process is passed as last arg
continue
}
arg = arg[1:]
argComponents := strings.Split(arg, "=")
argName := argComponents[0]
argVal := ""
if len(argComponents)>1 { argVal = argComponents[1] }
switch argName {
case "outfile":
OutFileName = argVal
case "generator":
GeneratorFileName = "generate_godescribe.go"
if len(argVal)>0 { GeneratorFileName = argVal }
}
}
}
func MustValidateConfig() {
if !PathExists(DirToParse) {
log.Fatal("Could not access path:", DirToParse)
}
}
func Usage() {
fmt.Println(`godescribe [-flag1 ... -flagN]
Without arguments : godescribe will create a generator file to call itself to parse the local package and list functions.
-outfile=filename.go : will output json encoded symbols as a global var to the specified Go source file
-generator : will create a go generate generator to call itself at build time and create a symbols file named gogenerate_godescribe.go
-generator=filename.go same as previously with the ability to name the go generate source file used for storing symbols information.
`)
}
func PrintConfig() {
fmt.Println("Dir to parse :", DirToParse)
fmt.Println("Generator file:", GeneratorFileName)
}
func main() {
ParseCLI()
MustValidateConfig()
if len(DirToParse)>0 {
symbolsJson, e := ParseSymbols(DirToParse)
if e != nil {
log.Fatal("Failed to parse symbols with err:", e)
}
if len(OutFileName) > 0 {
gocode, e := GenGenerator(symbolsJson)
if e = ioutil.WriteFile(OutFileName, []byte(gocode), 0644); e != nil {
log.Fatal("Failed to write to output file: %s with err: %v", OutFileName, e)
}
} else {
fmt.Println(symbolsJson)
}
// generator mode : simply create a source file containing a go generate directive
// that will call godescribe to generate a symbols source file
} else if len(GeneratorFileName) > 0 {
generatorCode := `package main
//go:generate godescribe . -outfile=godescribe_symbols.go`
if e := ioutil.WriteFile(GeneratorFileName, []byte(generatorCode), 0644); e != nil {
log.Fatalf("Failed to create generator file: %s with err: %v", GeneratorFileName , e)
}
}
}