forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_get.go
84 lines (69 loc) · 1.95 KB
/
config_get.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/commands"
)
var errInvalidConfigGetArgs = errors.New("invalid number of config get arguments provided")
type configGetCfg struct {
configCfg
raw bool
}
// newConfigGetCmd creates the config get command
func newConfigGetCmd(io commands.IO) *commands.Command {
cfg := &configGetCfg{}
cmd := commands.NewCommand(
commands.Metadata{
Name: "get",
ShortUsage: "config get [flags] [<key>]",
ShortHelp: "shows the Gno node configuration",
LongHelp: "Shows the Gno node configuration at the given path " +
"by fetching the option specified at <key>",
},
cfg,
func(_ context.Context, args []string) error {
return execConfigGet(cfg, io, args)
},
)
// Add subcommand helpers
helperGen := metadataHelperGenerator{
MetaUpdate: func(meta *commands.Metadata, inputType string) {
meta.ShortUsage = fmt.Sprintf("config get %s <%s>", meta.Name, inputType)
},
TagNameSelector: "json",
TreeDisplay: true,
}
subs := generateSubCommandHelper(helperGen, config.Config{}, func(_ context.Context, args []string) error {
return execConfigGet(cfg, io, args)
})
cmd.AddSubCommands(subs...)
return cmd
}
func (c *configGetCfg) RegisterFlags(fs *flag.FlagSet) {
c.configCfg.RegisterFlags(fs)
fs.BoolVar(
&c.raw,
"raw",
false,
"output raw string values, rather than as JSON strings",
)
}
func execConfigGet(cfg *configGetCfg, io commands.IO, args []string) error {
// Load the config
loadedCfg, err := config.LoadConfigFile(cfg.configPath)
if err != nil {
return fmt.Errorf("%s, %w", tryConfigInit, err)
}
// Make sure the get arguments are valid
if len(args) > 1 {
return errInvalidConfigGetArgs
}
// Find and print the config field, if any
if err := printKeyValue(loadedCfg, cfg.raw, io, args...); err != nil {
return fmt.Errorf("unable to get config field, %w", err)
}
return nil
}