diff --git a/cfg/client.go b/cfg/client.go deleted file mode 100644 index 196b203..0000000 --- a/cfg/client.go +++ /dev/null @@ -1,140 +0,0 @@ -package cfg - -import ( - "context" - "time" - - "github.com/pkg/errors" - "github.com/txix-open/isp-kit/grpc/client" -) - -type ConfigClient struct { - cli *client.Client - address string -} - -func NewConfigClient() *ConfigClient { - return &ConfigClient{} -} - -func (c *ConfigClient) ReceiveConfig(address string) error { - cli, err := client.Default() - if err != nil { - return err - } - c.cli = cli - cli.Upgrade([]string{address}) - - c.address = address - return nil -} - -func (c *ConfigClient) GetAvailableConfigs() ([]ModuleInfo, error) { - response := make([]ModuleInfo, 0) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(getAvailableConfigs). - JsonResponseBody(&response). - Do(ctx) - if err != nil { - return nil, errors.WithMessagef(err, "call %s", getAvailableConfigs) - } - return response, nil -} - -func (c *ConfigClient) GetConfigByModuleName(name string) (*Config, error) { - request := &getModuleByUuidAndNameRequest{ModuleName: name} - response := new(Config) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(getConfigByModuleName). - JsonRequestBody(request). - JsonResponseBody(&response). - Do(ctx) - if err != nil { - return nil, errors.WithMessagef(err, "call %s", getConfigByModuleName) - } - return response, nil -} - -func (c *ConfigClient) CreateUpdateConfig(request Config) (*Config, error) { - response := new(Config) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(createUpdateConfig). - JsonRequestBody(request). - JsonResponseBody(&response). - Do(ctx) - if err != nil { - return nil, errors.WithMessagef(err, "call %s", createUpdateConfig) - } - return response, nil -} - -func (c *ConfigClient) GetSchemaByModuleId(moduleId string) (*ConfigSchema, error) { - request := getSchemaByModuleIdRequest{ModuleId: moduleId} - response := new(ConfigSchema) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(getSchemaByModuleId). - JsonRequestBody(request). - JsonResponseBody(&response). - Do(ctx) - if err != nil { - return nil, errors.WithMessagef(err, "call %s", getSchemaByModuleId) - } - return response, nil -} - -func (c *ConfigClient) GetAllVariables() ([]Variable, error) { - response := make([]Variable, 0) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(getAllVariables). - JsonResponseBody(&response). - Do(ctx) - if err != nil { - return nil, errors.WithMessagef(err, "call %s", getAllVariables) - } - return response, nil -} - -func (c *ConfigClient) GetVariableByName(variableName string) (*Variable, error) { - request := VariableByNameRequest{Name: variableName} - response := new(Variable) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(getVariableByName). - JsonRequestBody(request). - JsonResponseBody(&response). - Do(ctx) - if err != nil { - return nil, errors.WithMessagef(err, "call %s", getVariableByName) - } - return response, nil -} - -func (c *ConfigClient) UpsertVariables(request []UpsertVariableRequest) error { - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(upsertVariable). - JsonRequestBody(request). - Do(ctx) - if err != nil { - return errors.WithMessagef(err, "call %s", upsertVariable) - } - return nil -} - -func (c *ConfigClient) DeleteVariable(variableName string) error { - request := VariableByNameRequest{Name: variableName} - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - err := c.cli.Invoke(deleteVariable). - JsonRequestBody(request). - Do(ctx) - if err != nil { - return errors.WithMessagef(err, "call %s", deleteVariable) - } - return nil -} diff --git a/cfg/method_name.go b/cfg/method_name.go deleted file mode 100644 index 01334cc..0000000 --- a/cfg/method_name.go +++ /dev/null @@ -1,13 +0,0 @@ -package cfg - -const ( - getAvailableConfigs = "config/module/get_modules_info" - getConfigByModuleName = "config/config/get_active_config_by_module_name" - createUpdateConfig = "config/config/create_update_config" - getSchemaByModuleId = "config/schema/get_by_module_id" - - getAllVariables = "config/variable/all" - getVariableByName = "config/variable/get_by_name" - upsertVariable = "config/variable/upsert" - deleteVariable = "config/variable/delete" -) diff --git a/command/commands.go b/command/commands.go new file mode 100644 index 0000000..da17a21 --- /dev/null +++ b/command/commands.go @@ -0,0 +1,16 @@ +package command + +import "github.com/urfave/cli/v2" + +func AllCommands() []*cli.Command { + return []*cli.Command{ + Status(), + Get(), + Set(), + Delete(), + Schema(), + Merge(), + GitGet(), + VariablesCommands(), + } +} diff --git a/command/utils/config.go b/command/utils/config.go index 74837c6..038160c 100644 --- a/command/utils/config.go +++ b/command/utils/config.go @@ -1,11 +1,11 @@ package utils import ( - "ispctl/cfg" + "ispctl/entity" "ispctl/service" ) -func CreateUpdateConfig(stringToChange string, configuration *cfg.Config) error { +func CreateUpdateConfig(stringToChange string, configuration *entity.Config) error { answer, err := service.Config.CreateUpdateConfig(stringToChange, configuration) if err != nil { return err diff --git a/command/vars.go b/command/vars.go index 14464a5..5102cd8 100644 --- a/command/vars.go +++ b/command/vars.go @@ -4,9 +4,9 @@ import ( "encoding/csv" "fmt" "ispctl/bash" - "ispctl/cfg" "ispctl/command/flag" "ispctl/command/utils" + "ispctl/entity" "ispctl/service" "os" "strings" @@ -106,12 +106,12 @@ func (g varsCommands) set(ctx *cli.Context) error { variableName := ctx.Args().Get(0) variableValue := ctx.Args().Get(1) - variableType := cfg.TextVariableType + variableType := entity.TextVariableType if ctx.Bool(flag.SetVariableSecretType.Name) { - variableType = cfg.SecretVariableType + variableType = entity.SecretVariableType } - return service.Config.UpsertVariables([]cfg.UpsertVariableRequest{{ + return service.Config.UpsertVariables([]entity.UpsertVariableRequest{{ Name: variableName, Value: variableValue, Type: variableType, @@ -141,7 +141,7 @@ func (g varsCommands) upload(ctx *cli.Context) error { return service.Config.UpsertVariables(variables) } -func (g varsCommands) readVariablesFromCsv(filepath string) ([]cfg.UpsertVariableRequest, error) { +func (g varsCommands) readVariablesFromCsv(filepath string) ([]entity.UpsertVariableRequest, error) { file, err := os.Open(filepath) if err != nil { return nil, fmt.Errorf("failed to open file %s: %w", filepath, err) @@ -157,15 +157,16 @@ func (g varsCommands) readVariablesFromCsv(filepath string) ([]cfg.UpsertVariabl if len(records) < 2 { return nil, fmt.Errorf("CSV file must have a header and at least one data row") } + header := records[0] columnIndexes := map[string]int{} for i, col := range header { columnIndexes[strings.ToLower(col)] = i } - variables := make([]cfg.UpsertVariableRequest, 0, len(records)-1) + variables := make([]entity.UpsertVariableRequest, 0, len(records)-1) for _, row := range records[1:] { - variables = append(variables, cfg.UpsertVariableRequest{ + variables = append(variables, entity.UpsertVariableRequest{ Name: row[columnIndexes["name"]], Type: row[columnIndexes["type"]], Value: row[columnIndexes["value"]], diff --git a/cfg/domain.go b/entity/config.go similarity index 91% rename from cfg/domain.go rename to entity/config.go index 565e726..dfe60b7 100644 --- a/cfg/domain.go +++ b/entity/config.go @@ -1,4 +1,4 @@ -package cfg +package entity import ( "time" @@ -7,12 +7,12 @@ import ( "github.com/txix-open/isp-kit/rc/schema" ) -type getModuleByUuidAndNameRequest struct { +type GetModuleByUuidAndNameRequest struct { ModuleName string Uuid string } -type getSchemaByModuleIdRequest struct { +type GetSchemaByModuleIdRequest struct { ModuleId string } diff --git a/cfg/variable.go b/entity/variable.go similarity index 97% rename from cfg/variable.go rename to entity/variable.go index ed00f25..3ca4987 100644 --- a/cfg/variable.go +++ b/entity/variable.go @@ -1,4 +1,4 @@ -package cfg +package entity const ( SecretVariableType = "SECRET" diff --git a/main.go b/main.go index 618c023..e8207b6 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,10 @@ import ( "log" "os" - "github.com/urfave/cli/v2" "ispctl/command" "ispctl/command/flag" + + "github.com/urfave/cli/v2" ) var version = "1.1.0" @@ -26,16 +27,7 @@ func initCommands() { flag.Unsafe, } app.EnableBashCompletion = true - app.Commands = []*cli.Command{ - command.Status(), - command.Get(), - command.Set(), - command.Delete(), - command.Schema(), - command.Merge(), - command.GitGet(), - command.VariablesCommands(), - } + app.Commands = command.AllCommands() app.BashComplete = func(context *cli.Context) { for _, appCommand := range app.Commands { fmt.Println(appCommand.Name) diff --git a/repository/client.go b/repository/client.go new file mode 100644 index 0000000..c3046fc --- /dev/null +++ b/repository/client.go @@ -0,0 +1,14 @@ +package repository + +import ( + "github.com/txix-open/isp-kit/grpc/client" +) + +func NewClientFromHost(address string) (*client.Client, error) { + cli, err := client.Default() + if err != nil { + return nil, err + } + cli.Upgrade([]string{address}) + return cli, nil +} diff --git a/repository/config.go b/repository/config.go new file mode 100644 index 0000000..7ca62f5 --- /dev/null +++ b/repository/config.go @@ -0,0 +1,135 @@ +package repository + +import ( + "context" + "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/client" + "ispctl/entity" + "time" +) + +const ( + getAvailableConfigs = "config/module/get_modules_info" + getConfigByModuleName = "config/config/get_active_config_by_module_name" + createUpdateConfig = "config/config/create_update_config" + getSchemaByModuleId = "config/schema/get_by_module_id" + + getAllVariables = "config/variable/all" + getVariableByName = "config/variable/get_by_name" + upsertVariable = "config/variable/upsert" + deleteVariable = "config/variable/delete" +) + +type Config struct { + cli *client.Client + baseCtx context.Context +} + +func NewConfig(cli *client.Client) Config { + return Config{ + cli: cli, + baseCtx: context.Background(), + } +} + +func (c Config) GetAvailableConfigs() ([]entity.ModuleInfo, error) { + response := make([]entity.ModuleInfo, 0) + err := c.cli.Invoke(getAvailableConfigs). + JsonResponseBody(&response). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return nil, errors.WithMessagef(err, "call %s", getAvailableConfigs) + } + return response, nil +} + +func (c Config) GetConfigByModuleName(name string) (*entity.Config, error) { + request := &entity.GetModuleByUuidAndNameRequest{ModuleName: name} + response := new(entity.Config) + err := c.cli.Invoke(getConfigByModuleName). + JsonRequestBody(request). + JsonResponseBody(&response). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return nil, errors.WithMessagef(err, "call %s", getConfigByModuleName) + } + return response, nil +} + +func (c Config) CreateUpdateConfig(request entity.Config) (*entity.Config, error) { + response := new(entity.Config) + err := c.cli.Invoke(createUpdateConfig). + JsonRequestBody(request). + JsonResponseBody(&response). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return nil, errors.WithMessagef(err, "call %s", createUpdateConfig) + } + return response, nil +} + +func (c Config) GetSchemaByModuleId(moduleId string) (*entity.ConfigSchema, error) { + request := entity.GetSchemaByModuleIdRequest{ModuleId: moduleId} + response := new(entity.ConfigSchema) + err := c.cli.Invoke(getSchemaByModuleId). + JsonRequestBody(request). + JsonResponseBody(&response). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return nil, errors.WithMessagef(err, "call %s", getSchemaByModuleId) + } + return response, nil +} + +func (c Config) GetAllVariables() ([]entity.Variable, error) { + response := make([]entity.Variable, 0) + err := c.cli.Invoke(getAllVariables). + JsonResponseBody(&response). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return nil, errors.WithMessagef(err, "call %s", getAllVariables) + } + return response, nil +} + +func (c Config) GetVariableByName(variableName string) (*entity.Variable, error) { + request := entity.VariableByNameRequest{Name: variableName} + response := new(entity.Variable) + err := c.cli.Invoke(getVariableByName). + JsonRequestBody(request). + JsonResponseBody(&response). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return nil, errors.WithMessagef(err, "call %s", getVariableByName) + } + return response, nil +} + +func (c Config) UpsertVariables(request []entity.UpsertVariableRequest) error { + err := c.cli.Invoke(upsertVariable). + JsonRequestBody(request). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return errors.WithMessagef(err, "call %s", upsertVariable) + } + return nil +} + +func (c Config) DeleteVariable(variableName string) error { + request := entity.VariableByNameRequest{Name: variableName} + err := c.cli.Invoke(deleteVariable). + JsonRequestBody(request). + Timeout(time.Second). + Do(c.baseCtx) + if err != nil { + return errors.WithMessagef(err, "call %s", deleteVariable) + } + return nil +} diff --git a/service/common.go b/service/common.go deleted file mode 100644 index e8a0759..0000000 --- a/service/common.go +++ /dev/null @@ -1,7 +0,0 @@ -package service - -import "ispctl/cfg" - -var ( - ConfigClient = cfg.NewConfigClient() -) diff --git a/service/config.go b/service/config.go index 0f77f61..5be1751 100644 --- a/service/config.go +++ b/service/config.go @@ -5,7 +5,8 @@ import ( "fmt" "os" - "ispctl/cfg" + "ispctl/entity" + "ispctl/repository" "github.com/pkg/errors" "github.com/txix-open/isp-kit/grpc/apierrors" @@ -15,26 +16,32 @@ import ( "google.golang.org/grpc/status" ) -var Config configService +var Config = &ConfigService{} -type configService struct { +type ConfigService struct { UnsafeEnable bool + configRepo repository.Config } -func (c configService) ReceiveConfiguration(host string) error { - return ConfigClient.ReceiveConfig(host) +func (c *ConfigService) ReceiveConfiguration(host string) error { + cli, err := repository.NewClientFromHost(host) + if err != nil { + return err + } + c.configRepo = repository.NewConfig(cli) + return nil } -func (c configService) GetAvailableConfigs() ([]cfg.ModuleInfo, error) { - return ConfigClient.GetAvailableConfigs() +func (c *ConfigService) GetAvailableConfigs() ([]entity.ModuleInfo, error) { + return c.configRepo.GetAvailableConfigs() } -func (c configService) GetConfigurationByModuleName(moduleName string) (*cfg.Config, error) { +func (c *ConfigService) GetConfigurationByModuleName(moduleName string) (*entity.Config, error) { if moduleName == "" { return nil, errors.New("need module name") } - moduleConfiguration, err := ConfigClient.GetConfigByModuleName(moduleName) + moduleConfiguration, err := c.configRepo.GetConfigByModuleName(moduleName) if err == nil { return moduleConfiguration, nil } @@ -45,15 +52,15 @@ func (c configService) GetConfigurationByModuleName(moduleName string) (*cfg.Con return nil, err } -func (c configService) GetSchemaByModuleId(moduleId string) (schema.Schema, error) { - configSchema, err := ConfigClient.GetSchemaByModuleId(moduleId) +func (c *ConfigService) GetSchemaByModuleId(moduleId string) (schema.Schema, error) { + configSchema, err := c.configRepo.GetSchemaByModuleId(moduleId) if err != nil { return nil, err } return configSchema.Schema, nil } -func (c configService) CreateUpdateConfig(stringToChange string, configuration *cfg.Config) (map[string]any, error) { +func (c *ConfigService) CreateUpdateConfig(stringToChange string, configuration *entity.Config) (map[string]any, error) { newData := make(map[string]any) if stringToChange != "" { err := json.Unmarshal([]byte(stringToChange), &newData) @@ -66,12 +73,13 @@ func (c configService) CreateUpdateConfig(stringToChange string, configuration * return c.CreateUpdateConfigV2(configuration) } -func (c configService) CreateUpdateConfigV2(configuration *cfg.Config) (map[string]any, error) { +func (c *ConfigService) CreateUpdateConfigV2(configuration *entity.Config) (map[string]any, error) { configuration.Unsafe = c.UnsafeEnable - resp, err := ConfigClient.CreateUpdateConfig(*configuration) + resp, err := c.configRepo.CreateUpdateConfig(*configuration) if err == nil { return resp.Data, nil } + errorStatus, ok := status.FromError(err) if ok && errorStatus.Code() == codes.InvalidArgument { fmt.Print("doesn't match with schema:\n") @@ -79,53 +87,54 @@ func (c configService) CreateUpdateConfigV2(configuration *cfg.Config) (map[stri os.Exit(1) }() for _, value := range errorStatus.Details() { - if a, ok := value.(*epb.BadRequest); ok { - for _, value := range a.FieldViolations { + switch errorDetails := value.(type) { + case *epb.BadRequest: + for _, value := range errorDetails.FieldViolations { fmt.Printf("%s | %s\n", value.Field, value.Description) } - } else { + default: fmt.Println(value) } - return nil, nil } + return nil, nil } return nil, err } -func (configService) GetAllVariables() ([]cfg.Variable, error) { - return ConfigClient.GetAllVariables() +func (c *ConfigService) GetAllVariables() ([]entity.Variable, error) { + return c.configRepo.GetAllVariables() } -func (configService) GetVariableByName(variableName string) (*cfg.Variable, error) { +func (c *ConfigService) GetVariableByName(variableName string) (*entity.Variable, error) { if variableName == "" { return nil, errors.New("need variable name") } - variable, err := ConfigClient.GetVariableByName(variableName) + variable, err := c.configRepo.GetVariableByName(variableName) if err == nil { return variable, nil } apiError := apierrors.FromError(err) - if apiError != nil && apiError.ErrorCode == cfg.ErrCodeVariableNotFound { + if apiError != nil && apiError.ErrorCode == entity.ErrCodeVariableNotFound { return nil, errors.Errorf("variable [%s] not found", variableName) } return nil, err } -func (configService) DeleteVariable(variableName string) error { +func (c *ConfigService) DeleteVariable(variableName string) error { if variableName == "" { return errors.New("need variable name") } - err := ConfigClient.DeleteVariable(variableName) + err := c.configRepo.DeleteVariable(variableName) apiError := apierrors.FromError(err) - if apiError != nil && apiError.ErrorCode == cfg.ErrCodeVariableNotFound { + if apiError != nil && apiError.ErrorCode == entity.ErrCodeVariableNotFound { return errors.Errorf("variable [%s] not found", variableName) } return err } -func (configService) UpsertVariables(vars []cfg.UpsertVariableRequest) error { - return ConfigClient.UpsertVariables(vars) +func (c *ConfigService) UpsertVariables(vars []entity.UpsertVariableRequest) error { + return c.configRepo.UpsertVariables(vars) }