-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (133 loc) · 3.86 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
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
package main
import (
"fmt"
"os"
"github.com/gunk/gunk/convert"
"github.com/gunk/gunk/dump"
"github.com/gunk/gunk/format"
"github.com/gunk/gunk/generate"
"github.com/gunk/gunk/generate/downloader"
"github.com/gunk/gunk/log"
"github.com/gunk/gunk/vetconfig"
"github.com/spf13/cobra"
)
var version = "v0.8.7"
func main() {
os.Exit(run())
}
func run() int {
app := cobra.Command{
Use: "gunk",
Short: "The modern frontend and syntax for Protocol Buffers.",
Version: version,
}
// version commmand
ver := &cobra.Command{
Use: "version",
Short: "Print the version number of gundk",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stdout, "gunk", version)
},
}
app.AddCommand(ver)
// generate command
gen := &cobra.Command{
Use: "generate [patterns]",
Short: "Generate code from Gunk packages",
RunE: func(cmd *cobra.Command, args []string) error {
return generate.Run("", args...)
},
}
gen.Flags().BoolVarP(&log.PrintCommands, "print-commands", "x", false, "Print the commands")
gen.Flags().BoolVarP(&log.Verbose, "verbose", "v", false, "Print the names of packages are they are generated")
app.AddCommand(gen)
// convert command
var overwrite bool
conv := &cobra.Command{
Use: "convert [-overwrite] [file | directory]...",
Short: "Convert Proto file to Gunk file.",
RunE: func(cmd *cobra.Command, args []string) error {
return convert.Run(args, overwrite)
},
}
conv.Flags().BoolVarP(&overwrite, "overwrite", "w", false, "Overwrite the converted Gunk file if it exists.")
// format command
app.AddCommand(conv)
frmt := &cobra.Command{
Use: "format [patterns]",
Short: "Format Gunk code",
RunE: func(cmd *cobra.Command, args []string) error {
return format.Run("", args...)
},
}
app.AddCommand(frmt)
// dump command
var dmpFormat string
dmp := &cobra.Command{
Use: "dump [patterns]",
Short: "Write a FileDescriptorSet, defined in descriptor.proto",
RunE: func(cmd *cobra.Command, args []string) error {
return dump.Run(dmpFormat, "", args...)
},
}
dmp.Flags().StringVarP(&dmpFormat, "format", "f", "proto", "output format: [proto | json]")
app.AddCommand(dmp)
// download list
// TODO(hhhapz): add protoc-java, and protoc-ts, etc.
downloadSubcommands := []func() error{
func() error { return downloadProtoc("", "") },
}
// download command
download := cobra.Command{
Use: "download [protoc | protoc]",
Short: "Download the necessary tools for Gunk",
}
download.Flags().BoolVarP(&log.Verbose, "verbose", "v", false, "Print details of downloaded tools")
dlAll := cobra.Command{
Use: "all",
Short: "Download all required tools for Gunk, e.g., protoc",
RunE: func(cmd *cobra.Command, args []string) error {
for _, f := range downloadSubcommands {
if err := f(); err != nil {
return err
}
}
return nil
},
}
var dlProtocPath, dlProtocVer string
dlProtoc := cobra.Command{
Use: "protoc",
Short: "Download protoc",
RunE: func(cmd *cobra.Command, args []string) error {
return downloadProtoc(dlProtocPath, dlProtocVer)
},
}
dlProtoc.Flags().StringVar(&dlProtocPath, "path", "", "Path to check for protoc binary, or where to download it to")
dlProtoc.Flags().BoolVarP(&log.Verbose, "verbose", "v", false, "Print details of download tools")
dlProtoc.Flags().StringVar(&dlProtocVer, "version", "", "Version of protoc to use")
download.AddCommand(&dlAll, &dlProtoc)
app.AddCommand(&download)
// vet command
vet := cobra.Command{
Use: "vet [path]",
Short: "Vet gunk config files",
RunE: func(cmd *cobra.Command, args []string) error {
path := "."
if len(args) > 0 {
path = args[0]
}
return vetconfig.Run(path)
},
}
app.AddCommand(&vet)
// run app
if err := app.Execute(); err != nil {
return 1
}
return 0
}
func downloadProtoc(path, version string) error {
_, err := downloader.CheckOrDownloadProtoc(path, version)
return err
}