-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (47 loc) · 1.28 KB
/
main.go
File metadata and controls
52 lines (47 loc) · 1.28 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
package main
import (
"flag"
"fmt"
"strings"
"github.com/go-sphere/protoc-gen-sphere-errors/generate/errors"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
)
const (
defaultErrorsPackage = "github.com/go-sphere/httpx"
)
var (
showVersion = flag.Bool("version", false, "print the version and exit")
newErrorsFunc = flag.String("new_errors_func", defaultErrorsPackage+";NewError", "new errors func, must be func(status, code int32, message string, err error) error")
)
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("protoc-gen-sphere-errors %v\n", "0.0.1")
return
}
protogen.Options{
ParamFunc: flag.CommandLine.Set,
}.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
for _, f := range gen.Files {
if !f.Generate {
continue
}
errPkg := strings.Split(*newErrorsFunc, ";")
if len(errPkg) != 2 {
return fmt.Errorf("invalid new_errors_func format, expected 'path;ident'")
}
_, gErr := errors.GenerateFile(gen, f, &errors.Config{
NewErrorsFunc: protogen.GoIdent{
GoName: errPkg[1],
GoImportPath: protogen.GoImportPath(errPkg[0]),
},
})
if gErr != nil {
return gErr
}
}
return nil
})
}