ref
This commit is contained in:
1 parent
8b8815d2f2
commit
cf23b95af1
20 files changed
+361
-423
No files matched your search
+8
-8
@@ -3,7 +3,7 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"isp-ctl/command/commonConfig"
|
||||
"isp-ctl/command/common_config"
|
||||
)
|
||||
|
||||
func CommonConfig() cli.Command {
|
||||
@@ -13,13 +13,13 @@ func CommonConfig() cli.Command {
|
||||
Action: cc.action,
|
||||
BashComplete: cc.bachComplete,
|
||||
Subcommands: []cli.Command{
|
||||
commonConfig.Get(),
|
||||
commonConfig.Set(),
|
||||
commonConfig.Delete(),
|
||||
commonConfig.Remove(),
|
||||
commonConfig.Link(),
|
||||
commonConfig.UnLink(),
|
||||
commonConfig.Contain(),
|
||||
common_config.Get(),
|
||||
common_config.Set(),
|
||||
common_config.Delete(),
|
||||
common_config.Remove(),
|
||||
common_config.Link(),
|
||||
common_config.UnLink(),
|
||||
common_config.Contain(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
package commonConfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/pkg/errors"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/command/utils"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
)
|
||||
|
||||
func Link() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "link",
|
||||
Usage: "link common configurations to module configuration",
|
||||
Action: link.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName_ModuleName,
|
||||
}
|
||||
}
|
||||
|
||||
var link linkCommand
|
||||
|
||||
type linkCommand struct{}
|
||||
|
||||
func (g linkCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
moduleName := ctx.Args().Get(1)
|
||||
|
||||
if configName == "" {
|
||||
utils.PrintError(errors.New("need config name"))
|
||||
return
|
||||
}
|
||||
|
||||
if moduleName == "" {
|
||||
utils.PrintError(errors.New("need module name"))
|
||||
return
|
||||
}
|
||||
|
||||
commonConfigs, err := service.Config.GetMapCommonConfigByName()
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[configName]
|
||||
if !ok {
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", configName))
|
||||
return
|
||||
}
|
||||
|
||||
moduleConfiguration, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
ccIdNameMap := make(map[string]string)
|
||||
for name, value := range commonConfigs {
|
||||
ccIdNameMap[value.Id] = name
|
||||
}
|
||||
|
||||
needLink := true
|
||||
for _, configId := range moduleConfiguration.CommonConfigs {
|
||||
if config.Id == configId {
|
||||
needLink = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if needLink {
|
||||
moduleConfiguration.CommonConfigs = append(moduleConfiguration.CommonConfigs, config.Id)
|
||||
if linked, err := service.Config.UpdateConfigAndGetLinkCommon(*moduleConfiguration); err != nil {
|
||||
utils.PrintError(err)
|
||||
} else {
|
||||
for _, name := range linked {
|
||||
fmt.Printf("[%s] ", ccIdNameMap[name])
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
} else {
|
||||
for _, name := range moduleConfiguration.CommonConfigs {
|
||||
fmt.Printf("[%s] ", ccIdNameMap[name])
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package commonConfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/pkg/errors"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/command/utils"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
)
|
||||
|
||||
func UnLink() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "unlink",
|
||||
Usage: "unlink common configurations from module configuration",
|
||||
Action: unlink.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName_ModuleName,
|
||||
}
|
||||
}
|
||||
|
||||
var unlink unlinkCommand
|
||||
|
||||
type unlinkCommand struct{}
|
||||
|
||||
func (g unlinkCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
moduleName := ctx.Args().Get(1)
|
||||
|
||||
if configName == "" {
|
||||
utils.PrintError(errors.New("need config name"))
|
||||
return
|
||||
}
|
||||
|
||||
if moduleName == "" {
|
||||
utils.PrintError(errors.New("need module name"))
|
||||
return
|
||||
}
|
||||
|
||||
commonConfigs, err := service.Config.GetMapCommonConfigByName()
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[configName]
|
||||
if !ok {
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", configName))
|
||||
return
|
||||
}
|
||||
|
||||
moduleConfig, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
ccIdNameMap := make(map[string]string)
|
||||
for name, value := range commonConfigs {
|
||||
ccIdNameMap[value.Id] = name
|
||||
}
|
||||
|
||||
needUnlink := false
|
||||
for i, configId := range moduleConfig.CommonConfigs {
|
||||
if config.Id == configId {
|
||||
copy(moduleConfig.CommonConfigs[i:], moduleConfig.CommonConfigs[i+1:])
|
||||
moduleConfig.CommonConfigs[len(moduleConfig.CommonConfigs)-1] = ""
|
||||
moduleConfig.CommonConfigs = moduleConfig.CommonConfigs[:len(moduleConfig.CommonConfigs)-1]
|
||||
needUnlink = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if needUnlink {
|
||||
if linked, err := service.Config.UpdateConfigAndGetLinkCommon(*moduleConfig); err != nil {
|
||||
utils.PrintError(err)
|
||||
} else {
|
||||
for _, name := range linked {
|
||||
fmt.Printf("[%s] ", ccIdNameMap[name])
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
} else {
|
||||
for _, name := range moduleConfig.CommonConfigs {
|
||||
fmt.Printf("[%s] ", ccIdNameMap[name])
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package commonConfig
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -15,7 +15,7 @@ func Contain() cli.Command {
|
||||
Name: "contain",
|
||||
Usage: "availability common configuration in module",
|
||||
Action: contain.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,29 +29,27 @@ func (g containCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ccName := ctx.Args().First()
|
||||
configName := ctx.Args().First()
|
||||
|
||||
if ccName == "" {
|
||||
if configName == "" {
|
||||
utils.PrintError(errors.New("empty common config name"))
|
||||
return
|
||||
}
|
||||
|
||||
commonConfigs, err := service.Config.GetMapCommonConfigByName()
|
||||
config, err := service.Config.GetCommonConfigByName(configName)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[ccName]
|
||||
if !ok {
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", ccName))
|
||||
return
|
||||
}
|
||||
|
||||
links, err := service.Config.GetLinksCommonConfig(config.Id)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
} else {
|
||||
fmt.Printf("%v\n", links)
|
||||
fmt.Printf("config [%s] linked with next modules:\n", configName)
|
||||
for _, link := range links {
|
||||
fmt.Printf("[%s] ", link)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package commonConfig
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -16,7 +16,7 @@ func Delete() cli.Command {
|
||||
Name: "delete",
|
||||
Usage: "set common configurations",
|
||||
Action: deleteComm.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName_ConfigData,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,10 @@ func (g deleteCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ccName := ctx.Args().First()
|
||||
configName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
if ccName == "" {
|
||||
if configName == "" {
|
||||
utils.PrintError(errors.New("empty config name"))
|
||||
return
|
||||
}
|
||||
@@ -44,18 +44,12 @@ func (g deleteCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
commonConfigs, err := service.Config.GetMapCommonConfigByName()
|
||||
config, err := service.Config.GetCommonConfigByName(configName)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[ccName]
|
||||
if !ok {
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", ccName))
|
||||
return
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
utils.CreateUpdateCommonConfig("", config)
|
||||
} else {
|
||||
@@ -1,4 +1,4 @@
|
||||
package commonConfig
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -16,7 +16,7 @@ func Get() cli.Command {
|
||||
Name: "get",
|
||||
Usage: "get common configurations",
|
||||
Action: get.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName_ConfigData,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,18 +30,19 @@ func (g getCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ccName := ctx.Args().First()
|
||||
configName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
commonConfigs, err := service.Config.GetMapCommonConfigByName()
|
||||
commonConfigs, err := service.Config.GetMapNameCommonConfig()
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if ccName == "" {
|
||||
if configName == "" {
|
||||
fmt.Printf("available next common configs:\n")
|
||||
for name := range commonConfigs {
|
||||
fmt.Printf("%s ", name)
|
||||
fmt.Printf("[%s] ", name)
|
||||
}
|
||||
fmt.Println()
|
||||
return
|
||||
@@ -53,9 +54,9 @@ func (g getCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[ccName]
|
||||
config, ok := commonConfigs[configName]
|
||||
if !ok {
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", ccName))
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", configName))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"github.com/codegangsta/cli"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/command/utils"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/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) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
moduleName := ctx.Args().Get(1)
|
||||
|
||||
if err := service.Config.LinkCommonConfigToModule(configName, moduleName); err != nil {
|
||||
utils.PrintError(err)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package commonConfig
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -15,7 +15,7 @@ func Remove() cli.Command {
|
||||
Name: "remove",
|
||||
Usage: "remove common configurations",
|
||||
Action: remove.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,32 +29,27 @@ func (g removeCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ccName := ctx.Args().First()
|
||||
configName := ctx.Args().First()
|
||||
|
||||
if ccName == "" {
|
||||
if configName == "" {
|
||||
utils.PrintError(errors.New("empty common config name"))
|
||||
return
|
||||
}
|
||||
|
||||
commonConfigs, err := service.Config.GetMapCommonConfigByName()
|
||||
config, err := service.Config.GetCommonConfigByName(configName)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := commonConfigs[ccName]
|
||||
if !ok {
|
||||
utils.PrintError(errors.Errorf("common config [%s] not found", ccName))
|
||||
return
|
||||
}
|
||||
|
||||
links, deleted, err := service.Config.DeleteCommonConfig(config.Id)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if deleted {
|
||||
fmt.Printf("config [%s] deleted", config.Name)
|
||||
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)
|
||||
}
|
||||
@@ -1,17 +1,15 @@
|
||||
package commonConfig
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/sjson"
|
||||
"io/ioutil"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/cfg"
|
||||
"isp-ctl/command/utils"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Set() cli.Command {
|
||||
@@ -19,7 +17,7 @@ func Set() cli.Command {
|
||||
Name: "set",
|
||||
Usage: "set common configurations",
|
||||
Action: set.action,
|
||||
BashComplete: bash.CommonConfig.ConfigName_ConfigData,
|
||||
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,25 +31,25 @@ func (g setCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ccName := ctx.Args().First()
|
||||
configName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
changeObject := ctx.Args().Get(2)
|
||||
|
||||
if ccName == "" {
|
||||
if configName == "" {
|
||||
utils.PrintError(errors.New("empty config name"))
|
||||
return
|
||||
}
|
||||
|
||||
mapConfigByName, err := service.Config.GetMapCommonConfigByName()
|
||||
mapConfigByName, err := service.Config.GetMapNameCommonConfig()
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, ok := mapConfigByName[ccName]
|
||||
config, ok := mapConfigByName[configName]
|
||||
if !ok {
|
||||
config = cfg.CommonConfig{
|
||||
Name: ccName,
|
||||
Name: configName,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,36 +59,25 @@ func (g setCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if changeObject == "" {
|
||||
bytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
changeObject = string(bytes)
|
||||
}
|
||||
if changeObject == "" {
|
||||
utils.PrintError(errors.New("expected argument"))
|
||||
changeObject, err = utils.CheckChangeObject(changeObject)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
utils.CreateUpdateCommonConfig(changeObject, config)
|
||||
return
|
||||
} else {
|
||||
jsonObject, err := json.Marshal(config.Data)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
changeArgument := utils.ParseSetObject(changeObject)
|
||||
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
} else {
|
||||
utils.CreateUpdateCommonConfig(stringToChange, config)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package common_config
|
||||
|
||||
import (
|
||||
"github.com/codegangsta/cli"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/command/utils"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/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) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
configName := ctx.Args().First()
|
||||
moduleName := ctx.Args().Get(1)
|
||||
|
||||
if err := service.Config.UnlinkCommonConfigFromModule(configName, moduleName); err != nil {
|
||||
utils.PrintError(err)
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -15,7 +15,7 @@ func Delete() cli.Command {
|
||||
Name: "delete",
|
||||
Usage: "delete configuration by module_name",
|
||||
Action: deleteComm.action,
|
||||
BashComplete: bash.Module.ModuleName_ModuleData,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,6 @@ func (d deleteCommand) action(ctx *cli.Context) {
|
||||
|
||||
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
} else {
|
||||
utils.CreateUpdateConfig(stringToChange, moduleConfiguration)
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ func Get() cli.Command {
|
||||
Name: "get",
|
||||
Usage: "get configuration by module_name",
|
||||
Action: get.action,
|
||||
BashComplete: bash.Module.ModuleName_ModuleData,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
Flags: []cli.Flag{
|
||||
flag.WithCommonConfig,
|
||||
},
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ func Schema() cli.Command {
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrint,
|
||||
},
|
||||
BashComplete: bash.Module.ModuleName,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.Empty).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-21
@@ -3,14 +3,11 @@ package command
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/sjson"
|
||||
"io/ioutil"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/command/utils"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Set() cli.Command {
|
||||
@@ -18,7 +15,7 @@ func Set() cli.Command {
|
||||
Name: "set",
|
||||
Usage: "set configuration by module_name",
|
||||
Action: set.action,
|
||||
BashComplete: bash.Module.ModuleName_ModuleData,
|
||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +33,7 @@ func (s setCommand) action(ctx *cli.Context) {
|
||||
pathObject := ctx.Args().Get(1)
|
||||
changeObject := ctx.Args().Get(2)
|
||||
|
||||
moduleConfiguration, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
@@ -48,24 +45,16 @@ func (s setCommand) action(ctx *cli.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if changeObject == "" {
|
||||
bytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
changeObject = string(bytes)
|
||||
}
|
||||
if changeObject == "" {
|
||||
utils.PrintError(errors.New("expected argument"))
|
||||
changeObject, err = utils.CheckChangeObject(changeObject)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
utils.CreateUpdateConfig(changeObject, moduleConfiguration)
|
||||
return
|
||||
utils.CreateUpdateConfig(changeObject, config)
|
||||
} else {
|
||||
jsonObject, err := json.Marshal(moduleConfiguration.Data)
|
||||
jsonObject, err := json.Marshal(config.Data)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
@@ -74,10 +63,8 @@ func (s setCommand) action(ctx *cli.Context) {
|
||||
changeArgument := utils.ParseSetObject(changeObject)
|
||||
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
} else {
|
||||
utils.CreateUpdateConfig(stringToChange, moduleConfiguration)
|
||||
return
|
||||
utils.CreateUpdateConfig(stringToChange, config)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@ import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/gjson"
|
||||
"io/ioutil"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -28,6 +30,21 @@ func CheckPath(pathObject string) (string, error) {
|
||||
return pathObject, nil
|
||||
}
|
||||
|
||||
func CheckChangeObject(changeObject string) (string, error) {
|
||||
if changeObject == "" {
|
||||
bytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
changeObject = string(bytes)
|
||||
}
|
||||
if changeObject == "" {
|
||||
return "", errors.New("expected argument")
|
||||
} else {
|
||||
return changeObject, nil
|
||||
}
|
||||
}
|
||||
|
||||
func PrintAnswer(data interface{}) {
|
||||
if answer, err := json.MarshalIndent(data, "", " "); err != nil {
|
||||
PrintError(err)
|
||||
@@ -38,6 +55,7 @@ func PrintAnswer(data interface{}) {
|
||||
|
||||
func PrintError(err error) {
|
||||
fmt.Println("ERROR:", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
func ParseSetObject(argument string) interface{} {
|
||||
|
||||
Reference in new issue
Block a user