fix json output

This commit is contained in:
d1slike committed 2020-06-03 13:27:40 +03:00
1 parent b65cda4099
commit 3eeb933231
28 files changed
+180 -595

No files matched your search

+6 -5
View File
@@ -2,17 +2,17 @@ package command
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/urfave/cli/v2"
"isp-ctl/command/common_config"
)
func CommonConfig() cli.Command {
return cli.Command{
func CommonConfig() *cli.Command {
return &cli.Command{
Name: "common",
Usage: "access to common configs commands",
Action: cc.action,
BashComplete: cc.bachComplete,
Subcommands: []cli.Command{
Subcommands: []*cli.Command{
common_config.Get(),
common_config.Set(),
common_config.Delete(),
@@ -28,7 +28,7 @@ var cc commonConfigCommand
type commonConfigCommand struct{}
func (g commonConfigCommand) action(ctx *cli.Context) {
func (g commonConfigCommand) action(ctx *cli.Context) error {
if ctx.Args().First() == "" {
fmt.Print("need use command: ")
for _, comm := range CommonConfig().Subcommands {
@@ -36,6 +36,7 @@ func (g commonConfigCommand) action(ctx *cli.Context) {
}
fmt.Printf("\n")
}
return nil
}
func (g commonConfigCommand) bachComplete(ctx *cli.Context) {
+9 -12
View File
@@ -2,16 +2,15 @@ package common_config
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Contain() cli.Command {
return cli.Command{
func Contain() *cli.Command {
return &cli.Command{
Name: "contain",
Usage: "availability common configuration in module",
Action: contain.action,
@@ -23,28 +22,25 @@ var contain containCommand
type containCommand struct{}
func (g containCommand) action(ctx *cli.Context) {
func (g containCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
if configName == "" {
utils.PrintError(errors.New("empty common config name"))
return
return errors.New("empty common config name")
}
config, err := service.Config.GetCommonConfigByName(configName)
if err != nil {
utils.PrintError(err)
return
return err
}
links, err := service.Config.GetLinksCommonConfig(config.Id)
if err != nil {
utils.PrintError(err)
return err
} else {
fmt.Printf("config [%s] linked with next modules:\n", configName)
for _, link := range links {
@@ -52,4 +48,5 @@ func (g containCommand) action(ctx *cli.Context) {
}
fmt.Printf("\n")
}
return nil
}
+12 -17
View File
@@ -2,17 +2,17 @@ package common_config
import (
"encoding/json"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Delete() cli.Command {
return cli.Command{
func Delete() *cli.Command {
return &cli.Command{
Name: "delete",
Usage: "set common configurations",
Action: deleteComm.action,
@@ -24,44 +24,39 @@ var deleteComm deleteCommand
type deleteCommand struct{}
func (g deleteCommand) action(ctx *cli.Context) {
func (g deleteCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
pathObject := ctx.Args().Get(1)
if configName == "" {
utils.PrintError(errors.New("empty config name"))
return
return errors.New("empty config name")
}
pathObject, err := utils.CheckPath(pathObject)
if err != nil {
utils.PrintError(err)
return
return err
}
config, err := service.Config.GetCommonConfigByName(configName)
if err != nil {
utils.PrintError(err)
return
return err
}
if pathObject == "" {
utils.CreateUpdateCommonConfig("", config)
return utils.CreateUpdateCommonConfig("", config)
} else {
jsonObject, err := json.Marshal(config.Data)
if err != nil {
utils.PrintError(err)
return
return err
}
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
utils.PrintError(err)
return err
} else {
utils.CreateUpdateCommonConfig(stringToChange, config)
return utils.CreateUpdateCommonConfig(stringToChange, config)
}
}
}
+11 -14
View File
@@ -3,16 +3,16 @@ package common_config
import (
"encoding/json"
"fmt"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Get() cli.Command {
return cli.Command{
func Get() *cli.Command {
return &cli.Command{
Name: "get",
Usage: "get common configurations",
Action: get.action,
@@ -24,10 +24,9 @@ var get getCommand
type getCommand struct{}
func (g getCommand) action(ctx *cli.Context) {
func (g getCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
@@ -35,8 +34,7 @@ func (g getCommand) action(ctx *cli.Context) {
commonConfigs, err := service.Config.GetMapNameCommonConfig()
if err != nil {
utils.PrintError(err)
return
return err
}
if configName == "" {
@@ -45,26 +43,25 @@ func (g getCommand) action(ctx *cli.Context) {
fmt.Printf("[%s] ", name)
}
fmt.Println()
return
return nil
}
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
utils.PrintError(err)
return
return err
}
config, ok := commonConfigs[configName]
if !ok {
utils.PrintError(errors.Errorf("common config [%s] not found", configName))
return
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 {
utils.PrintError(err)
return err
} else {
utils.CheckObject(jsonObject, pathObject)
}
return nil
}
+7 -8
View File
@@ -1,15 +1,14 @@
package common_config
import (
"github.com/codegangsta/cli"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Link() cli.Command {
return cli.Command{
func Link() *cli.Command {
return &cli.Command{
Name: "link",
Usage: "link common configurations to module configuration",
Action: link.action,
@@ -21,16 +20,16 @@ var link linkCommand
type linkCommand struct{}
func (g linkCommand) action(ctx *cli.Context) {
func (g linkCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
moduleName := ctx.Args().Get(1)
if err := service.Config.LinkCommonConfigToModule(configName, moduleName); err != nil {
utils.PrintError(err)
return err
}
return nil
}
+9 -13
View File
@@ -2,16 +2,15 @@ package common_config
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Remove() cli.Command {
return cli.Command{
func Remove() *cli.Command {
return &cli.Command{
Name: "remove",
Usage: "remove common configurations",
Action: remove.action,
@@ -23,29 +22,25 @@ var remove removeCommand
type removeCommand struct{}
func (g removeCommand) action(ctx *cli.Context) {
func (g removeCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
if configName == "" {
utils.PrintError(errors.New("empty common config name"))
return
return errors.New("empty common config name")
}
config, err := service.Config.GetCommonConfigByName(configName)
if err != nil {
utils.PrintError(err)
return
return err
}
links, deleted, err := service.Config.DeleteCommonConfig(config.Id)
if err != nil {
utils.PrintError(err)
return
return err
}
if deleted {
@@ -53,4 +48,5 @@ func (g removeCommand) action(ctx *cli.Context) {
} else {
fmt.Printf("config [%s] not deleted, need unlink in next modules:\n%v\n", config.Name, links)
}
return nil
}
+13 -19
View File
@@ -2,9 +2,9 @@ package common_config
import (
"encoding/json"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/cfg"
"isp-ctl/command/utils"
@@ -12,8 +12,8 @@ import (
"isp-ctl/service"
)
func Set() cli.Command {
return cli.Command{
func Set() *cli.Command {
return &cli.Command{
Name: "set",
Usage: "set common configurations",
Action: set.action,
@@ -25,10 +25,9 @@ var set setCommand
type setCommand struct{}
func (g setCommand) action(ctx *cli.Context) {
func (g setCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
@@ -36,14 +35,12 @@ func (g setCommand) action(ctx *cli.Context) {
changeObject := ctx.Args().Get(2)
if configName == "" {
utils.PrintError(errors.New("empty config name"))
return
return errors.New("empty config name")
}
mapConfigByName, err := service.Config.GetMapNameCommonConfig()
if err != nil {
utils.PrintError(err)
return
return err
}
config, ok := mapConfigByName[configName]
@@ -55,29 +52,26 @@ func (g setCommand) action(ctx *cli.Context) {
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
utils.PrintError(err)
return
return err
}
changeObject, err = utils.CheckChangeObject(changeObject)
if err != nil {
utils.PrintError(err)
return
return err
}
if pathObject == "" {
utils.CreateUpdateCommonConfig(changeObject, config)
return utils.CreateUpdateCommonConfig(changeObject, config)
} else {
jsonObject, err := json.Marshal(config.Data)
if err != nil {
utils.PrintError(err)
return
return err
}
changeArgument := utils.ParseSetObject(changeObject)
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
utils.PrintError(err)
return err
} else {
utils.CreateUpdateCommonConfig(stringToChange, config)
return utils.CreateUpdateCommonConfig(stringToChange, config)
}
}
}
+7 -8
View File
@@ -1,15 +1,14 @@
package common_config
import (
"github.com/codegangsta/cli"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func UnLink() cli.Command {
return cli.Command{
func UnLink() *cli.Command {
return &cli.Command{
Name: "unlink",
Usage: "unlink common configurations from module configuration",
Action: unlink.action,
@@ -21,16 +20,16 @@ var unlink unlinkCommand
type unlinkCommand struct{}
func (g unlinkCommand) action(ctx *cli.Context) {
func (g unlinkCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
configName := ctx.Args().First()
moduleName := ctx.Args().Get(1)
if err := service.Config.UnlinkCommonConfigFromModule(configName, moduleName); err != nil {
utils.PrintError(err)
return err
}
return nil
}
+11 -15
View File
@@ -2,16 +2,16 @@ package command
import (
"encoding/json"
"github.com/codegangsta/cli"
"github.com/tidwall/sjson"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Delete() cli.Command {
return cli.Command{
func Delete() *cli.Command {
return &cli.Command{
Name: "delete",
Usage: "delete configuration by module_name",
Action: deleteComm.action,
@@ -23,39 +23,35 @@ var deleteComm deleteCommand
type deleteCommand struct{}
func (d deleteCommand) action(ctx *cli.Context) {
func (d deleteCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
moduleName := ctx.Args().First()
pathObject := ctx.Args().Get(1)
moduleConfiguration, err := service.Config.GetConfigurationByModuleName(moduleName)
if err != nil {
utils.PrintError(err)
return
return err
}
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
utils.PrintError(err)
return
return err
}
if pathObject == "" {
utils.CreateUpdateConfig("", moduleConfiguration)
return utils.CreateUpdateConfig("", moduleConfiguration)
} else {
jsonObject, err := json.Marshal(moduleConfiguration.Data)
if err != nil {
utils.PrintError(err)
return
return err
}
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
utils.PrintError(err)
return err
} else {
utils.CreateUpdateConfig(stringToChange, moduleConfiguration)
return utils.CreateUpdateConfig(stringToChange, moduleConfiguration)
}
}
}
+10 -14
View File
@@ -2,15 +2,15 @@ package command
import (
"encoding/json"
"github.com/codegangsta/cli"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Get() cli.Command {
return cli.Command{
func Get() *cli.Command {
return &cli.Command{
Name: "get",
Usage: "get configuration by module_name",
Action: get.action,
@@ -25,32 +25,28 @@ var get getCommand
type getCommand struct{}
func (g getCommand) action(ctx *cli.Context) {
func (g getCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
moduleName := ctx.Args().First()
pathObject := ctx.Args().Get(1)
config, err := service.Config.GetConfigurationByModuleName(moduleName)
if err != nil {
utils.PrintError(err)
return
return err
}
data := config.Data
if ctx.Bool(flag.WithCommonConfig.Name) {
if data, err = service.Config.CompileDataWithCommonConfigs(data, config.CommonConfigs); err != nil {
utils.PrintError(err)
return
return err
}
}
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
utils.PrintError(err)
return
return err
}
if pathObject == "" {
@@ -58,9 +54,9 @@ func (g getCommand) action(ctx *cli.Context) {
} else {
jsonObject, err := json.Marshal(data)
if err != nil {
utils.PrintError(err)
return
return err
}
utils.CheckObject(jsonObject, pathObject)
}
return nil
}
+7 -8
View File
@@ -1,8 +1,8 @@
package command
import (
"github.com/codegangsta/cli"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"html/template"
"isp-ctl/bash"
"isp-ctl/command/utils"
@@ -12,8 +12,8 @@ import (
"os"
)
func Schema() cli.Command {
return cli.Command{
func Schema() *cli.Command {
return &cli.Command{
Name: "schema",
Usage: "get schema configuration by module_name",
Action: schema.action,
@@ -28,10 +28,9 @@ var schema schemaCommand
type schemaCommand struct{}
func (s schemaCommand) action(ctx *cli.Context) {
func (s schemaCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
moduleName := ctx.Args().First()
@@ -45,10 +44,10 @@ func (s schemaCommand) action(ctx *cli.Context) {
case flag.OutPrintHtmlValue:
s.printHtml(schema)
default:
utils.PrintError(errors.Errorf(
"invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue))
return errors.Errorf("invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue)
}
}
return nil
}
func (s schemaCommand) getSchemaConfig(moduleName string) interface{} {
+12 -17
View File
@@ -2,16 +2,16 @@ package command
import (
"encoding/json"
"github.com/codegangsta/cli"
"github.com/tidwall/sjson"
"github.com/urfave/cli/v2"
"isp-ctl/bash"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
)
func Set() cli.Command {
return cli.Command{
func Set() *cli.Command {
return &cli.Command{
Name: "set",
Usage: "set configuration by module_name",
Action: set.action,
@@ -23,10 +23,9 @@ var set setCommand
type setCommand struct{}
func (s setCommand) action(ctx *cli.Context) {
func (s setCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
moduleName := ctx.Args().First()
@@ -35,36 +34,32 @@ func (s setCommand) action(ctx *cli.Context) {
config, err := service.Config.GetConfigurationByModuleName(moduleName)
if err != nil {
utils.PrintError(err)
return
return err
}
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
utils.PrintError(err)
return
return err
}
changeObject, err = utils.CheckChangeObject(changeObject)
if err != nil {
utils.PrintError(err)
return
return err
}
if pathObject == "" {
utils.CreateUpdateConfig(changeObject, config)
return utils.CreateUpdateConfig(changeObject, config)
} else {
jsonObject, err := json.Marshal(config.Data)
if err != nil {
utils.PrintError(err)
return
return err
}
changeArgument := utils.ParseSetObject(changeObject)
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
utils.PrintError(err)
return err
} else {
utils.CreateUpdateConfig(stringToChange, config)
return utils.CreateUpdateConfig(stringToChange, config)
}
}
}
+7 -7
View File
@@ -2,8 +2,8 @@ package command
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"isp-ctl/command/utils"
"isp-ctl/flag"
"isp-ctl/service"
@@ -20,8 +20,8 @@ const (
tableConnectedStatus = "CONNECTED"
)
func Status() cli.Command {
return cli.Command{
func Status() *cli.Command {
return &cli.Command{
Name: "status",
Usage: "get available configs",
Action: status.action,
@@ -35,13 +35,12 @@ var status statusCommand
type statusCommand struct{}
func (statusCommand) action(ctx *cli.Context) {
func (statusCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
utils.PrintError(err)
return
return err
}
if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil {
utils.PrintError(err)
return err
} else {
switch ctx.String(flag.OutPrintStatus.Name) {
case flag.OutPrintJsonValue:
@@ -66,4 +65,5 @@ func (statusCommand) action(ctx *cli.Context) {
table.Render()
}
}
return nil
}
+1 -1
View File
@@ -47,7 +47,7 @@ func CheckChangeObject(changeObject string) (string, error) {
}
func PrintAnswer(data interface{}) {
buffer := bytes.NewBuffer(make([]byte, 1024))
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
+3 -2
View File
@@ -5,10 +5,11 @@ import (
"isp-ctl/service"
)
func CreateUpdateCommonConfig(stringToChange string, configuration cfg.CommonConfig) {
func CreateUpdateCommonConfig(stringToChange string, configuration cfg.CommonConfig) error {
if answer, err := service.Config.CreateUpdateCommonConfig(stringToChange, configuration); err != nil {
PrintError(err)
return err
} else if answer != nil {
PrintAnswer(answer)
}
return nil
}
+3 -2
View File
@@ -5,10 +5,11 @@ import (
"isp-ctl/service"
)
func CreateUpdateConfig(stringToChange string, configuration *cfg.Config) {
func CreateUpdateConfig(stringToChange string, configuration *cfg.Config) error {
if answer, err := service.Config.CreateUpdateConfig(stringToChange, configuration); err != nil {
PrintError(err)
return err
} else if answer != nil {
PrintAnswer(answer)
}
return nil
}