-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkpbcopy.go
65 lines (53 loc) · 1.25 KB
/
mkpbcopy.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
package main
import (
"flag"
"fmt"
"os"
"github.com/debspencer/pbcopy/pbcopier"
)
func main() {
structName := flag.String("m", "", "Base protobuf message to create copy method")
outFile := flag.String("o", "", "Output file (defaults to <Base function>.go")
// process command line
flag.Parse()
args := flag.Args()
if len(*structName) == 0 || len(args) != 1 {
fmt.Fprintf(flag.CommandLine.Output(), "Usage %s -m message [-o output] <file>.pb.go\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
pbgo := args[0]
pbreader, err := pbcopier.NewPbReader(pbgo)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(-1)
}
var output string
if len(*outFile) > 0 {
output = *outFile
} else {
output = *structName + ".go"
}
packageName, err := pbreader.FindPackage()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(-1)
}
pbwriter, err := pbcopier.NewPbWriter(output, packageName)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(-1)
}
pbCopier := pbcopier.NewPbCopier(pbreader, pbwriter)
err = pbCopier.MkCopy(*structName)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(-1)
}
pbwriter.Flush()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(-1)
}
os.Exit(0)
}