github.com/koko1123/flow-go-1@v0.29.6/admin/commands/common/get_config.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/koko1123/flow-go-1/admin"
     7  	"github.com/koko1123/flow-go-1/admin/commands"
     8  	"github.com/koko1123/flow-go-1/module/updatable_configs"
     9  )
    10  
    11  var _ commands.AdminCommand = (*GetConfigCommand)(nil)
    12  
    13  // GetConfigCommand is an admin command which retrieves the current value of a
    14  // dynamically updatable config.
    15  type GetConfigCommand struct {
    16  	configs *updatable_configs.Manager
    17  }
    18  
    19  func NewGetConfigCommand(configs *updatable_configs.Manager) *GetConfigCommand {
    20  	return &GetConfigCommand{
    21  		configs: configs,
    22  	}
    23  }
    24  
    25  // validatedGetConfigData represents a validated get-config request,
    26  // and includes the requested config field.
    27  type validatedGetConfigData struct {
    28  	field updatable_configs.Field
    29  }
    30  
    31  func (s *GetConfigCommand) Handler(_ context.Context, req *admin.CommandRequest) (interface{}, error) {
    32  	validatedReq := req.ValidatorData.(validatedGetConfigData)
    33  	curValue := validatedReq.field.Get()
    34  	return curValue, nil
    35  }
    36  
    37  // Validator validates the request.
    38  // Returns admin.InvalidAdminReqError for invalid/malformed requests.
    39  func (s *GetConfigCommand) Validator(req *admin.CommandRequest) error {
    40  	configName, ok := req.Data.(string)
    41  	if !ok {
    42  		return admin.NewInvalidAdminReqFormatError("the data field must be a string")
    43  	}
    44  
    45  	field, ok := s.configs.GetField(configName)
    46  	if !ok {
    47  		return admin.NewInvalidAdminReqErrorf("unknown config field: %s", configName)
    48  	}
    49  
    50  	// we have found a corresponding updatable config field, set it in the ValidatorData
    51  	// field - we will read it in the Handler
    52  	req.ValidatorData = validatedGetConfigData{
    53  		field: field,
    54  	}
    55  
    56  	return nil
    57  }