MSP-5014 Try get rid of global service and autocomplete
This commit is contained in:
1 parent
84f639a9d3
commit
c267b1ae6e
15 files changed
+468
-335
No files matched your search
+39
-10
@@ -1,16 +1,45 @@
|
||||
package command
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
import (
|
||||
"ispctl/model"
|
||||
|
||||
func AllCommands() []*cli.Command {
|
||||
"github.com/txix-open/isp-kit/rc/schema"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type AutoComplete interface {
|
||||
Complete(first string, second string) cli.BashCompleteFunc
|
||||
}
|
||||
|
||||
type ConfigService interface {
|
||||
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||
CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error)
|
||||
DeleteVariable(variableName string) error
|
||||
GetAllVariables() ([]model.Variable, error)
|
||||
GetAvailableConfigs() ([]model.ModuleInfo, error)
|
||||
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||
GetSchemaByModuleId(moduleId string) (schema.Schema, error)
|
||||
GetVariableByName(variableName string) (*model.Variable, error)
|
||||
UpsertVariables(vars []model.UpsertVariableRequest) error
|
||||
}
|
||||
|
||||
func AllCommands(configService ConfigService, autoComplete AutoComplete) []*cli.Command {
|
||||
status := NewStatus(configService)
|
||||
get := NewGet(configService, autoComplete)
|
||||
set := NewSet(configService, autoComplete)
|
||||
delete := NewDelete(configService, autoComplete)
|
||||
schema := NewSchema(configService, autoComplete)
|
||||
merge := NewMerge(configService, autoComplete)
|
||||
gitGet := NewGitGet()
|
||||
variables := NewVariables(configService, autoComplete)
|
||||
return []*cli.Command{
|
||||
Status(),
|
||||
Get(),
|
||||
Set(),
|
||||
Delete(),
|
||||
Schema(),
|
||||
Merge(),
|
||||
GitGet(),
|
||||
VariablesCommands(),
|
||||
status.Command(),
|
||||
get.Command(),
|
||||
set.Command(),
|
||||
delete.Command(),
|
||||
schema.Command(),
|
||||
merge.Command(),
|
||||
gitGet.Command(),
|
||||
variables.Command(),
|
||||
}
|
||||
}
|
||||
+37
-27
@@ -4,33 +4,44 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"ispctl/bash"
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/tidwall/sjson"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Delete() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "delete configuration by module_name",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: deleteComm.action,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
type DeleteService interface {
|
||||
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||
}
|
||||
|
||||
type Delete struct {
|
||||
service DeleteService
|
||||
autoComplete AutoComplete
|
||||
}
|
||||
|
||||
func NewDelete(service DeleteService, autoComplete AutoComplete) Delete {
|
||||
return Delete{
|
||||
service: service,
|
||||
autoComplete: autoComplete,
|
||||
}
|
||||
}
|
||||
|
||||
var deleteComm deleteCommand
|
||||
func (c Delete) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "delete configuration by module_name",
|
||||
Action: c.action,
|
||||
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.ModuleData),
|
||||
}
|
||||
}
|
||||
|
||||
type deleteCommand struct{}
|
||||
|
||||
func (d deleteCommand) action(ctx *cli.Context) error {
|
||||
func (c Delete) action(ctx *cli.Context) error {
|
||||
moduleName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
moduleConfiguration, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
moduleConfiguration, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -39,19 +50,18 @@ func (d deleteCommand) action(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
return utils.CreateUpdateConfig("", moduleConfiguration)
|
||||
} else {
|
||||
jsonObject, err := json.Marshal(moduleConfiguration.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return utils.CreateUpdateConfig(stringToChange, moduleConfiguration)
|
||||
}
|
||||
return utils.CreateUpdateConfig("", moduleConfiguration, c.service)
|
||||
}
|
||||
|
||||
jsonObject, err := json.Marshal(moduleConfiguration.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stringToChange, err := sjson.Delete(string(jsonObject), pathObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return utils.CreateUpdateConfig(stringToChange, moduleConfiguration, c.service)
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
package flag
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"ispctl/service"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@@ -24,13 +20,3 @@ var (
|
||||
|
||||
SetVariableSecretType = &cli.BoolFlag{Name: secretVariableName, Usage: secretVariableUsage}
|
||||
)
|
||||
|
||||
func ApplyGlobalFlags(c *cli.Context) error {
|
||||
var host string
|
||||
service.Config.UnsafeEnable = c.Bool(Unsafe.Name)
|
||||
|
||||
host = c.String(Host.Name)
|
||||
host = strings.Replace(host, "'", "", -1)
|
||||
|
||||
return service.Config.ReceiveConfiguration(host)
|
||||
}
|
||||
+24
-17
@@ -4,35 +4,42 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"ispctl/bash"
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Get() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "get",
|
||||
Usage: "get configuration by module_name",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: get.action,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
type GetService interface {
|
||||
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||
}
|
||||
|
||||
type Get struct {
|
||||
service SetService
|
||||
autoComplete AutoComplete
|
||||
}
|
||||
|
||||
func NewGet(service SetService, autoComplete AutoComplete) Get {
|
||||
return Get{
|
||||
service: service,
|
||||
autoComplete: autoComplete,
|
||||
}
|
||||
}
|
||||
|
||||
var get getCommand
|
||||
|
||||
type getCommand struct{}
|
||||
|
||||
func (g getCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.ApplyGlobalFlags(ctx); err != nil {
|
||||
return err
|
||||
func (c Get) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "get",
|
||||
Usage: "get configuration by module_name",
|
||||
Action: c.action,
|
||||
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.ModuleData),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Get) action(ctx *cli.Context) error {
|
||||
moduleName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
config, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+55
-47
@@ -13,57 +13,65 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
semverRegexp = regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$`)
|
||||
)
|
||||
type GitGet struct {
|
||||
semverRegexp *regexp.Regexp
|
||||
}
|
||||
|
||||
func GitGet() *cli.Command {
|
||||
func NewGitGet() GitGet {
|
||||
return GitGet{
|
||||
semverRegexp: regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$`),
|
||||
}
|
||||
}
|
||||
|
||||
func (c GitGet) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "gitget",
|
||||
Usage: "gitget [repository] [file] (commit|tag)",
|
||||
Description: "clone and print specified file from specified repository and specified commit hash or tag",
|
||||
Action: func(context *cli.Context) error {
|
||||
storage := memory.NewStorage()
|
||||
repo := context.Args().Get(0)
|
||||
|
||||
file := context.Args().Get(1)
|
||||
commit := context.Args().Get(2)
|
||||
|
||||
repository, err := git.Clone(storage, memfs.New(), &git.CloneOptions{
|
||||
URL: repo,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "clone %s", repo)
|
||||
}
|
||||
tree, err := repository.Worktree()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get worktree")
|
||||
}
|
||||
|
||||
if commit != "" {
|
||||
checkoutOpts := git.CheckoutOptions{}
|
||||
if semverRegexp.MatchString(commit) {
|
||||
checkoutOpts.Branch = plumbing.NewTagReferenceName(commit)
|
||||
} else {
|
||||
checkoutOpts.Hash = plumbing.NewHash(commit)
|
||||
}
|
||||
err = tree.Checkout(&checkoutOpts)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "checkout: %s", commit)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := tree.Filesystem.Open(file)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "open %s", file)
|
||||
}
|
||||
|
||||
_, err = io.Copy(os.Stdout, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Action: c.action,
|
||||
}
|
||||
}
|
||||
|
||||
func (c GitGet) action(ctx *cli.Context) error {
|
||||
storage := memory.NewStorage()
|
||||
repo := ctx.Args().Get(0)
|
||||
|
||||
file := ctx.Args().Get(1)
|
||||
commit := ctx.Args().Get(2)
|
||||
|
||||
repository, err := git.Clone(storage, memfs.New(), &git.CloneOptions{
|
||||
URL: repo,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "clone %s", repo)
|
||||
}
|
||||
tree, err := repository.Worktree()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get worktree")
|
||||
}
|
||||
|
||||
if commit != "" {
|
||||
checkoutOpts := git.CheckoutOptions{}
|
||||
if c.semverRegexp.MatchString(commit) {
|
||||
checkoutOpts.Branch = plumbing.NewTagReferenceName(commit)
|
||||
} else {
|
||||
checkoutOpts.Hash = plumbing.NewHash(commit)
|
||||
}
|
||||
err = tree.Checkout(&checkoutOpts)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "checkout: %s", commit)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := tree.Filesystem.Open(file)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "open %s", file)
|
||||
}
|
||||
|
||||
_, err = io.Copy(os.Stdout, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+46
-29
@@ -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
|
||||
}
|
||||
+49
-39
@@ -7,36 +7,50 @@ import (
|
||||
"ispctl/bash"
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
"ispctl/tmpl"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/rc/schema"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Schema() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "schema",
|
||||
Usage: "get schema configuration by module_name",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: schema.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrintSchema,
|
||||
},
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.Empty).Complete,
|
||||
type SchemaService interface {
|
||||
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||
GetSchemaByModuleId(moduleId string) (schema.Schema, error)
|
||||
}
|
||||
|
||||
type Schema struct {
|
||||
service SchemaService
|
||||
autoComplete AutoComplete
|
||||
}
|
||||
|
||||
func NewSchema(service SchemaService, autoComplete AutoComplete) Schema {
|
||||
return Schema{
|
||||
service: service,
|
||||
autoComplete: autoComplete,
|
||||
}
|
||||
}
|
||||
|
||||
var schema schemaCommand
|
||||
|
||||
type schemaCommand struct{}
|
||||
|
||||
func (s schemaCommand) action(ctx *cli.Context) error {
|
||||
moduleName := ctx.Args().First()
|
||||
schemaConfig := s.getSchemaConfig(moduleName)
|
||||
if schemaConfig == nil {
|
||||
return nil
|
||||
func (c Schema) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "schema",
|
||||
Usage: "get schema configuration by module_name",
|
||||
Action: c.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrintSchema,
|
||||
},
|
||||
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.Empty),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Schema) action(ctx *cli.Context) error {
|
||||
moduleName := ctx.Args().First()
|
||||
schemaConfig, err := c.getSchemaConfig(moduleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
schema := make(map[string]any)
|
||||
schema["title"] = moduleName
|
||||
schema["schema"] = schemaConfig
|
||||
@@ -44,33 +58,29 @@ func (s schemaCommand) action(ctx *cli.Context) error {
|
||||
case flag.OutPrintJsonValue:
|
||||
utils.PrintAnswer(schema)
|
||||
case flag.OutPrintHtmlValue:
|
||||
s.printHtml(schema)
|
||||
return c.printHtml(schema)
|
||||
default:
|
||||
return errors.Errorf("invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s schemaCommand) getSchemaConfig(moduleName string) any {
|
||||
if configuration, err := service.Config.GetConfigurationByModuleName(moduleName); err != nil {
|
||||
utils.PrintError(err)
|
||||
return nil
|
||||
} else if schema, err := service.Config.GetSchemaByModuleId(configuration.ModuleId); err != nil {
|
||||
utils.PrintError(err)
|
||||
return nil
|
||||
} else {
|
||||
return schema
|
||||
func (c Schema) getSchemaConfig(moduleName string) (schema.Schema, error) {
|
||||
configuration, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
schema, err := c.service.GetSchemaByModuleId(configuration.ModuleId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
func (s schemaCommand) printHtml(schema map[string]any) {
|
||||
if temp, err := template.New("template").Parse(tmpl.HtmlFile); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
} else {
|
||||
if err := temp.ExecuteTemplate(os.Stdout, "template", schema); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
func (c Schema) printHtml(schema map[string]any) error {
|
||||
temp, err := template.New("template").Parse(tmpl.HtmlFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return temp.ExecuteTemplate(os.Stdout, "template", schema)
|
||||
}
|
||||
+37
-27
@@ -4,34 +4,45 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"ispctl/bash"
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/tidwall/sjson"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Set() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "set",
|
||||
Usage: "set configuration by module_name",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: set.action,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
type SetService interface {
|
||||
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||
}
|
||||
|
||||
type Set struct {
|
||||
service SetService
|
||||
autoComplete AutoComplete
|
||||
}
|
||||
|
||||
func NewSet(service SetService, autoComplete AutoComplete) Set {
|
||||
return Set{
|
||||
service: service,
|
||||
autoComplete: autoComplete,
|
||||
}
|
||||
}
|
||||
|
||||
var set setCommand
|
||||
func (c Set) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "set",
|
||||
Usage: "set configuration by module_name",
|
||||
Action: c.action,
|
||||
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.ModuleData),
|
||||
}
|
||||
}
|
||||
|
||||
type setCommand struct{}
|
||||
|
||||
func (s setCommand) action(ctx *cli.Context) error {
|
||||
func (c Set) action(ctx *cli.Context) error {
|
||||
moduleName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
changeObject := ctx.Args().Get(2)
|
||||
|
||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
config, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -47,18 +58,17 @@ func (s setCommand) action(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
return utils.CreateUpdateConfig(changeObject, config)
|
||||
} else {
|
||||
jsonObject, err := json.Marshal(config.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
changeArgument := utils.ParseSetObject(changeObject)
|
||||
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return utils.CreateUpdateConfig(stringToChange, config)
|
||||
}
|
||||
return utils.CreateUpdateConfig(changeObject, config, c.service)
|
||||
}
|
||||
jsonObject, err := json.Marshal(config.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
changeArgument := utils.ParseSetObject(changeObject)
|
||||
stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return utils.CreateUpdateConfig(stringToChange, config, c.service)
|
||||
}
|
||||
+38
-31
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -22,48 +22,55 @@ const (
|
||||
tableConnectedStatus = "CONNECTED"
|
||||
)
|
||||
|
||||
func Status() *cli.Command {
|
||||
type StatusService interface {
|
||||
GetAvailableConfigs() ([]model.ModuleInfo, error)
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
service StatusService
|
||||
}
|
||||
|
||||
func NewStatus(service StatusService) Status {
|
||||
return Status{service: service}
|
||||
}
|
||||
|
||||
func (c Status) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "status",
|
||||
Usage: "get available configs",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: status.action,
|
||||
Action: c.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrintStatus,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var status statusCommand
|
||||
|
||||
type statusCommand struct{}
|
||||
|
||||
func (statusCommand) action(ctx *cli.Context) error {
|
||||
if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil {
|
||||
func (c Status) action(ctx *cli.Context) error {
|
||||
arrayOfModules, err := c.service.GetAvailableConfigs()
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
switch ctx.String(flag.OutPrintStatus.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
utils.PrintAnswer(arrayOfModules)
|
||||
default:
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
|
||||
table.SetHeader([]string{tableHeaderModuleName, tableHeaderStatus, tableHeaderAddresses})
|
||||
for _, module := range arrayOfModules {
|
||||
addresses := ""
|
||||
connection := tableNotConnectedStatus
|
||||
instanceList := make([]string, 0, len(module.Status))
|
||||
for _, value := range module.Status {
|
||||
instanceList = append(instanceList, fmt.Sprintf("%s:%s %s", value.Address.IP, value.Address.Port, value.Version))
|
||||
}
|
||||
addresses = strings.Join(instanceList, "\n")
|
||||
if addresses != "" {
|
||||
connection = tableConnectedStatus
|
||||
}
|
||||
table.Append([]string{module.Name, connection, addresses})
|
||||
}
|
||||
switch ctx.String(flag.OutPrintStatus.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
utils.PrintAnswer(arrayOfModules)
|
||||
default:
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
|
||||
table.SetHeader([]string{tableHeaderModuleName, tableHeaderStatus, tableHeaderAddresses})
|
||||
for _, module := range arrayOfModules {
|
||||
addresses := ""
|
||||
connection := tableNotConnectedStatus
|
||||
instanceList := make([]string, 0, len(module.Status))
|
||||
for _, value := range module.Status {
|
||||
instanceList = append(instanceList, fmt.Sprintf("%s:%s %s", value.Address.IP, value.Address.Port, value.Version))
|
||||
}
|
||||
table.Render()
|
||||
addresses = strings.Join(instanceList, "\n")
|
||||
if addresses != "" {
|
||||
connection = tableConnectedStatus
|
||||
}
|
||||
table.Append([]string{module.Name, connection, addresses})
|
||||
}
|
||||
table.Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -2,11 +2,14 @@ package utils
|
||||
|
||||
import (
|
||||
"ispctl/model"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func CreateUpdateConfig(stringToChange string, configuration *model.Config) error {
|
||||
answer, err := service.Config.CreateUpdateConfig(stringToChange, configuration)
|
||||
type UpdateConfigService interface {
|
||||
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||
}
|
||||
|
||||
func CreateUpdateConfig(stringToChange string, configuration *model.Config, service UpdateConfigService) error {
|
||||
answer, err := service.CreateUpdateConfig(stringToChange, configuration)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if answer != nil {
|
||||
|
||||
+40
-31
@@ -7,7 +7,6 @@ import (
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/model"
|
||||
"ispctl/service"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -15,7 +14,26 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func VariablesCommands() *cli.Command {
|
||||
type VariablesService interface {
|
||||
GetAllVariables() ([]model.Variable, error)
|
||||
GetVariableByName(variableName string) (*model.Variable, error)
|
||||
DeleteVariable(variableName string) error
|
||||
UpsertVariables(vars []model.UpsertVariableRequest) error
|
||||
}
|
||||
|
||||
type Variables struct {
|
||||
service VariablesService
|
||||
autoComplete AutoComplete
|
||||
}
|
||||
|
||||
func NewVariables(service VariablesService, autoComplete AutoComplete) Variables {
|
||||
return Variables{
|
||||
service: service,
|
||||
autoComplete: autoComplete,
|
||||
}
|
||||
}
|
||||
|
||||
func (c Variables) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "vars",
|
||||
Usage: "manage variables",
|
||||
@@ -23,49 +41,40 @@ func VariablesCommands() *cli.Command {
|
||||
{
|
||||
Name: "get",
|
||||
Usage: "vars get <name>",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: varsComm.getByName,
|
||||
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
|
||||
Action: c.getByName,
|
||||
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
Usage: "list all variables",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: varsComm.list,
|
||||
Action: c.list,
|
||||
},
|
||||
{
|
||||
Name: "set",
|
||||
Usage: "vars set <name> <value>",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: varsComm.set,
|
||||
Action: c.set,
|
||||
Flags: []cli.Flag{
|
||||
flag.SetVariableSecretType,
|
||||
},
|
||||
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
|
||||
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Usage: "vars delete <name>",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: varsComm.delete,
|
||||
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
|
||||
Action: c.delete,
|
||||
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||
},
|
||||
{
|
||||
Name: "upload",
|
||||
Usage: "vars upload <filepath>",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: varsComm.upload,
|
||||
Action: c.upload,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var varsComm varsCommands
|
||||
|
||||
type varsCommands struct{}
|
||||
|
||||
func (g varsCommands) list(ctx *cli.Context) error {
|
||||
vars, err := service.Config.GetAllVariables()
|
||||
func (c Variables) list(ctx *cli.Context) error {
|
||||
vars, err := c.service.GetAllVariables()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -84,9 +93,9 @@ func (g varsCommands) list(ctx *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g varsCommands) getByName(ctx *cli.Context) error {
|
||||
func (c Variables) getByName(ctx *cli.Context) error {
|
||||
variableName := ctx.Args().Get(0)
|
||||
variable, err := service.Config.GetVariableByName(variableName)
|
||||
variable, err := c.service.GetVariableByName(variableName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -94,33 +103,33 @@ func (g varsCommands) getByName(ctx *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g varsCommands) set(ctx *cli.Context) error {
|
||||
func (c Variables) set(ctx *cli.Context) error {
|
||||
variableName := ctx.Args().Get(0)
|
||||
variableValue := ctx.Args().Get(1)
|
||||
variableType := model.TextVariableType
|
||||
if ctx.Bool(flag.SetVariableSecretType.Name) {
|
||||
variableType = model.SecretVariableType
|
||||
}
|
||||
return service.Config.UpsertVariables([]model.UpsertVariableRequest{{
|
||||
return c.service.UpsertVariables([]model.UpsertVariableRequest{{
|
||||
Name: variableName,
|
||||
Value: variableValue,
|
||||
Type: variableType,
|
||||
}})
|
||||
}
|
||||
|
||||
func (g varsCommands) delete(ctx *cli.Context) error {
|
||||
return service.Config.DeleteVariable(ctx.Args().Get(0))
|
||||
func (c Variables) delete(ctx *cli.Context) error {
|
||||
return c.service.DeleteVariable(ctx.Args().Get(0))
|
||||
}
|
||||
|
||||
func (g varsCommands) upload(ctx *cli.Context) error {
|
||||
variables, err := g.readVariablesFromCsv(ctx.Args().First())
|
||||
func (c Variables) upload(ctx *cli.Context) error {
|
||||
variables, err := c.readVariablesFromCsv(ctx.Args().First())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return service.Config.UpsertVariables(variables)
|
||||
return c.service.UpsertVariables(variables)
|
||||
}
|
||||
|
||||
func (g varsCommands) readVariablesFromCsv(filepath string) ([]model.UpsertVariableRequest, error) {
|
||||
func (c Variables) readVariablesFromCsv(filepath string) ([]model.UpsertVariableRequest, error) {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file %s: %w", filepath, err)
|
||||
|
||||
Reference in new issue
Block a user