forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_help.go
127 lines (106 loc) · 3.1 KB
/
config_help.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
package main
import (
"context"
"fmt"
"reflect"
"strings"
"unicode"
"github.com/gnolang/gno/tm2/pkg/commands"
)
type metadataHelperGenerator struct {
// Optional callback to edit metadata
MetaUpdate func(meta *commands.Metadata, inputType string)
// Tag to select for name, if empty will use the field Name
TagNameSelector string
// Will display description with tree representation
TreeDisplay bool
}
// generateSubCommandHelper generates subcommands based on `s` structure fields and their respective tag descriptions
func generateSubCommandHelper(gen metadataHelperGenerator, s any, exec commands.ExecMethod) []*commands.Command {
rv := reflect.ValueOf(s)
metas := gen.generateFields(rv, "", 0)
cmds := make([]*commands.Command, len(metas))
for i := 0; i < len(metas); i++ {
meta := metas[i]
exec := func(ctx context.Context, args []string) error {
args = append([]string{meta.Name}, args...)
return exec(ctx, args)
}
cmds[i] = commands.NewCommand(meta, nil, exec)
}
return cmds
}
func (g *metadataHelperGenerator) generateFields(rv reflect.Value, parent string, depth int) []commands.Metadata {
if parent != "" {
parent += "."
}
// Unwrap pointer if needed
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
// Create a new non-nil instance of the original type that was nil
rv = reflect.New(rv.Type().Elem())
}
rv = rv.Elem() // Dereference to struct value
}
metas := []commands.Metadata{}
if rv.Kind() != reflect.Struct {
return metas
}
rt := rv.Type()
for i := 0; i < rv.NumField(); i++ {
field := rt.Field(i)
if !field.IsExported() {
continue
}
fieldValue := rv.Field(i)
name := field.Name
// Get JSON tag name
if g.TagNameSelector != "" {
name, _, _ = strings.Cut(field.Tag.Get(g.TagNameSelector), ",")
if name == "" || name == "-" {
continue
}
}
// Recursive call for nested struct
var childs []commands.Metadata
if k := fieldValue.Kind(); k == reflect.Ptr || k == reflect.Struct {
childs = g.generateFields(fieldValue, name, depth+1)
}
// Generate metadata
var meta commands.Metadata
// Name
meta.Name = parent + name
// Create a tree-like display to see nested field
if g.TreeDisplay && depth > 0 {
meta.ShortHelp += strings.Repeat(" ", depth*2)
if i == rv.NumField()-1 {
meta.ShortHelp += "└─"
} else {
meta.ShortHelp += "├─"
}
}
meta.ShortHelp += fmt.Sprintf("<%s>", field.Type)
// Get Short/Long Help Message from comment tag
comment := field.Tag.Get("comment")
comment = strings.TrimFunc(comment, func(r rune) bool {
return unicode.IsSpace(r) || r == '#'
})
if comment != "" {
// Use the first line as short help
meta.ShortHelp += " "
meta.ShortHelp += strings.Split(comment, "\n")[0]
// Display full comment as Long Help
meta.LongHelp = comment
} else {
// If the comment is empty, it mostly means that there is no help.
// Use a blank space to avoid falling back on short help.
meta.LongHelp = " "
}
if g.MetaUpdate != nil {
g.MetaUpdate(&meta, field.Type.String())
}
metas = append(metas, meta)
metas = append(metas, childs...)
}
return metas
}