generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmd_config.go
147 lines (128 loc) 路 3.38 KB
/
cmd_config.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
cf "github.com/TBD54566975/ftl/common/configuration"
)
type configCmd struct {
cf.DefaultConfigMixin
List configListCmd `cmd:"" help:"List configuration."`
Get configGetCmd `cmd:"" help:"Get a configuration value."`
Set configSetCmd `cmd:"" help:"Set a configuration value."`
Unset configUnsetCmd `cmd:"" help:"Unset a configuration value."`
}
func (s *configCmd) Help() string {
return `
Configuration values are used to store non-sensitive information such as URLs,
etc.
`
}
type configListCmd struct {
Values bool `help:"List configuration values."`
Module string `optional:"" arg:"" placeholder:"MODULE" help:"List configuration only in this module."`
}
func (s *configListCmd) Run(ctx context.Context, scmd *configCmd, cr cf.Resolver[cf.Configuration]) error {
sm, err := scmd.NewConfigurationManager(ctx, cr)
if err != nil {
return err
}
listing, err := sm.List(ctx)
if err != nil {
return err
}
for _, config := range listing {
module, ok := config.Module.Get()
if s.Module != "" && module != s.Module {
continue
}
if ok {
fmt.Printf("%s.%s", module, config.Name)
} else {
fmt.Print(config.Name)
}
if s.Values {
var value any
err := sm.Get(ctx, config.Ref, &value)
if err != nil {
fmt.Printf(" (error: %s)\n", err)
} else {
data, _ := json.Marshal(value)
fmt.Printf(" = %s\n", data)
}
} else {
fmt.Println()
}
}
return nil
}
type configGetCmd struct {
Ref cf.Ref `arg:"" help:"Configuration reference in the form [<module>.]<name>."`
}
func (s *configGetCmd) Help() string {
return `
Returns a JSON-encoded configuration value.
`
}
func (s *configGetCmd) Run(ctx context.Context, scmd *configCmd, cr cf.Resolver[cf.Configuration]) error {
sm, err := scmd.NewConfigurationManager(ctx, cr)
if err != nil {
return err
}
var value any
err = sm.Get(ctx, s.Ref, &value)
if err != nil {
return err
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
err = enc.Encode(value)
if err != nil {
return fmt.Errorf("%s: %w", s.Ref, err)
}
return nil
}
type configSetCmd struct {
JSON bool `help:"Assume input value is JSON."`
Ref cf.Ref `arg:"" help:"Configuration reference in the form [<module>.]<name>."`
Value *string `arg:"" placeholder:"VALUE" help:"Configuration value (read from stdin if omitted)." optional:""`
}
func (s *configSetCmd) Run(ctx context.Context, scmd *configCmd, cr cf.Resolver[cf.Configuration]) error {
sm, err := scmd.NewConfigurationManager(ctx, cr)
if err != nil {
return err
}
if err := sm.Mutable(); err != nil {
return err
}
var config []byte
if s.Value != nil {
config = []byte(*s.Value)
} else {
config, err = io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read config from stdin: %w", err)
}
}
var configValue any
if s.JSON {
if err := json.Unmarshal(config, &configValue); err != nil {
return fmt.Errorf("config is not valid JSON: %w", err)
}
} else {
configValue = string(config)
}
return sm.Set(ctx, s.Ref, configValue)
}
type configUnsetCmd struct {
Ref cf.Ref `arg:"" help:"Configuration reference in the form [<module>.]<name>."`
}
func (s *configUnsetCmd) Run(ctx context.Context, scmd *configCmd, cr cf.Resolver[cf.Configuration]) error {
sm, err := scmd.NewConfigurationManager(ctx, cr)
if err != nil {
return err
}
return sm.Unset(ctx, s.Ref)
}