/
command.go
183 lines (157 loc) · 4.02 KB
/
command.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package compiler
import (
"bytes"
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"github.com/mitchellh/cli"
compilerlib "github.com/protoconf/protoconf/compiler/lib"
"github.com/protoconf/protoconf/consts"
"go.starlark.net/repl"
"go.starlark.net/starlark"
"golang.org/x/sync/errgroup"
)
type cliCommand struct{}
type cliConfig struct {
repl bool
verboseLogging bool
processTemplates bool
cpuprofile string
memprofile string
}
func newFlagSet() (*flag.FlagSet, *cliConfig) {
flags := flag.NewFlagSet("", flag.ExitOnError)
flags.Usage = func() {
fmt.Fprintln(flags.Output(), "Usage: [OPTION]... protoconf_root [config]...")
flags.PrintDefaults()
}
config := &cliConfig{}
flags.BoolVar(&config.repl, "repl", false, "Interactive REPL mode")
flags.BoolVar(&config.verboseLogging, "V", false, "Verbose logging")
flags.BoolVar(&config.processTemplates, "process-templates", false, "Process template files")
flags.StringVar(&config.cpuprofile, "cpuprofile", "", "Write cpu profiling info to this file")
flags.StringVar(&config.memprofile, "memprofile", "", "Write memory profiling info to this file")
return flags, config
}
func (c *cliCommand) Run(args []string) int {
flags, config := newFlagSet()
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
flags.Parse(args)
if flags.NArg() < 1 {
flags.Usage()
return 1
}
if config.cpuprofile != "" {
f, err := os.Create(config.cpuprofile)
if err != nil {
log.Fatal("Could not create CPU profile:", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("Could not start CPU profile:", err)
}
defer pprof.StopCPUProfile()
}
protoconfRoot := strings.TrimSpace(flags.Args()[0])
compiler := compilerlib.NewCompiler(protoconfRoot, config.verboseLogging)
if config.repl {
REPL(compiler)
return 0
}
if config.processTemplates {
if err := findTemplateFilesAndProccess(); err != nil {
log.Fatal(err)
}
}
var configs []string
if flags.NArg() == 1 {
var err error
configs, err = getAllConfigs(protoconfRoot)
if err != nil {
log.Printf("Error getting all configs from %s, err=%s", protoconfRoot, err)
return 1
}
} else {
configs = flags.Args()[1:]
}
g, _ := errgroup.WithContext(context.Background())
for _, config := range configs {
filename := strings.TrimSpace(config)
g.Go(func() error {
err := compiler.CompileFile(filename)
if err != nil {
ui.Error(fmt.Sprintf("Error compiling config %s:\n %s", filename, err))
}
return err
})
}
err := g.Wait()
if err != nil {
// log.Println(err)
return 1
}
if config.memprofile != "" {
f, err := os.Create(config.memprofile)
if err != nil {
log.Fatal("Could not create memory profile:", err)
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("Could not start memory profile:", err)
}
}
return 0
}
func (c *cliCommand) Help() string {
var b bytes.Buffer
b.WriteString(c.Synopsis())
b.WriteString("\n")
flags, _ := newFlagSet()
flags.SetOutput(&b)
flags.Usage()
return b.String()
}
func (c *cliCommand) Synopsis() string {
return "Compile configs"
}
// Command is a cli.CommandFactory
func Command() (cli.Command, error) {
return &cliCommand{}, nil
}
func getAllConfigs(protoconfRoot string) ([]string, error) {
srcDir, err := filepath.Abs(filepath.Join(protoconfRoot, consts.SrcPath))
if err != nil {
return nil, err
}
var configs []string
err = filepath.Walk(srcDir, func(path string, f os.FileInfo, err error) error {
ext := filepath.Ext(path)
if ext == consts.ConfigExtension || ext == consts.MultiConfigExtension {
configs = append(configs, strings.TrimPrefix(path, srcDir))
}
return nil
})
if err != nil {
return nil, err
}
return configs, nil
}
func REPL(c *compilerlib.Compiler) {
fmt.Printf("Protoconf %s\n", consts.Version)
loader := c.GetLoader()
thread := &starlark.Thread{
Load: loader.Load,
}
repl.REPL(thread, loader.Modules)
}