2025-03-24 15:00:34: merge [main:f6e45d7212331e8c861cded4b952eaec0b701cc1];
This commit is contained in:
1 parent
dbf061213d
commit
922641647a
39 files changed
+1262
-1418
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"ispctl/command/utils"
|
||||
"ispctl/model"
|
||||
|
||||
"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
|
||||
}
|
||||
|
||||
type UpdateConfigService interface {
|
||||
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, 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.Command(),
|
||||
get.Command(),
|
||||
set.Command(),
|
||||
delete.Command(),
|
||||
schema.Command(),
|
||||
merge.Command(),
|
||||
gitGet.Command(),
|
||||
variables.Command(),
|
||||
}
|
||||
}
|
||||
|
||||
func CreateUpdateConfig(stringToChange string, configuration *model.Config, service UpdateConfigService) error {
|
||||
answer, err := service.CreateUpdateConfig(stringToChange, configuration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if answer != nil {
|
||||
return utils.PrintAnswer(answer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/command/common_config"
|
||||
)
|
||||
|
||||
func CommonConfig() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "common",
|
||||
Usage: "access to common configs commands",
|
||||
Action: cc.action,
|
||||
BashComplete: cc.bachComplete,
|
||||
Subcommands: []*cli.Command{
|
||||
common_config.Get(),
|
||||
common_config.Set(),
|
||||
common_config.Delete(),
|
||||
common_config.Remove(),
|
||||
common_config.Link(),
|
||||
common_config.UnLink(),
|
||||
common_config.Contain(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var cc commonConfigCommand
|
||||
|
||||
type commonConfigCommand struct{}
|
||||
|
||||
func (g commonConfigCommand) action(ctx *cli.Context) error {
|
||||
if ctx.Args().First() == "" {
|
||||
fmt.Print("need use command: ")
|
||||
for _, comm := range CommonConfig().Subcommands {
|
||||
fmt.Printf("[%s] ", comm.Name)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g commonConfigCommand) bachComplete(ctx *cli.Context) {
|
||||
for _, command := range CommonConfig().Subcommands {
|
||||
fmt.Println(command.Name)
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func Contain() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "contain",
|
||||
Usage: "availability common configuration in module",
|
||||
Action: contain.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var contain containCommand
|
||||
|
||||
type containCommand struct{}
|
||||
|
||||
func (g containCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
|
||||
if configName == "" {
|
||||
return errors.New("empty common config name")
|
||||
}
|
||||
|
||||
config, err := service.Config.GetCommonConfigByName(configName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
links, err := service.Config.GetLinksCommonConfig(config.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
fmt.Printf("config [%s] linked with next modules:\n", configName)
|
||||
for _, link := range links {
|
||||
fmt.Printf("[%s] ", link)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/sjson"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func Delete() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "set common configurations",
|
||||
Action: deleteComm.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var deleteComm deleteCommand
|
||||
|
||||
type deleteCommand struct{}
|
||||
|
||||
func (g deleteCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
if configName == "" {
|
||||
return errors.New("empty config name")
|
||||
}
|
||||
|
||||
pathObject, err := utils.CheckPath(pathObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config, err := service.Config.GetCommonConfigByName(configName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
return utils.CreateUpdateCommonConfig("", config)
|
||||
} else {
|
||||
jsonObject, err := json.Marshal(config.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return utils.CreateUpdateCommonConfig(stringToChange, config)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func Get() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "get",
|
||||
Usage: "get common configurations",
|
||||
Action: get.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var get getCommand
|
||||
|
||||
type getCommand struct{}
|
||||
|
||||
func (g getCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
commonConfigs, err := service.Config.GetMapNameCommonConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if configName == "" {
|
||||
fmt.Printf("available next common configs:\n")
|
||||
for name := range commonConfigs {
|
||||
fmt.Printf("[%s] ", name)
|
||||
}
|
||||
fmt.Println()
|
||||
return nil
|
||||
}
|
||||
|
||||
pathObject, err = utils.CheckPath(pathObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[configName]
|
||||
if !ok {
|
||||
return errors.Errorf("common config [%s] not found", configName)
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
utils.PrintAnswer(config.Data)
|
||||
} else if jsonObject, err := json.Marshal(config.Data); err != nil {
|
||||
return err
|
||||
} else {
|
||||
utils.CheckObject(jsonObject, pathObject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func Link() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "link",
|
||||
Usage: "link common configurations to module configuration",
|
||||
Action: link.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.ModuleName).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var link linkCommand
|
||||
|
||||
type linkCommand struct{}
|
||||
|
||||
func (g linkCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
moduleName := ctx.Args().Get(1)
|
||||
|
||||
if err := service.Config.LinkCommonConfigToModule(configName, moduleName); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func Remove() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "remove",
|
||||
Usage: "remove common configurations",
|
||||
Action: remove.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var remove removeCommand
|
||||
|
||||
type removeCommand struct{}
|
||||
|
||||
func (g removeCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
|
||||
if configName == "" {
|
||||
return errors.New("empty common config name")
|
||||
}
|
||||
|
||||
config, err := service.Config.GetCommonConfigByName(configName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
links, deleted, err := service.Config.DeleteCommonConfig(config.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if deleted {
|
||||
fmt.Printf("config [%s] deleted\n", config.Name)
|
||||
} else {
|
||||
fmt.Printf("config [%s] not deleted, need unlink in next modules:\n%v\n", config.Name, links)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/sjson"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/cfg"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func Set() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "set",
|
||||
Usage: "set common configurations",
|
||||
Action: set.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var set setCommand
|
||||
|
||||
type setCommand struct{}
|
||||
|
||||
func (g setCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
changeObject := ctx.Args().Get(2)
|
||||
|
||||
if configName == "" {
|
||||
return errors.New("empty config name")
|
||||
}
|
||||
|
||||
mapConfigByName, err := service.Config.GetMapNameCommonConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config, ok := mapConfigByName[configName]
|
||||
if !ok {
|
||||
config = cfg.CommonConfig{
|
||||
Name: configName,
|
||||
}
|
||||
}
|
||||
|
||||
pathObject, err = utils.CheckPath(pathObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
changeObject, err = utils.CheckChangeObject(changeObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
return utils.CreateUpdateCommonConfig(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.CreateUpdateCommonConfig(stringToChange, config)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func UnLink() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "unlink",
|
||||
Usage: "unlink common configurations from module configuration",
|
||||
Action: unlink.action,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.ModuleName).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
var unlink unlinkCommand
|
||||
|
||||
type unlinkCommand struct{}
|
||||
|
||||
func (g unlinkCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
moduleName := ctx.Args().Get(1)
|
||||
|
||||
if err := service.Config.UnlinkCommonConfigFromModule(configName, moduleName); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+40
-31
@@ -3,35 +3,45 @@ package command
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/tidwall/sjson"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"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",
|
||||
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
|
||||
|
||||
type deleteCommand struct{}
|
||||
|
||||
func (d deleteCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -40,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 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 CreateUpdateConfig(stringToChange, moduleConfiguration, c.service)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package command
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
HostFlagName = "gateHost"
|
||||
HostFlagUsage = "isp-config-service address"
|
||||
|
||||
UnsafeFlagName = "unsafe"
|
||||
UnsafeFlagUsage = "disable checking schema for set configuration"
|
||||
|
||||
OutPrintFlagName = "o"
|
||||
OutPrintJsonValue = "json"
|
||||
OutPrintHtmlValue = "html"
|
||||
|
||||
SecretFlagName = "secret"
|
||||
secretVariableFlagUsage = "set variable as secret"
|
||||
)
|
||||
|
||||
var (
|
||||
outPrintStatusUsage = fmt.Sprintf("out print for status; %s", OutPrintJsonValue)
|
||||
outPrintSchemaUsage = fmt.Sprintf("out print for schema; %s or %s", OutPrintJsonValue, OutPrintHtmlValue)
|
||||
)
|
||||
+33
-33
@@ -3,47 +3,48 @@ package command
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Get() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "get",
|
||||
Usage: "get configuration by module_name",
|
||||
Action: get.action,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
Flags: []cli.Flag{
|
||||
flag.WithCommonConfig,
|
||||
},
|
||||
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.CheckGlobal(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
|
||||
}
|
||||
|
||||
data := config.Data
|
||||
if ctx.Bool(flag.WithCommonConfig.Name) {
|
||||
if data, err = service.Config.CompileDataWithCommonConfigs(data, config.CommonConfigs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
pathObject, err = utils.CheckPath(pathObject)
|
||||
if err != nil {
|
||||
@@ -51,13 +52,12 @@ func (g getCommand) action(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
utils.PrintAnswer(data)
|
||||
} else {
|
||||
jsonObject, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
utils.CheckObject(jsonObject, pathObject)
|
||||
return utils.PrintAnswer(data)
|
||||
}
|
||||
return nil
|
||||
|
||||
jsonObject, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return utils.CheckObject(jsonObject, pathObject)
|
||||
}
|
||||
+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
|
||||
}
|
||||
+48
-34
@@ -4,46 +4,60 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"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,
|
||||
Action: func(context *cli.Context) error {
|
||||
err := flag.CheckGlobal(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
return utils.PrintAnswer(result)
|
||||
}
|
||||
+57
-49
@@ -4,73 +4,81 @@ import (
|
||||
"html/template"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"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 {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (c Schema) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "schema",
|
||||
Usage: "get schema configuration by module_name",
|
||||
Action: schema.action,
|
||||
Action: c.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrintSchema,
|
||||
&cli.StringFlag{Name: OutPrintFlagName, Usage: outPrintSchemaUsage, Value: OutPrintJsonValue},
|
||||
},
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.Empty).Complete,
|
||||
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.Empty),
|
||||
}
|
||||
}
|
||||
|
||||
var schema schemaCommand
|
||||
|
||||
type schemaCommand struct{}
|
||||
|
||||
func (s schemaCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
func (c Schema) action(ctx *cli.Context) error {
|
||||
moduleName := ctx.Args().First()
|
||||
schemaConfig, err := c.getSchemaConfig(moduleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
moduleName := ctx.Args().First()
|
||||
|
||||
if schemaConfig := s.getSchemaConfig(moduleName); schemaConfig != nil {
|
||||
schema := make(map[string]interface{})
|
||||
schema["title"] = moduleName
|
||||
schema["schema"] = schemaConfig
|
||||
switch ctx.String(flag.OutPrintSchema.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
utils.PrintAnswer(schema)
|
||||
case flag.OutPrintHtmlValue:
|
||||
s.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) interface{} {
|
||||
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
|
||||
schema := make(map[string]any)
|
||||
schema["title"] = moduleName
|
||||
schema["schema"] = schemaConfig
|
||||
switch ctx.String(OutPrintFlagName) {
|
||||
case OutPrintJsonValue:
|
||||
return utils.PrintAnswer(schema)
|
||||
case OutPrintHtmlValue:
|
||||
return c.printHtml(schema)
|
||||
default:
|
||||
return errors.Errorf("invalid flag value, expected [%s] or [%s]", OutPrintJsonValue, OutPrintHtmlValue)
|
||||
}
|
||||
}
|
||||
|
||||
func (s schemaCommand) printHtml(schema map[string]interface{}) {
|
||||
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) 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 (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)
|
||||
}
|
||||
+39
-31
@@ -3,37 +3,46 @@ package command
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/tidwall/sjson"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"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",
|
||||
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
|
||||
|
||||
type setCommand struct{}
|
||||
|
||||
func (s setCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
return err
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -49,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 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 CreateUpdateConfig(stringToChange, config, c.service)
|
||||
}
|
||||
+40
-35
@@ -5,11 +5,11 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"ispctl/command/utils"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/urfave/cli/v2"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/flag"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -21,50 +21,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",
|
||||
Action: status.action,
|
||||
Action: c.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrintStatus,
|
||||
&cli.StringFlag{Name: OutPrintFlagName, Usage: outPrintStatusUsage},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var status statusCommand
|
||||
|
||||
type statusCommand struct{}
|
||||
|
||||
func (statusCommand) action(ctx *cli.Context) error {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
func (c Status) action(ctx *cli.Context) error {
|
||||
arrayOfModules, err := c.service.GetAvailableConfigs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if arrayOfModules, err := service.Config.GetAvailableConfigs(); 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(OutPrintFlagName) {
|
||||
case OutPrintJsonValue:
|
||||
return 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
|
||||
}
|
||||
+17
-26
@@ -2,7 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -22,7 +22,7 @@ func CheckPath(pathObject string) (string, error) {
|
||||
continue
|
||||
}
|
||||
if key == 1 {
|
||||
pathObject = fmt.Sprintf("%s", value)
|
||||
pathObject = value
|
||||
continue
|
||||
}
|
||||
pathObject = fmt.Sprintf("%s.%s", pathObject, value)
|
||||
@@ -32,7 +32,7 @@ func CheckPath(pathObject string) (string, error) {
|
||||
|
||||
func CheckChangeObject(changeObject string) (string, error) {
|
||||
if changeObject == "" {
|
||||
bytes, err := ioutil.ReadAll(os.Stdin)
|
||||
bytes, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -40,26 +40,18 @@ func CheckChangeObject(changeObject string) (string, error) {
|
||||
}
|
||||
if changeObject == "" {
|
||||
return "", errors.New("expected argument")
|
||||
} else {
|
||||
return changeObject, nil
|
||||
}
|
||||
return changeObject, nil
|
||||
}
|
||||
|
||||
func PrintAnswer(data interface{}) {
|
||||
func PrintAnswer(data any) error {
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetEscapeHTML(false)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
PrintError(err)
|
||||
}
|
||||
return encoder.Encode(data)
|
||||
}
|
||||
|
||||
func PrintError(err error) {
|
||||
fmt.Println("ERROR:", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
func ParseSetObject(argument string) interface{} {
|
||||
func ParseSetObject(argument string) any {
|
||||
tryParse := []byte(argument)
|
||||
if tryParse[0] == '"' && tryParse[len(tryParse)-1] == '"' {
|
||||
tryParse = tryParse[1 : len(tryParse)-1]
|
||||
@@ -70,12 +62,12 @@ func ParseSetObject(argument string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
mapStringInterface := make(map[string]interface{})
|
||||
mapStringInterface := make(map[string]any)
|
||||
if err := json.Unmarshal(tryParse, &mapStringInterface); err == nil {
|
||||
return mapStringInterface
|
||||
}
|
||||
|
||||
arrayOfObject := make([]interface{}, 0)
|
||||
arrayOfObject := make([]any, 0)
|
||||
if err := json.Unmarshal(tryParse, &arrayOfObject); err == nil {
|
||||
return arrayOfObject
|
||||
}
|
||||
@@ -98,16 +90,15 @@ func ParseSetObject(argument string) interface{} {
|
||||
return argument
|
||||
}
|
||||
|
||||
func CheckObject(jsonObject []byte, depth string) {
|
||||
func CheckObject(jsonObject []byte, depth string) error {
|
||||
jsonString := gjson.GetBytes(jsonObject, depth)
|
||||
if jsonString.Raw == "" {
|
||||
PrintError(errors.Errorf("path '%s' not found\n", depth))
|
||||
} else {
|
||||
var data interface{}
|
||||
if err := json.Unmarshal([]byte(jsonString.Raw), &data); err != nil {
|
||||
PrintError(err)
|
||||
} else {
|
||||
PrintAnswer(data)
|
||||
}
|
||||
return errors.Errorf("path '%s' not found\n", depth)
|
||||
}
|
||||
var data any
|
||||
err := json.Unmarshal([]byte(jsonString.Raw), &data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return PrintAnswer(data)
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"ispctl/cfg"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func CreateUpdateCommonConfig(stringToChange string, configuration cfg.CommonConfig) error {
|
||||
if answer, err := service.Config.CreateUpdateCommonConfig(stringToChange, configuration); err != nil {
|
||||
return err
|
||||
} else if answer != nil {
|
||||
PrintAnswer(answer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"ispctl/cfg"
|
||||
"ispctl/service"
|
||||
)
|
||||
|
||||
func CreateUpdateConfig(stringToChange string, configuration *cfg.Config) error {
|
||||
if answer, err := service.Config.CreateUpdateConfig(stringToChange, configuration); err != nil {
|
||||
return err
|
||||
} else if answer != nil {
|
||||
PrintAnswer(answer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"ispctl/bash"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/model"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
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",
|
||||
Subcommands: []*cli.Command{
|
||||
{
|
||||
Name: "get",
|
||||
Usage: "vars get <name>",
|
||||
Action: c.getByName,
|
||||
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
Usage: "list all variables",
|
||||
Action: c.list,
|
||||
},
|
||||
{
|
||||
Name: "set",
|
||||
Usage: "vars set <name> <value>",
|
||||
Action: c.set,
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{Name: SecretFlagName, Usage: secretVariableFlagUsage},
|
||||
},
|
||||
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Usage: "vars delete <name>",
|
||||
Action: c.delete,
|
||||
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||
},
|
||||
{
|
||||
Name: "upload",
|
||||
Usage: "vars upload <filepath>",
|
||||
Action: c.upload,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c Variables) list(ctx *cli.Context) error {
|
||||
vars, err := c.service.GetAllVariables()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetHeader([]string{"Name", "Description", "Type", "Value", "Configs"})
|
||||
for _, v := range vars {
|
||||
configs := make([]string, 0, len(v.ContainsInConfigs))
|
||||
for _, c := range v.ContainsInConfigs {
|
||||
configs = append(configs, c.Name)
|
||||
}
|
||||
table.Append([]string{v.Name, v.Description, v.Type, v.Value, strings.Join(configs, ",")})
|
||||
}
|
||||
table.SetBorder(true)
|
||||
table.SetRowLine(true)
|
||||
table.Render()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Variables) getByName(ctx *cli.Context) error {
|
||||
variableName := ctx.Args().Get(0)
|
||||
variable, err := c.service.GetVariableByName(variableName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return utils.PrintAnswer(variable)
|
||||
}
|
||||
|
||||
func (c Variables) set(ctx *cli.Context) error {
|
||||
variableName := ctx.Args().Get(0)
|
||||
variableValue := ctx.Args().Get(1)
|
||||
variableType := model.TextVariableType
|
||||
if ctx.Bool(SecretFlagName) {
|
||||
variableType = model.SecretVariableType
|
||||
}
|
||||
return c.service.UpsertVariables([]model.UpsertVariableRequest{{
|
||||
Name: variableName,
|
||||
Value: variableValue,
|
||||
Type: variableType,
|
||||
}})
|
||||
}
|
||||
|
||||
func (c Variables) delete(ctx *cli.Context) error {
|
||||
return c.service.DeleteVariable(ctx.Args().Get(0))
|
||||
}
|
||||
|
||||
func (c Variables) upload(ctx *cli.Context) error {
|
||||
variables, err := c.readVariablesFromCsv(ctx.Args().First())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.service.UpsertVariables(variables)
|
||||
}
|
||||
|
||||
func (c Variables) readVariablesFromCsv(filepath string) ([]model.UpsertVariableRequest, error) {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "failed to open file %s", filepath)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
reader := csv.NewReader(file)
|
||||
reader.TrimLeadingSpace = true
|
||||
records, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "failed to read CSV file")
|
||||
}
|
||||
if len(records) < 2 {
|
||||
return nil, errors.New("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
|
||||
}
|
||||
|
||||
for _, columnName := range []string{"name", "type", "value", "description"} {
|
||||
_, ok := columnIndexes[columnName]
|
||||
if !ok {
|
||||
return nil, errors.Errorf("column '%s' is required", columnName)
|
||||
}
|
||||
}
|
||||
|
||||
variables := make([]model.UpsertVariableRequest, 0, len(records)-1)
|
||||
for _, row := range records[1:] {
|
||||
variables = append(variables, model.UpsertVariableRequest{
|
||||
Name: row[columnIndexes["name"]],
|
||||
Type: row[columnIndexes["type"]],
|
||||
Value: row[columnIndexes["value"]],
|
||||
Description: row[columnIndexes["description"]],
|
||||
})
|
||||
}
|
||||
return variables, nil
|
||||
}
|
||||
Reference in new issue
Block a user