/
main.go
68 lines (53 loc) · 1.45 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
package main
import (
"flag"
"fmt"
"os"
"github.com/jesselucas/executil"
)
// Semantic Version
const Version = "0.0.3"
const space = " " // Used for pretty printing
const br = "\n\n"
func main() {
// Check for our command line configuration flags
var (
versionUsage = "Prints current version" + " (v. " + Version + ")"
versionPtr = flag.Bool("version", false, versionUsage)
)
// Set up short hand flags
flag.BoolVar(versionPtr, "v", false, versionUsage+" (shorthand)")
flag.Parse()
if *versionPtr {
fmt.Println(Version)
os.Exit(0)
}
// usage description
usage := "protogen /path/to/myService.proto"
options := "--version (-v)"
optionsDesc := versionUsage
// Create help message with usage messaging
helpMessage := fmt.Sprintf(bold("USAGE:")+"\n%s%v", space, usage)
// Break between messages
helpMessage += br
// Add options messaging
helpMessage += fmt.Sprintf(bold("OPTIONS:")+"\n%v\n%s%v", options, space, optionsDesc)
// Check arg for appname to load
if len(os.Args) != 2 {
fmt.Println(helpMessage)
os.Exit(0)
}
protoPath := os.Args[1]
// run protoc command (protoc --go_out=plugins=grpc:. $proto)
cmd := executil.Command("protoc", "--go_out=plugins=grpc:.", protoPath)
cmd.OutputPrefix = "protoc"
err := cmd.StartAndWait()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(bold("SUCCESS: ") + protoPath + " pb.go successfully created.")
}
func bold(s string) string {
return "\033[1m" + s + "\033[0m"
}