-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfigkingpin.go
More file actions
138 lines (110 loc) · 2.66 KB
/
Copy pathfigkingpin.go
File metadata and controls
138 lines (110 loc) · 2.66 KB
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
package figkingpin
import (
"encoding/json"
"fmt"
"os"
"gopkg.in/alecthomas/kingpin.v2"
)
func figFlagJson(model *kingpin.FlagModel) any {
var flag = map[string]interface{}{}
if model.Short != 0 {
flag["name"] = []string{fmt.Sprintf("--%s", model.Name), fmt.Sprintf("-%c", model.Short)}
} else {
flag["name"] = fmt.Sprintf("--%s", model.Name)
}
if model.Help != "" {
flag["description"] = model.Help
}
if model.Required {
flag["isRequired"] = model.Required
}
if model.Hidden {
flag["hidden"] = model.Hidden
}
if !model.IsBoolFlag() {
flag["args"] = map[string]interface{}{
"name": "ARG",
}
flag["requiresEquals"] = true
}
return flag
}
func figFlagsJson(models []*kingpin.FlagModel) []any {
var flags []any
for _, model := range models {
flags = append(flags, figFlagJson(model))
}
return flags
}
func figArgJson(model *kingpin.ArgModel) any {
var arg = map[string]interface{}{
"name": model.Name,
}
if model.Help != "" {
arg["description"] = model.Help
}
if model.Required {
arg["isRequired"] = model.Required
}
return arg
}
func figArgsJson(models []*kingpin.ArgModel) any {
if len(models) == 1 {
return figArgJson(models[0])
} else {
var args []any
for _, model := range models {
args = append(args, figArgJson(model))
}
return args
}
}
func figSpecJson(model *kingpin.ApplicationModel) any {
return map[string]interface{}{
"name": model.Name,
"description": model.Help,
"subcommands": figSubcommandsJson(model.Commands),
"options": figFlagsJson(model.Flags),
}
}
func figSubcommandJson(model *kingpin.CmdModel) any {
var subcommand = map[string]interface{}{
"name": model.Name,
}
if model.Help != "" {
subcommand["description"] = model.Help
}
if model.Hidden {
subcommand["hidden"] = model.Hidden
}
if len(model.Commands) > 0 {
subcommand["subcommands"] = figSubcommandsJson(model.Commands)
}
if len(model.Flags) > 0 {
subcommand["options"] = figFlagsJson(model.Flags)
}
if len(model.Args) > 0 {
subcommand["args"] = figArgsJson(model.Args)
}
return subcommand
}
func figSubcommandsJson(models []*kingpin.CmdModel) []any {
var subcommands []any
for _, model := range models {
subcommands = append(subcommands, figSubcommandJson(model))
}
return subcommands
}
func GenerateFigCompletionSpec(a *kingpin.Application) func(c *kingpin.ParseContext) error {
return func(c *kingpin.ParseContext) error {
var marsheledJson, err = json.MarshalIndent(figSpecJson(a.Model()), "", " ")
if err != nil {
return err
}
print("const completionSpec: Fig.Spec = ")
print(string(marsheledJson))
print(";\n\nexport default completionSpec;\n")
os.Exit(0)
return nil
}
}