-
Notifications
You must be signed in to change notification settings - Fork 175
/
get_config.go
57 lines (46 loc) · 1.57 KB
/
get_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
package common
import (
"context"
"github.com/onflow/flow-go/admin"
"github.com/onflow/flow-go/admin/commands"
"github.com/onflow/flow-go/module/updatable_configs"
)
var _ commands.AdminCommand = (*GetConfigCommand)(nil)
// GetConfigCommand is an admin command which retrieves the current value of a
// dynamically updatable config.
type GetConfigCommand struct {
configs *updatable_configs.Manager
}
func NewGetConfigCommand(configs *updatable_configs.Manager) *GetConfigCommand {
return &GetConfigCommand{
configs: configs,
}
}
// validatedGetConfigData represents a validated get-config request,
// and includes the requested config field.
type validatedGetConfigData struct {
field updatable_configs.Field
}
func (s *GetConfigCommand) Handler(_ context.Context, req *admin.CommandRequest) (interface{}, error) {
validatedReq := req.ValidatorData.(validatedGetConfigData)
curValue := validatedReq.field.Get()
return curValue, nil
}
// Validator validates the request.
// Returns admin.InvalidAdminReqError for invalid/malformed requests.
func (s *GetConfigCommand) Validator(req *admin.CommandRequest) error {
configName, ok := req.Data.(string)
if !ok {
return admin.NewInvalidAdminReqFormatError("the data field must be a string")
}
field, ok := s.configs.GetField(configName)
if !ok {
return admin.NewInvalidAdminReqErrorf("unknown config field: %s", configName)
}
// we have found a corresponding updatable config field, set it in the ValidatorData
// field - we will read it in the Handler
req.ValidatorData = validatedGetConfigData{
field: field,
}
return nil
}