diff --git a/bash/autocomplete.go b/bash/autocomplete.go deleted file mode 100644 index 541f307..0000000 --- a/bash/autocomplete.go +++ /dev/null @@ -1,52 +0,0 @@ -package bash - -import ( - "fmt" - "github.com/codegangsta/cli" - "github.com/integration-system/bellows" - "isp-ctl/flag" - "isp-ctl/service" -) - -var ( - ModuleNameAndConfigurationPath = bashAction{enableConfigurationBash: true} - ModuleName = bashAction{enableConfigurationBash: false} -) - -type ( - bashAction struct{ enableConfigurationBash bool } -) - -func (b bashAction) Complete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - switch ctx.NArg() { - case 0: - b.moduleNameHelper() - case 1: - if b.enableConfigurationBash { - b.configurationPathHelper(ctx.Args().First(), ctx.Args().Get(1)) - } - } -} - -func (bashAction) moduleNameHelper() { - if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { - return - } else { - for _, module := range arrayOfModules { - fmt.Println(module.Name) - } - } -} - -func (bashAction) configurationPathHelper(moduleName, path string) { - if moduleConfiguration, _, err := service.Config.GetConfigurationAndJsonByModuleName(moduleName); err != nil { - return - } else { - for key, _ := range bellows.Flatten(moduleConfiguration.Data) { - fmt.Printf(".%v\n", key) - } - } -} diff --git a/bash/common_config.go b/bash/common_config.go new file mode 100644 index 0000000..2bb43ee --- /dev/null +++ b/bash/common_config.go @@ -0,0 +1,75 @@ +package bash + +import ( + "fmt" + "github.com/codegangsta/cli" + "github.com/integration-system/bellows" + "isp-ctl/flag" + "isp-ctl/service" +) + +var CommonConfig commonConfig + +type commonConfig struct{} + +func (commonConfig) LinkUnlink(ctx *cli.Context) { + if err := flag.CheckGlobal(ctx); err != nil { + return + } + commonConfigs, err := service.Config.GetMapCommonConfigByName() + if err != nil { + return + } + switch ctx.NArg() { + case 0: + for _, config := range commonConfigs { + fmt.Println(config.Name) + } + case 1: + if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { + return + } else { + for _, module := range arrayOfModules { + fmt.Println(module.Name) + } + } + } +} + +func (commonConfig) GetSetDelete(ctx *cli.Context) { + if err := flag.CheckGlobal(ctx); err != nil { + return + } + commonConfigs, err := service.Config.GetMapCommonConfigByName() + if err != nil { + return + } + switch ctx.NArg() { + case 0: + for _, config := range commonConfigs { + fmt.Println(config.Name) + } + case 1: + if config, ok := commonConfigs[ctx.Args().First()]; ok { + for key, _ := range bellows.Flatten(config.Data) { + fmt.Printf(".%v\n", key) + } + } + } +} + +func (commonConfig) Remove(ctx *cli.Context) { + if err := flag.CheckGlobal(ctx); err != nil { + return + } + commonConfigs, err := service.Config.GetMapCommonConfigByName() + if err != nil { + return + } + switch ctx.NArg() { + case 0: + for _, config := range commonConfigs { + fmt.Println(config.Name) + } + } +} diff --git a/bash/module.go b/bash/module.go new file mode 100644 index 0000000..cfac811 --- /dev/null +++ b/bash/module.go @@ -0,0 +1,53 @@ +package bash + +import ( + "fmt" + "github.com/codegangsta/cli" + "github.com/integration-system/bellows" + "isp-ctl/flag" + "isp-ctl/service" +) + +var Module module + +type module struct{} + +func (module) GetSetDelete(ctx *cli.Context) { + if err := flag.CheckGlobal(ctx); err != nil { + return + } + switch ctx.NArg() { + case 0: + if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { + return + } else { + for _, module := range arrayOfModules { + fmt.Println(module.Name) + } + } + case 1: + if moduleConfiguration, _, err := service.Config.GetConfigurationAndJsonByModuleName(ctx.Args().Get(1)); err != nil { + return + } else { + for key, _ := range bellows.Flatten(moduleConfiguration.Data) { + fmt.Printf(".%v\n", key) + } + } + } +} + +func (module) Schema(ctx *cli.Context) { + if err := flag.CheckGlobal(ctx); err != nil { + return + } + switch ctx.NArg() { + case 0: + if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { + return + } else { + for _, module := range arrayOfModules { + fmt.Println(module.Name) + } + } + } +} diff --git a/command/commonConfig/delete.go b/command/commonConfig/delete.go index 5fd8416..054aaea 100644 --- a/command/commonConfig/delete.go +++ b/command/commonConfig/delete.go @@ -2,11 +2,10 @@ package commonConfig import ( "encoding/json" - "fmt" "github.com/codegangsta/cli" - "github.com/integration-system/bellows" "github.com/pkg/errors" "github.com/tidwall/sjson" + "isp-ctl/bash" "isp-ctl/command/utils" "isp-ctl/flag" "isp-ctl/service" @@ -17,7 +16,7 @@ func Delete() cli.Command { Name: "delete", Usage: "set common configurations", Action: deleteComm.action, - BashComplete: deleteComm.bashComplete, + BashComplete: bash.CommonConfig.GetSetDelete, } } @@ -72,25 +71,3 @@ func (g deleteCommand) action(ctx *cli.Context) { } } } - -func (g deleteCommand) bashComplete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - commonConfigs, err := service.Config.GetMapCommonConfigByName() - if err != nil { - return - } - switch ctx.NArg() { - case 0: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } - case 1: - if config, ok := commonConfigs[ctx.Args().First()]; ok { - for key, _ := range bellows.Flatten(config.Data) { - fmt.Printf(".%v\n", key) - } - } - } -} diff --git a/command/commonConfig/get.go b/command/commonConfig/get.go index 7f36550..2b3eb3b 100644 --- a/command/commonConfig/get.go +++ b/command/commonConfig/get.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" "github.com/codegangsta/cli" - "github.com/integration-system/bellows" "github.com/pkg/errors" + "isp-ctl/bash" "isp-ctl/command/utils" "isp-ctl/flag" "isp-ctl/service" @@ -16,7 +16,7 @@ func Get() cli.Command { Name: "get", Usage: "get common configurations", Action: get.action, - BashComplete: get.bashComplete, + BashComplete: bash.CommonConfig.GetSetDelete, } } @@ -67,25 +67,3 @@ func (g getCommand) action(ctx *cli.Context) { utils.CheckObject(jsonObject, pathObject) } } - -func (g getCommand) bashComplete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - commonConfigs, err := service.Config.GetMapCommonConfigByName() - if err != nil { - return - } - switch ctx.NArg() { - case 0: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } - case 1: - if config, ok := commonConfigs[ctx.Args().First()]; ok { - for key, _ := range bellows.Flatten(config.Data) { - fmt.Printf(".%v\n", key) - } - } - } -} diff --git a/command/commonConfig/link.go b/command/commonConfig/link.go index e2e53cc..a30ed53 100644 --- a/command/commonConfig/link.go +++ b/command/commonConfig/link.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/codegangsta/cli" "github.com/pkg/errors" + "isp-ctl/bash" "isp-ctl/command/utils" "isp-ctl/flag" "isp-ctl/service" @@ -14,7 +15,7 @@ func Link() cli.Command { Name: "link", Usage: "link common configurations to module configuration", Action: link.action, - BashComplete: link.bashComplete, + BashComplete: bash.CommonConfig.LinkUnlink, } } @@ -89,27 +90,3 @@ func (g linkCommand) action(ctx *cli.Context) { fmt.Printf("\n") } } - -func (g linkCommand) bashComplete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - commonConfigs, err := service.Config.GetMapCommonConfigByName() - if err != nil { - return - } - switch ctx.NArg() { - case 0: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } - case 1: - if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { - return - } else { - for _, module := range arrayOfModules { - fmt.Println(module.Name) - } - } - } -} diff --git a/command/commonConfig/remove.go b/command/commonConfig/remove.go index ad508af..c9d24b6 100644 --- a/command/commonConfig/remove.go +++ b/command/commonConfig/remove.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/codegangsta/cli" "github.com/pkg/errors" + "isp-ctl/bash" "isp-ctl/command/utils" "isp-ctl/flag" "isp-ctl/service" @@ -14,7 +15,7 @@ func Remove() cli.Command { Name: "remove", Usage: "remove common configurations", Action: remove.action, - BashComplete: remove.bashComplete, + BashComplete: bash.CommonConfig.Remove, } } @@ -53,19 +54,3 @@ func (g removeCommand) action(ctx *cli.Context) { fmt.Printf("count deleted config: %d\n", deleted) } } - -func (g removeCommand) bashComplete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - commonConfigs, err := service.Config.GetMapCommonConfigByName() - if err != nil { - return - } - switch ctx.NArg() { - case 0: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } - } -} diff --git a/command/commonConfig/set.go b/command/commonConfig/set.go index 988501f..4e35ed8 100644 --- a/command/commonConfig/set.go +++ b/command/commonConfig/set.go @@ -2,12 +2,11 @@ package commonConfig import ( "encoding/json" - "fmt" "github.com/codegangsta/cli" - "github.com/integration-system/bellows" "github.com/pkg/errors" "github.com/tidwall/sjson" "io/ioutil" + "isp-ctl/bash" "isp-ctl/cfg" "isp-ctl/command/utils" "isp-ctl/flag" @@ -20,7 +19,7 @@ func Set() cli.Command { Name: "set", Usage: "set common configurations", Action: set.action, - BashComplete: set.bashComplete, + BashComplete: bash.CommonConfig.GetSetDelete, } } @@ -95,25 +94,3 @@ func (g setCommand) action(ctx *cli.Context) { } } } - -func (g setCommand) bashComplete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - commonConfigs, err := service.Config.GetMapCommonConfigByName() - if err != nil { - return - } - switch ctx.NArg() { - case 0: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } - case 1: - if config, ok := commonConfigs[ctx.Args().First()]; ok { - for key, _ := range bellows.Flatten(config.Data) { - fmt.Printf(".%v\n", key) - } - } - } -} diff --git a/command/commonConfig/unlink.go b/command/commonConfig/unlink.go index 0958c2a..9c2a4e3 100644 --- a/command/commonConfig/unlink.go +++ b/command/commonConfig/unlink.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/codegangsta/cli" "github.com/pkg/errors" + "isp-ctl/bash" "isp-ctl/command/utils" "isp-ctl/flag" "isp-ctl/service" @@ -14,7 +15,7 @@ func UnLink() cli.Command { Name: "unlink", Usage: "unlink common configurations from module configuration", Action: unlink.action, - BashComplete: unlink.bashComplete, + BashComplete: bash.CommonConfig.LinkUnlink, } } @@ -91,27 +92,3 @@ func (g unlinkCommand) action(ctx *cli.Context) { fmt.Printf("\n") } } - -func (g unlinkCommand) bashComplete(ctx *cli.Context) { - if err := flag.CheckGlobal(ctx); err != nil { - return - } - commonConfigs, err := service.Config.GetMapCommonConfigByName() - if err != nil { - return - } - switch ctx.NArg() { - case 0: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } - case 1: - if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { - return - } else { - for _, module := range arrayOfModules { - fmt.Println(module.Name) - } - } - } -} diff --git a/command/delete.go b/command/delete.go index f473455..3071682 100644 --- a/command/delete.go +++ b/command/delete.go @@ -14,7 +14,7 @@ func Delete() cli.Command { Name: "delete", Usage: "delete configuration by module_name", Action: deleteComm.action, - BashComplete: bash.ModuleNameAndConfigurationPath.Complete, + BashComplete: bash.Module.GetSetDelete, } } diff --git a/command/get.go b/command/get.go index d848f6d..d8dcf09 100644 --- a/command/get.go +++ b/command/get.go @@ -13,7 +13,7 @@ func Get() cli.Command { Name: "get", Usage: "get configuration by module_name", Action: get.action, - BashComplete: bash.ModuleNameAndConfigurationPath.Complete, + BashComplete: bash.Module.GetSetDelete, } } diff --git a/command/schema.go b/command/schema.go index 3ec19de..7f4b91c 100644 --- a/command/schema.go +++ b/command/schema.go @@ -20,7 +20,7 @@ func Schema() cli.Command { Flags: []cli.Flag{ flag.OutPrint, }, - BashComplete: bash.ModuleName.Complete, + BashComplete: bash.Module.Schema, } } diff --git a/command/set.go b/command/set.go index fe9fe76..9367840 100644 --- a/command/set.go +++ b/command/set.go @@ -17,7 +17,7 @@ func Set() cli.Command { Name: "set", Usage: "set configuration by module_name", Action: set.action, - BashComplete: bash.ModuleNameAndConfigurationPath.Complete, + BashComplete: bash.Module.GetSetDelete, } } diff --git a/main.go b/main.go index 2746ea3..1f6773e 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,8 @@ func initCommands() { ispctl [flag...] get module_name property_path ispctl [flag...] set module_name property_path [new_object] ispctl [flag...] delete module_name property_path - ispctl [flag...] schema module_name [local_flag]` + ispctl [flag...] schema module_name [local_flag] + ispctl [flag...] common ` app.Version = version app.Flags = []cli.Flag{ @@ -44,8 +45,8 @@ func initCommands() { command.CommonConfig(), } app.BashComplete = func(context *cli.Context) { - for _, command := range app.Commands { - fmt.Println(command.Name) + for _, appCommand := range app.Commands { + fmt.Println(appCommand.Name) } }