From 51f6ed4c7063eeb70886a4966ac317db1d0938b3 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 27 Dec 2019 16:38:55 +0300 Subject: [PATCH] add output format for `status` --- .version | 2 +- CHANGELOG.md | 2 ++ command/schema.go | 4 ++-- command/status.go | 38 +++++++++++++++++++++++--------------- flag/description.go | 3 ++- flag/structure.go | 3 ++- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/.version b/.version index 38f77a6..7ec1d6d 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -2.0.1 +2.1.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 081eedf..998b22d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## v2.1.0 +* add output format for `status` ## v2.0.1 * print module version in `status` command ## v2.0.0 diff --git a/command/schema.go b/command/schema.go index 843d79f..88b7076 100644 --- a/command/schema.go +++ b/command/schema.go @@ -18,7 +18,7 @@ func Schema() cli.Command { Usage: "get schema configuration by module_name", Action: schema.action, Flags: []cli.Flag{ - flag.OutPrint, + flag.OutPrintSchema, }, BashComplete: bash.Get(bash.ModuleName, bash.Empty).Complete, } @@ -39,7 +39,7 @@ func (s schemaCommand) action(ctx *cli.Context) { schema := make(map[string]interface{}) schema["title"] = moduleName schema["schema"] = schemaConfig - switch ctx.String(flag.OutPrint.Name) { + switch ctx.String(flag.OutPrintSchema.Name) { case flag.OutPrintJsonValue: utils.PrintAnswer(schema) case flag.OutPrintHtmlValue: diff --git a/command/status.go b/command/status.go index 7dea233..6d49ed8 100644 --- a/command/status.go +++ b/command/status.go @@ -25,6 +25,9 @@ func Status() cli.Command { Name: "status", Usage: "get available configs", Action: status.action, + Flags: []cli.Flag{ + flag.OutPrintStatus, + }, } } @@ -40,22 +43,27 @@ func (statusCommand) action(ctx *cli.Context) { if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { utils.PrintError(err) } else { - 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", value.Address.GetAddress(), value.Version)) + 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", value.Address.GetAddress(), value.Version)) + } + addresses = strings.Join(instanceList, "\n") + if addresses != "" { + connection = tableConnectedStatus + } + table.Append([]string{module.Name, connection, addresses}) } - addresses = strings.Join(instanceList, "\n") - if addresses != "" { - connection = tableConnectedStatus - } - table.Append([]string{module.Name, connection, addresses}) + table.Render() } - table.Render() } } diff --git a/flag/description.go b/flag/description.go index 5330fbd..bde4eb0 100644 --- a/flag/description.go +++ b/flag/description.go @@ -24,5 +24,6 @@ const ( ) var ( - outPrintUsage = fmt.Sprintf("out print for schema; %s or %s", OutPrintJsonValue, OutPrintHtmlValue) + outPrintStatusUsage = fmt.Sprintf("out print for status; %s", OutPrintJsonValue) + outPrintSchemaUsage = fmt.Sprintf("out print for schema; %s or %s", OutPrintJsonValue, OutPrintHtmlValue) ) diff --git a/flag/structure.go b/flag/structure.go index f13e081..944d586 100644 --- a/flag/structure.go +++ b/flag/structure.go @@ -13,7 +13,8 @@ var ( Color = cli.BoolFlag{Name: colorName, Usage: colorUsage} Unsafe = cli.BoolFlag{Name: unsafeName, Usage: unsafeUsage} //---local--- - OutPrint = cli.StringFlag{Name: outPrintName, Usage: outPrintUsage, Value: OutPrintJsonValue} + OutPrintStatus = cli.StringFlag{Name: outPrintName, Usage: outPrintStatusUsage} + OutPrintSchema = cli.StringFlag{Name: outPrintName, Usage: outPrintSchemaUsage, Value: OutPrintJsonValue} WithCommonConfig = cli.BoolFlag{Name: withCommonConfigName, Usage: withCommonConfigUsage} )