From 1f794377e5c990ffed0850322d150ed49666f3ca Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Wed, 21 Aug 2019 19:10:24 +0300 Subject: [PATCH] add(bash complete) --- CHANGELOG.md | 1 + Gopkg.lock | 16 ++++++++++++--- command/bash.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ command/delete.go | 7 ++++--- command/get.go | 7 ++++--- command/schema.go | 5 +++-- command/set.go | 7 ++++--- main.go | 3 +-- 8 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 command/bash.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 2214e64..fce8cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## v1.2.0 * add new command `schema` +* add bash complete ## v1.1.0 * fix status response * add checking schema to set configuration diff --git a/Gopkg.lock b/Gopkg.lock index acdb14d..fbf8447 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -32,6 +32,14 @@ revision = "6c65a5562fc06764971b7c5d05c76c75e84bdbf7" version = "v1.3.2" +[[projects]] + branch = "master" + digest = "1:2cf4ff28b309c93a52b3329c7a22d5375b137249d4ed63c9d40707a1b253f7df" + name = "github.com/integration-system/bellows" + packages = ["."] + pruneopts = "UT" + revision = "244325e8836c364c86f308e3aef7e979cb007114" + [[projects]] branch = "master" digest = "1:abd0cd78d6d27a98080f2b2a7fd39bb2f3d3eef6aa20c720093129f04fb41d78" @@ -41,7 +49,7 @@ revision = "368713356c71961354e09ab9b734740ebdd94784" [[projects]] - digest = "1:cd9785ed4d12c737f23ec65b08fffa4e235cbf8689290211dd95eeb0f1ae7e15" + digest = "1:ecb402b81b0ed58f59ee14a1587f94c9cef1d8710f0eac3e1cbbbc2cb254f8e8" name = "github.com/integration-system/isp-lib" packages = [ "config/schema", @@ -51,8 +59,8 @@ "utils", ] pruneopts = "UT" - revision = "d2cc77b8fb8648ae7f8efdf1d1b834e8e57b56d7" - version = "v1.5.2" + revision = "d8ea213ec2182662432624bf09cf6726b56f12c4" + version = "v1.6.0" [[projects]] branch = "master" @@ -316,8 +324,10 @@ analyzer-version = 1 input-imports = [ "github.com/codegangsta/cli", + "github.com/integration-system/bellows", "github.com/integration-system/isp-lib/config/schema", "github.com/integration-system/isp-lib/http", + "github.com/integration-system/isp-lib/logger", "github.com/integration-system/isp-lib/structure", "github.com/olekukonko/tablewriter", "github.com/tidwall/gjson", diff --git a/command/bash.go b/command/bash.go new file mode 100644 index 0000000..43b9f97 --- /dev/null +++ b/command/bash.go @@ -0,0 +1,51 @@ +package command + +import ( + "fmt" + "github.com/codegangsta/cli" + "github.com/integration-system/bellows" + "isp-ctl/service" +) + +var ( + bash = bashAction{enableConfigurationBash: true} + bashSchema = bashAction{enableConfigurationBash: false} +) + +type ( + bashAction struct{ enableConfigurationBash bool } +) + +func (b bashAction) run(ctx *cli.Context) { + if err := checkFlags(ctx); err != nil { + return + } + switch ctx.NArg() { + case 0: + b.moduleNameHelper() + case 1: + if b.enableConfigurationBash { + b.configurationHelper(ctx.Args().First(), ctx.Args().Get(1)) + } + } +} + +func (bashAction) moduleNameHelper() { + if arrayOfModules, err := service.ConfigClient.GetAvailableConfigs(); err != nil { + return + } else { + for _, module := range arrayOfModules { + fmt.Println(module.Name) + } + } +} + +func (bashAction) configurationHelper(moduleName, path string) { + moduleConfiguration, _ := getModuleConfiguration(moduleName) + if moduleConfiguration == nil { + return + } + for key, _ := range bellows.Flatten(moduleConfiguration.Data) { + fmt.Printf(".%v\n", key) + } +} diff --git a/command/delete.go b/command/delete.go index 61b483d..29e0837 100644 --- a/command/delete.go +++ b/command/delete.go @@ -7,9 +7,10 @@ import ( func Delete() cli.Command { return cli.Command{ - Name: "delete", - Usage: "delete configuration by module_name", - Action: deleteComm.action, + Name: "delete", + Usage: "delete configuration by module_name", + Action: deleteComm.action, + BashComplete: bash.run, } } diff --git a/command/get.go b/command/get.go index 5690aaa..feba54d 100644 --- a/command/get.go +++ b/command/get.go @@ -10,9 +10,10 @@ import ( func Get() cli.Command { return cli.Command{ - Name: "get", - Usage: "get configuration by module_name", - Action: get.action, + Name: "get", + Usage: "get configuration by module_name", + Action: get.action, + BashComplete: bash.run, } } diff --git a/command/schema.go b/command/schema.go index 14ff6e5..8b13e48 100644 --- a/command/schema.go +++ b/command/schema.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/codegangsta/cli" + convert "github.com/integration-system/isp-lib/config/schema" "html/template" "isp-ctl/flag" "isp-ctl/service" @@ -18,6 +19,7 @@ func Schema() cli.Command { Flags: []cli.Flag{ flag.OutPrint, }, + BashComplete: bashSchema.run, } } @@ -36,7 +38,6 @@ func (s schemaCommand) action(c *cli.Context) { schema := make(map[string]interface{}) schema["title"] = moduleName schema["schema"] = schemaConfig - switch c.String(flag.OutPrint.Name) { case flag.OutPrintJsonValue: printAnswer(schema) @@ -56,7 +57,7 @@ func (s schemaCommand) getSchemaConfig(moduleName string) interface{} { printError(err) return nil } else { - return schema.Schema + return convert.DereferenceSchema(schema.Schema) } } diff --git a/command/set.go b/command/set.go index 5fa3bf1..98a3763 100644 --- a/command/set.go +++ b/command/set.go @@ -12,9 +12,10 @@ import ( func Set() cli.Command { return cli.Command{ - Name: "set", - Usage: "set configuration by module_name", - Action: set.action, + Name: "set", + Usage: "set configuration by module_name", + Action: set.action, + BashComplete: bash.run, } } diff --git a/main.go b/main.go index b29840b..fabe7cb 100644 --- a/main.go +++ b/main.go @@ -27,14 +27,13 @@ func initCommands() { ispctl [flag...] schema module_name [local_flag]` app.Version = version - app.Flags = []cli.Flag{ flag.Host, flag.Uuid, flag.Color, flag.Unsafe, } - + app.EnableBashCompletion = true app.Commands = []cli.Command{ command.Status(), command.Get(),