-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
161 lines (155 loc) · 4.89 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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/lint"
"github.com/gunk/gunk/log"
"github.com/gunk/gunk/vetconfig"
"github.com/spf13/cobra"
)
var version = "v0.12.1"
func main() {
if err := run(); err != nil {
os.Exit(1)
}
}
func run() error {
app := cobra.Command{
Use: "gunk",
Short: "The modern frontend and syntax for Protocol Buffers.",
Version: version,
SilenceUsage: true,
}
app.SetFlagErrorFunc(func(c *cobra.Command, e error) error {
return fmt.Errorf("%v\nRun '%s --help' for usage.", e, c.CommandPath())
})
// versionCmd commmand
versionCmd := &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(versionCmd)
// generate command
generateCmd := &cobra.Command{
Use: "generate [patterns]",
Short: "Generate code from Gunk packages",
RunE: func(cmd *cobra.Command, args []string) error {
return generate.Run("", args...)
},
}
generateCmd.Flags().BoolVarP(&log.PrintCommands, "print-commands", "x", false, "Print the commands")
generateCmd.Flags().BoolVarP(&log.Verbose, "verbose", "v", false, "Print the names of packages are they are generated")
app.AddCommand(generateCmd)
// convert command
var overwrite bool
convertCmd := &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)
},
}
convertCmd.Flags().BoolVarP(&overwrite, "overwrite", "w", false, "Overwrite the converted Gunk file if it exists.")
app.AddCommand(convertCmd)
// format command
formatCmd := &cobra.Command{
Use: "format [patterns]",
Short: "Format Gunk code",
RunE: func(cmd *cobra.Command, args []string) error {
return format.Run("", args...)
},
}
app.AddCommand(formatCmd)
// dump command
var dumpFormat string
dump := &cobra.Command{
Use: "dump [patterns]",
Short: "Write a FileDescriptorSet, defined in descriptor.proto",
RunE: func(cmd *cobra.Command, args []string) error {
return dump.Run(dumpFormat, "", args...)
},
}
dump.Flags().StringVarP(&dumpFormat, "format", "f", "proto", "output format: [proto | json]")
app.AddCommand(dump)
// download list
// TODO(hhhapz): add protoc-java, and protoc-ts, etc.
downloadSubcommands := []func(string, string) error{
downloadProtoc,
}
// download command
downloadCmd := cobra.Command{
Use: "download [protoc | protoc]",
Short: "Download the necessary tools for Gunk",
}
downloadCmd.Flags().BoolVarP(&log.Verbose, "verbose", "v", false, "Print details of downloaded tools")
downloadAllCmd := 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
},
}
// download proto command
var dlProtocPath, dlProtocVer string
downloadProtocCmd := cobra.Command{
Use: "protoc",
Short: "Download protoc",
RunE: func(cmd *cobra.Command, args []string) error {
return downloadProtoc(dlProtocPath, dlProtocVer)
},
}
downloadProtocCmd.Flags().StringVar(&dlProtocPath, "path", "", "Path to check for protoc binary, or where to download it to")
downloadProtocCmd.Flags().BoolVarP(&log.Verbose, "verbose", "v", false, "Print details of download tools")
downloadProtocCmd.Flags().StringVar(&dlProtocVer, "version", "", "Version of protoc to use")
downloadCmd.AddCommand(&downloadAllCmd, &downloadProtocCmd)
app.AddCommand(&downloadCmd)
// vet command
vetCmd := 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(&vetCmd)
// lint command
var enableLint, disableLint string
var listLinters bool
lintCmd := cobra.Command{
Use: "lint [patterns]",
Short: "Lint a set of Gunk files",
RunE: func(cmd *cobra.Command, args []string) error {
if listLinters {
lint.PrintLinters()
return nil
}
return lint.Run("", enableLint, disableLint, args...)
},
}
lintCmd.Flags().StringVar(&enableLint, "enable", "", "Linters to enable (all if empty) separated by comma")
lintCmd.Flags().StringVar(&disableLint, "disable", "", "Linters to disable separated by comma, overrides enable")
lintCmd.Flags().BoolVarP(&listLinters, "list", "l", false, "List all linters and exit")
app.AddCommand(&lintCmd)
return app.Execute()
}
func downloadProtoc(path, version string) error {
_, err := downloader.CheckOrDownloadProtoc(path, version)
return err
}