-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (183 loc) · 3.97 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"time"
)
//go:generate goclap -type goclap
// Pre-build tool to generate command line argument parsing code from Go comments.
type goclap struct {
// The root command struct name.
//
// clap:opt type
rootCmdType string
// Directory of source files to parse (default ".").
//
// clap:opt srcdir
srcDir string
// Include goclap's version info in the generated code.
//
// clap:opt with-version
withVersion bool
// Output file path (default "./clap.gen.go").
//
// clap:opt out
outFilePath string
// How the usage message for each command will be structured (possible values: packed
// or roomy).
//
// clap:opt usg-layout-kind
usgLayoutKind string
// Max width for lines of text in the usage message.
//
// clap:opt usg-text-width
usgTextWidth int
// Print version info and exit.
//
// clap:opt version
version bool
}
type basicType string
func (t basicType) IsBool() bool { return t == "bool" }
type buildVersionInfo struct {
modVersion string
commitHash string
commitDate string
hasLocalChanges bool
}
func getBuildVersionInfo() buildVersionInfo {
bi, ok := debug.ReadBuildInfo()
if !ok {
warn("unable to read build info")
return buildVersionInfo{}
}
v := buildVersionInfo{
modVersion: bi.Main.Version,
}
for _, s := range bi.Settings {
switch s.Key {
case "vcs.revision":
v.commitHash = s.Value
case "vcs.time":
t, err := time.Parse(time.RFC3339, s.Value)
if err != nil {
warn("unable to parse vcs.time '%s': %v", s.Value, err)
} else {
v.commitDate = t.Format("20060102")
}
case "vcs.modified":
v.hasLocalChanges = (s.Value == "true")
}
}
return v
}
func (v buildVersionInfo) String() string {
hash := v.commitHash
if len(hash) > 12 {
hash = v.commitHash[:12]
}
s := v.modVersion
if v.commitDate != "" {
s += "-" + v.commitDate
}
if hash != "" {
s += "-" + hash
}
if v.hasLocalChanges {
s += "-(with unstaged changes)"
}
return s
}
type clapData struct {
Blurb string
overview []string // paragraphs
configs []clapConfig
}
type clapConfig struct {
key string
val string
}
func (d *clapData) getConfig(k string) (string, bool) {
for i := range d.configs {
if k == d.configs[i].key {
return d.configs[i].val, true
}
}
return "", false
}
type command struct {
IsRoot bool
parentNames []string
FieldName string
TypeName string
Data clapData
Opts []option
Args []argument
Subcmds []command
}
type option struct {
FieldType basicType
FieldName string
Name string
data clapData
}
type argument struct {
FieldType basicType
FieldName string
name string
data clapData
}
func (c *command) UsgName() string {
if cfgName, ok := c.Data.getConfig("cmd_name"); ok {
return cfgName
}
return strings.ToLower(c.FieldName)
}
func gen(c *goclap) error {
if c.usgTextWidth == 0 {
c.usgTextWidth = 90
}
rootCmdTypeName := c.rootCmdType
if rootCmdTypeName == "" {
fmt.Fprintf(os.Stderr, "no root command type provided\n")
fmt.Fprintf(os.Stderr, "%s\n", c.UsageHelp())
os.Exit(1)
}
rootCmd, pkgName, err := parse(c.srcDir, rootCmdTypeName)
if err != nil {
return err
}
code, err := generate(c.withVersion, pkgName, c.usgTextWidth, c.usgLayoutKind, &rootCmd)
if err != nil {
return err
}
if c.outFilePath == "" {
c.outFilePath = "./clap.gen.go"
}
f, err := os.OpenFile(c.outFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return fmt.Errorf("opening '%s': %w", c.outFilePath, err)
}
defer f.Close()
if _, err = f.Write(code); err != nil {
return fmt.Errorf("writing code to output file: %w", err)
}
return nil
}
func warn(format string, a ...any) {
fmt.Fprintf(os.Stderr, "\033[1;33mwarning:\033[0m "+format+"\n", a...)
}
func main() {
c := goclap{}
c.Parse(os.Args[1:])
if c.version {
fmt.Println(getBuildVersionInfo())
return
}
if err := gen(&c); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}