/
main.go
75 lines (66 loc) · 1.9 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
package main
import (
"context"
cli "github.com/jawher/mow.cli"
"log"
"os"
"time"
)
type Config struct {
Deep int
Package string
Target string
Variable string
Comment string
Write bool
Suffix, Path string
}
func main() {
app := cli.App("gosingl", "generate module level singleton")
chosenPackage := app.StringArg("PKG", "", "package to walk to")
chosenStruct := app.StringArg("TARGET", "", "structure will be use as module singleton")
chosenVariable := app.StringOpt("variable", "Instance", "singleton instance (module variable)")
comment := app.StringOpt("comment", "Code generated by <git repo>. DO NOT EDIT.", "file header comment")
suffix := app.StringOpt("suffix", "_singleton.go", "suffix of generated file")
path := app.StringOpt("filepath", "", "path override")
chosenDeep := app.IntOpt("deep", 0, "recursive deep")
delay := app.IntOpt("delay", 0, "debug only")
// writeType
write := app.BoolOpt("w write", false, "writes the result in file")
exitOnError := func(err error) {
log.Println(err)
os.Exit(1)
}
var err error
app.Action = func() {
if *delay > 0 {
time.Sleep(time.Second * time.Duration(*delay))
}
cfg := Config{
Deep: *chosenDeep,
Package: *chosenPackage,
Target: *chosenStruct,
Variable: *chosenVariable,
Comment: *comment,
Write: *write,
Suffix: *suffix,
Path: *path,
}
ctx := context.Background()
ctx = SetupCtx(ctx, //as reference
nil, //global output target //"write" and "file" flags will be ignored if set
nil, //shared buffer, mostly internal
nil, //helper to generate name for unnamed function input parameters
nil, //checker which excludes duplicates from output
nil, //blacklisted file suffix ([]string{"_test.go", cfg.Suffix},)
)
err = ParsePackage(ctx, cfg)
if err != nil {
exitOnError(err)
}
}
err = app.Run(os.Args)
if err != nil {
exitOnError(err)
}
}