MSP-5014 Try get rid of global service and autocomplete

This commit is contained in:
Синельник Тимур Романович committed 2025-03-24 11:30:39 +03:00
1 parent 84f639a9d3
commit c267b1ae6e
15 files changed
+468 -335

No files matched your search

+46 -29
View File
@@ -5,43 +5,60 @@ import (
"os"
"ispctl/bash"
"ispctl/command/flag"
"ispctl/command/utils"
"ispctl/service"
"ispctl/model"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
func Merge() *cli.Command {
type MergeService interface {
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error)
}
type Merge struct {
service MergeService
autoComplete AutoComplete
}
func NewMerge(service MergeService, autoComplete AutoComplete) Merge {
return Merge{
service: service,
autoComplete: autoComplete,
}
}
func (c Merge) Command() *cli.Command {
return &cli.Command{
Name: "merge",
Usage: "merge actual config with config from stdin",
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
Before: flag.ApplyGlobalFlags,
Action: func(context *cli.Context) error {
moduleName := context.Args().First()
config, err := service.Config.GetConfigurationByModuleName(moduleName)
if err != nil {
return errors.WithMessagef(err, "get module config '%s'", moduleName)
}
inputConfig := make(map[string]any)
err = json.NewDecoder(os.Stdin).Decode(&inputConfig)
if err != nil {
return errors.WithMessage(err, "json unmarshal input config")
}
for key, value := range inputConfig {
config.Data[key] = value
}
result, err := service.Config.CreateUpdateConfigV2(config)
if err != nil {
return errors.WithMessagef(err, "update module config '%s'", moduleName)
}
utils.PrintAnswer(result)
return nil
},
BashComplete: c.autoComplete.Complete(bash.CommonConfigName, bash.Empty),
Action: c.action,
}
}
func (c Merge) action(ctx *cli.Context) error {
moduleName := ctx.Args().First()
config, err := c.service.GetConfigurationByModuleName(moduleName)
if err != nil {
return errors.WithMessagef(err, "get module config '%s'", moduleName)
}
inputConfig := make(map[string]any)
err = json.NewDecoder(os.Stdin).Decode(&inputConfig)
if err != nil {
return errors.WithMessage(err, "json unmarshal input config")
}
for key, value := range inputConfig {
config.Data[key] = value
}
result, err := c.service.CreateUpdateConfigV2(config)
if err != nil {
return errors.WithMessagef(err, "update module config '%s'", moduleName)
}
utils.PrintAnswer(result)
return nil
}