From 412d2fbc838ea9416f8d4d796be361b3480b5750 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Fri, 2 Aug 2019 12:49:51 +0200 Subject: [PATCH] Cleanup commands Signed-off-by: Ondrej Fabry --- cmd/agentctl/agentctl.go | 7 +- cmd/agentctl/cmd/root.go | 54 ------------ cmd/agentctl/{cmd => commands}/del.go | 28 +++---- cmd/agentctl/{cmd => commands}/dump.go | 98 +++++++++++----------- cmd/agentctl/{cmd => commands}/generate.go | 64 +++++++------- cmd/agentctl/{cmd => commands}/import.go | 42 +++++----- cmd/agentctl/{cmd => commands}/log.go | 27 +++--- cmd/agentctl/{cmd => commands}/put.go | 29 +++---- cmd/agentctl/commands/root.go | 77 +++++++++++++++++ cmd/agentctl/{cmd => commands}/show.go | 30 ++++--- cmd/agentctl/{cmd => commands}/vppcli.go | 47 ++++++----- 11 files changed, 264 insertions(+), 239 deletions(-) delete mode 100644 cmd/agentctl/cmd/root.go rename cmd/agentctl/{cmd => commands}/del.go (76%) rename cmd/agentctl/{cmd => commands}/dump.go (87%) rename cmd/agentctl/{cmd => commands}/generate.go (86%) rename cmd/agentctl/{cmd => commands}/import.go (92%) rename cmd/agentctl/{cmd => commands}/log.go (91%) rename cmd/agentctl/{cmd => commands}/put.go (81%) create mode 100644 cmd/agentctl/commands/root.go rename cmd/agentctl/{cmd => commands}/show.go (95%) rename cmd/agentctl/{cmd => commands}/vppcli.go (52%) diff --git a/cmd/agentctl/agentctl.go b/cmd/agentctl/agentctl.go index 9982ea8e07..4cefb1a151 100644 --- a/cmd/agentctl/agentctl.go +++ b/cmd/agentctl/agentctl.go @@ -1,15 +1,14 @@ package main import ( - "fmt" "os" - "github.com/ligato/vpp-agent/cmd/agentctl/cmd" + "github.com/ligato/vpp-agent/cmd/agentctl/commands" ) func main() { - if err := cmd.RootCmd.Execute(); err != nil { - fmt.Println(err) + rootCmd := commands.NewRootCmd("agentctl") + if err := rootCmd.Execute(); err != nil { os.Exit(1) } } diff --git a/cmd/agentctl/cmd/root.go b/cmd/agentctl/cmd/root.go deleted file mode 100644 index 9b93c9ba79..0000000000 --- a/cmd/agentctl/cmd/root.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2017 Cisco and/or its affiliates. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at: -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "os" - - "github.com/ligato/cn-infra/agent" - "github.com/spf13/cobra" -) - -// GlobalFlags defines a single type to hold all cobra global flags. -var globalFlags struct { - Endpoints []string - Label string -} - -// RootCmd represents the base command when called without any subcommands. -var RootCmd = &cobra.Command{ - Use: "agentctl", - Short: "A CLI tool for managing agents.", - Example: `Specify the etcd to connect to and list all agents that it knows about: - $ export ETCD_ENDPOINTS=172.17.0.1:2379 - -or with a command line flag: - $ agentctl --endpoints 172.17.0.1:2379 show -`, - Version: agent.BuildVersion, -} - -func init() { - label := "vpp1" - if l := os.Getenv("MICROSERVICE_LABEL"); l != "" { - label = l - } - RootCmd.PersistentFlags().StringSliceVarP(&globalFlags.Endpoints, - "endpoints", "e", nil, - "One or more comma-separated Etcd endpoints.") - RootCmd.PersistentFlags().StringVarP(&globalFlags.Label, - "label", "l", label, - "Microservice label which identifies the agent.") -} diff --git a/cmd/agentctl/cmd/del.go b/cmd/agentctl/commands/del.go similarity index 76% rename from cmd/agentctl/cmd/del.go rename to cmd/agentctl/commands/del.go index e008c9a093..5977ed6ef0 100644 --- a/cmd/agentctl/cmd/del.go +++ b/cmd/agentctl/commands/del.go @@ -1,30 +1,28 @@ -package cmd +package commands import ( + "errors" "strings" "github.com/ligato/cn-infra/db/keyval" "github.com/ligato/cn-infra/servicelabel" - "github.com/ligato/vpp-agent/cmd/agentctl/utils" "github.com/spf13/cobra" - "errors" + "github.com/ligato/vpp-agent/cmd/agentctl/utils" ) -// RootCmd represents the base command when called without any subcommands. -var delConfig = &cobra.Command{ - Use: "del ", - Aliases: []string{"d"}, - Short: "Delete configuration file", - Long: ` +func delCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "del ", + Aliases: []string{"d"}, + Short: "Delete configuration file", + Long: ` Delete configuration file `, - Args: cobra.RangeArgs(1, 1), - Run: delFunction, -} - -func init() { - RootCmd.AddCommand(delConfig) + Args: cobra.RangeArgs(1, 1), + Run: delFunction, + } + return cmd } func delFunction(cmd *cobra.Command, args []string) { diff --git a/cmd/agentctl/cmd/dump.go b/cmd/agentctl/commands/dump.go similarity index 87% rename from cmd/agentctl/cmd/dump.go rename to cmd/agentctl/commands/dump.go index d1b3d05adb..33e727e320 100644 --- a/cmd/agentctl/cmd/dump.go +++ b/cmd/agentctl/commands/dump.go @@ -1,30 +1,30 @@ -package cmd +package commands import ( "fmt" "os" + "github.com/spf13/cobra" + "github.com/ligato/vpp-agent/api/models/linux" "github.com/ligato/vpp-agent/api/models/vpp" - "github.com/ligato/vpp-agent/cmd/agentctl/utils" - "github.com/ligato/vpp-agent/cmd/agentctl/restapi" + "github.com/ligato/vpp-agent/cmd/agentctl/utils" "github.com/ligato/vpp-agent/plugins/restapi/resturl" - "github.com/spf13/cobra" ) -// RootCmd represents the base command when called without any subcommands. -var dumpCmd = &cobra.Command{ - Use: "dump", - Aliases: []string{"d"}, - Short: "Dump command for vppagent", - Long: ` -A Dump tool to connect to vppagent and dump value. -Use the 'ETCD_ENDPOINTS'' environment variable or the 'endpoints' -flag in the command line to specify vppagent instances to -connect to. -`, - Example: `Specify the vppagent to connect to and run VPP CLI command: +func dumpCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "dump", + Aliases: []string{"d"}, + Short: "Dump command for vppagent", + /*Long: ` + A Dump tool to connect to vppagent and dump value. + Use the 'ETCD_ENDPOINTS'' environment variable or the 'endpoints' + flag in the command line to specify vppagent instances to + connect to. + `,*/ + Example: `Specify the vppagent to connect to and run VPP CLI command: $ export ETCD_ENDPOINTS=172.17.0.3:9191 $ ./agentctl dump @@ -32,7 +32,39 @@ Do as above, but with a command line flag: $ ./agentctl --endpoints 172.17.0.3:9191 dump `, - Args: cobra.MinimumNArgs(2), + Args: cobra.MinimumNArgs(2), + } + + cmd.AddCommand(dumpLinuxInterface) + cmd.AddCommand(dumpLinuxRoutes) + cmd.AddCommand(dumpLinuxArps) + cmd.AddCommand(dumpACLIP) + cmd.AddCommand(dumpACLMACIP) + cmd.AddCommand(dumpInterface) + cmd.AddCommand(dumpLoopback) + cmd.AddCommand(dumpEthernet) + cmd.AddCommand(dumpMemif) + cmd.AddCommand(dumpTap) + cmd.AddCommand(dumpAfPacket) + cmd.AddCommand(dumpVxLan) + cmd.AddCommand(dumpNatGlobal) + cmd.AddCommand(dumpNatDNat) + cmd.AddCommand(dumpBd) + cmd.AddCommand(dumpFib) + cmd.AddCommand(dumpXc) + cmd.AddCommand(dumpRoutes) + cmd.AddCommand(dumpArps) + cmd.AddCommand(dumpPArpIfs) + cmd.AddCommand(dumpPArpRngs) + cmd.AddCommand(dumpCommand) + cmd.AddCommand(dumpTelemetry) + cmd.AddCommand(dumpTMemory) + cmd.AddCommand(dumpTRuntime) + cmd.AddCommand(dumpTNodeCount) + cmd.AddCommand(dumpTracer) + cmd.AddCommand(dumpIndex) + + return cmd } var dumpLinuxInterface = &cobra.Command{ @@ -315,38 +347,6 @@ var dumpIndex = &cobra.Command{ Run: indexDumpFunction, } -func init() { - RootCmd.AddCommand(dumpCmd) - dumpCmd.AddCommand(dumpLinuxInterface) - dumpCmd.AddCommand(dumpLinuxRoutes) - dumpCmd.AddCommand(dumpLinuxArps) - dumpCmd.AddCommand(dumpACLIP) - dumpCmd.AddCommand(dumpACLMACIP) - dumpCmd.AddCommand(dumpInterface) - dumpCmd.AddCommand(dumpLoopback) - dumpCmd.AddCommand(dumpEthernet) - dumpCmd.AddCommand(dumpMemif) - dumpCmd.AddCommand(dumpTap) - dumpCmd.AddCommand(dumpAfPacket) - dumpCmd.AddCommand(dumpVxLan) - dumpCmd.AddCommand(dumpNatGlobal) - dumpCmd.AddCommand(dumpNatDNat) - dumpCmd.AddCommand(dumpBd) - dumpCmd.AddCommand(dumpFib) - dumpCmd.AddCommand(dumpXc) - dumpCmd.AddCommand(dumpRoutes) - dumpCmd.AddCommand(dumpArps) - dumpCmd.AddCommand(dumpPArpIfs) - dumpCmd.AddCommand(dumpPArpRngs) - dumpCmd.AddCommand(dumpCommand) - dumpCmd.AddCommand(dumpTelemetry) - dumpCmd.AddCommand(dumpTMemory) - dumpCmd.AddCommand(dumpTRuntime) - dumpCmd.AddCommand(dumpTNodeCount) - dumpCmd.AddCommand(dumpTracer) - dumpCmd.AddCommand(dumpIndex) -} - func linuxInterfaceDumpFunction(cmd *cobra.Command, args []string) { msg := restapi.GetMsg(globalFlags.Endpoints, resturl.LinuxInterface) diff --git a/cmd/agentctl/cmd/generate.go b/cmd/agentctl/commands/generate.go similarity index 86% rename from cmd/agentctl/cmd/generate.go rename to cmd/agentctl/commands/generate.go index 405e09b29a..4d2e2d16db 100644 --- a/cmd/agentctl/cmd/generate.go +++ b/cmd/agentctl/commands/generate.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "encoding/json" @@ -9,9 +9,8 @@ import ( "github.com/ghodss/yaml" "github.com/gogo/protobuf/proto" - "github.com/spf13/cobra" - "github.com/ligato/cn-infra/servicelabel" + "github.com/spf13/cobra" "github.com/ligato/vpp-agent/api/models/linux" "github.com/ligato/vpp-agent/api/models/vpp" @@ -20,14 +19,34 @@ import ( "github.com/ligato/vpp-agent/pkg/models" ) -// RootCmd represents the base command when called without any subcommands. -var generateConfig = &cobra.Command{ - Use: "generate", - Aliases: []string{"g"}, - Short: "Generate example command", - Long: ` - Generate example command -`, +func generateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "generate", + Aliases: []string{"g"}, + Short: "Generate example command", + } + cmd.AddCommand(generateACL) + cmd.AddCommand(generateInterface) + cmd.AddCommand(generateBd) + cmd.AddCommand(generateFib) + cmd.AddCommand(generateIPScanNeighbor) + cmd.AddCommand(generateNatGlobal) + cmd.AddCommand(generateNatDNat) + cmd.AddCommand(generateIPSecPolicy) + cmd.AddCommand(generateIPSecAssociation) + cmd.AddCommand(generateArps) + cmd.AddCommand(generateRoutes) + cmd.AddCommand(generatePArp) + cmd.AddCommand(generateLinuxInterface) + cmd.AddCommand(generateLinuxARP) + cmd.AddCommand(generateLinuxRoutes) + + formatType = cmd.PersistentFlags().String("format", "json", + "Output formats:\n\tjson\n\tyaml\n\tproto\n") + cmd.PersistentFlags().BoolVar(&short, "short", false, + "Print command to one line. Work only with json format") + + return cmd } var generateACL = &cobra.Command{ @@ -185,29 +204,6 @@ var ( short bool ) -func init() { - RootCmd.AddCommand(generateConfig) - generateConfig.AddCommand(generateACL) - generateConfig.AddCommand(generateInterface) - generateConfig.AddCommand(generateBd) - generateConfig.AddCommand(generateFib) - generateConfig.AddCommand(generateIPScanNeighbor) - generateConfig.AddCommand(generateNatGlobal) - generateConfig.AddCommand(generateNatDNat) - generateConfig.AddCommand(generateIPSecPolicy) - generateConfig.AddCommand(generateIPSecAssociation) - generateConfig.AddCommand(generateArps) - generateConfig.AddCommand(generateRoutes) - generateConfig.AddCommand(generatePArp) - generateConfig.AddCommand(generateLinuxInterface) - generateConfig.AddCommand(generateLinuxARP) - generateConfig.AddCommand(generateLinuxRoutes) - formatType = generateConfig.PersistentFlags().String("format", "json", - "Output formats:\n\tjson\n\tyaml\n\tproto\n") - generateConfig.PersistentFlags().BoolVar(&short, "short", false, - "Print command to one line. Work only with json format") -} - func aclGenerateFunction(cmd *cobra.Command, args []string) { generateFunction(cmd_generator.ACL) } diff --git a/cmd/agentctl/cmd/import.go b/cmd/agentctl/commands/import.go similarity index 92% rename from cmd/agentctl/cmd/import.go rename to cmd/agentctl/commands/import.go index 6612f39925..d6cece10f9 100644 --- a/cmd/agentctl/cmd/import.go +++ b/cmd/agentctl/commands/import.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package commands import ( "bytes" @@ -26,11 +26,10 @@ import ( "github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/proto" + "github.com/ligato/cn-infra/servicelabel" "github.com/spf13/cobra" "google.golang.org/grpc" - "github.com/ligato/cn-infra/servicelabel" - "github.com/ligato/vpp-agent/api/genericmanager" "github.com/ligato/vpp-agent/client/remoteclient" "github.com/ligato/vpp-agent/cmd/agentctl/utils" @@ -43,22 +42,13 @@ var ( timeout uint ) -func init() { - RootCmd.AddCommand(importConfig) - importConfig.PersistentFlags().UintVar(&txops, "txops", 128, - "Number of OPs per transaction") - importConfig.PersistentFlags().StringVar(&grpcAddr, "grpc", "", - "Address of gRPC server.") - importConfig.PersistentFlags().UintVarP(&timeout, "time", "t", 60, - "Client timeout in seconds (how long to wait for response from server)") -} - -var importConfig = &cobra.Command{ - Use: "import", - Aliases: []string{"i"}, - Args: cobra.RangeArgs(1, 1), - Short: "Import configuration from file", - Long: ` +func importCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "import", + Aliases: []string{"i"}, + Args: cobra.RangeArgs(1, 1), + Short: "Import configuration from file", + Long: ` Import configuration from file. File format: @@ -75,7 +65,7 @@ Supported key formats: For short keys, the import command uses microservice label defined with --label. `, - Example: ` Import configuration from file: + Example: ` Import configuration from file: $ cat input.txt config/vpp/v2/interfaces/loop1 {"name":"loop1","type":"SOFTWARE_LOOPBACK"} @@ -86,7 +76,17 @@ Supported key formats: $ agentctl import --grpc=localhost:9111 input.txt `, - Run: importFunction, + Run: importFunction, + } + + cmd.PersistentFlags().UintVar(&txops, "txops", 128, + "Number of OPs per transaction") + cmd.PersistentFlags().StringVar(&grpcAddr, "grpc", "", + "Address of gRPC server.") + cmd.PersistentFlags().UintVarP(&timeout, "time", "t", 60, + "Client timeout in seconds (how long to wait for response from server)") + + return cmd } func getTimeout() time.Duration { diff --git a/cmd/agentctl/cmd/log.go b/cmd/agentctl/commands/log.go similarity index 91% rename from cmd/agentctl/cmd/log.go rename to cmd/agentctl/commands/log.go index cf8100dc79..67e99a6c27 100644 --- a/cmd/agentctl/cmd/log.go +++ b/cmd/agentctl/commands/log.go @@ -1,27 +1,33 @@ -package cmd +package commands import ( "fmt" "os" "strings" - "github.com/ligato/vpp-agent/cmd/agentctl/utils" + "github.com/spf13/cobra" "github.com/ligato/vpp-agent/cmd/agentctl/restapi" - "github.com/spf13/cobra" + "github.com/ligato/vpp-agent/cmd/agentctl/utils" ) -// RootCmd represents the base command when called without any subcommands. -var log = &cobra.Command{ - Use: "log", - Aliases: []string{"l"}, - Short: "Show/Set vppagent logs", - Long: ` +func logCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "log", + Aliases: []string{"l"}, + Short: "Show/Set vppagent logs", + Long: ` A CLI tool to connect to vppagent and show/set vppagent logs. Use the 'ETCD_ENDPOINTS'' environment variable or the 'endpoints' flag in the command line to specify vppagent instances to connect to. `, + } + + cmd.AddCommand(logList) + cmd.AddCommand(logSet) + + return cmd } var logList = &cobra.Command{ @@ -69,9 +75,6 @@ Do as above, but with a command line flag: var verbose bool func init() { - RootCmd.AddCommand(log) - log.AddCommand(logList) - log.AddCommand(logSet) logList.Flags().BoolVar(&verbose, "v", false, "verbose") } diff --git a/cmd/agentctl/cmd/put.go b/cmd/agentctl/commands/put.go similarity index 81% rename from cmd/agentctl/cmd/put.go rename to cmd/agentctl/commands/put.go index a7c85677b6..0158976458 100644 --- a/cmd/agentctl/cmd/put.go +++ b/cmd/agentctl/commands/put.go @@ -1,24 +1,23 @@ -package cmd +package commands import ( "errors" "fmt" "strings" - "github.com/spf13/cobra" - "github.com/ligato/cn-infra/db/keyval" "github.com/ligato/cn-infra/servicelabel" + "github.com/spf13/cobra" "github.com/ligato/vpp-agent/cmd/agentctl/utils" ) -// RootCmd represents the base command when called without any subcommands. -var putConfig = &cobra.Command{ - Use: "put", - Aliases: []string{"p"}, - Short: "Put configuration file", - Long: ` +func putCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "put", + Aliases: []string{"p"}, + Short: "Put configuration file", + Long: ` Put configuration file to Etcd. Supported key formats: @@ -28,8 +27,8 @@ Supported key formats: For short key, put command use default microservice label and 'vpp1' as default agent label. `, - Args: cobra.RangeArgs(1, 2), - Example: ` Set route configuration for "vpp1": + Args: cobra.RangeArgs(1, 2), + Example: ` Set route configuration for "vpp1": $ agentctl -e 172.17.0.3:2379 put /vnf-agent/vpp1/config/vpp/v2/route/vrf/1/dst/10.1.1.3/32/gw/192.168.1.13 '{ "type": 1, "vrf_id": 1, @@ -40,11 +39,9 @@ For short key, put command use default microservice label and 'vpp1' as default Alternative: $ agentctl put $(agentctl generate Route --short) `, - Run: putFunction, -} - -func init() { - RootCmd.AddCommand(putConfig) + Run: putFunction, + } + return cmd } func putFunction(cmd *cobra.Command, args []string) { diff --git a/cmd/agentctl/commands/root.go b/cmd/agentctl/commands/root.go new file mode 100644 index 0000000000..f15b63974e --- /dev/null +++ b/cmd/agentctl/commands/root.go @@ -0,0 +1,77 @@ +// Copyright (c) 2017 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package commands + +import ( + "fmt" + "os" + + "github.com/ligato/cn-infra/agent" + "github.com/spf13/cobra" +) + +const defaultLabel = "vpp1" + +var ( + // globalFlags defines a single type to hold all cobra global flags. + globalFlags struct { + Endpoints []string + Label string + } +) + +// NewRootCmd returns new base command. +func NewRootCmd(name string) *cobra.Command { + cmd := &cobra.Command{ + Use: name, + Short: "A CLI tool for managing agents", + Example: `Specify the etcd to connect to and list all agents that it knows about: + $ export ETCD_ENDPOINTS=172.17.0.1:2379 + +or with a command line flag: + $ agentctl --endpoints 172.17.0.1:2379 show +`, + Version: fmt.Sprintf("%s", agent.BuildVersion), + } + cmd.Name() + + label := defaultLabel + if l := os.Getenv("MICROSERVICE_LABEL"); l != "" { + label = l + } + + cmd.PersistentFlags().StringVarP(&globalFlags.Label, + "label", "l", label, + "Microservice label identiying agent instance") + cmd.PersistentFlags().StringSliceVarP(&globalFlags.Endpoints, + "endpoints", "e", nil, + "Etcd endpoints to connect to (comma-separated)") + + addCommands(cmd) + return cmd +} + +func addCommands(cmd *cobra.Command) { + cmd.AddCommand( + showCmd(), + generateCmd(), + putCmd(), + delCmd(), + importCmd(), + dumpCmd(), + vppcliCmd(), + logCmd(), + ) +} diff --git a/cmd/agentctl/cmd/show.go b/cmd/agentctl/commands/show.go similarity index 95% rename from cmd/agentctl/cmd/show.go rename to cmd/agentctl/commands/show.go index 5a6169332b..011a262ecf 100644 --- a/cmd/agentctl/cmd/show.go +++ b/cmd/agentctl/commands/show.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "errors" @@ -22,12 +22,12 @@ import ( "github.com/ligato/vpp-agent/cmd/agentctl/utils" ) -// RootCmd represents the base command when called without any subcommands. -var showCmd = &cobra.Command{ - Use: "show", - Aliases: []string{"s"}, - Short: "Show detailed config and status data", - Long: ` +func showCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "show", + Aliases: []string{"s"}, + Short: "Show detailed config and status data", + Long: ` 'show' prints out Etcd configuration and status data (where applicable) for agents whose microservice label matches the label filter specified in the command's '[agent-label-filter] argument. The filter contains a @@ -43,7 +43,16 @@ exist (i.e. they do not push status records into Etcd) are listed as The etcd flag set to true enables the printout of Etcd metadata for each data record (except JSON-formatted output) `, - Run: showFunction, + Run: showFunction, + } + + cmd.AddCommand(showConfig) + cmd.AddCommand(keyConfig) + + cmd.PersistentFlags().BoolVar(&showAll, "all", false, + "Show all configuration") + + return cmd } var showConfig = &cobra.Command{ @@ -94,11 +103,6 @@ var ( ) func init() { - RootCmd.AddCommand(showCmd) - showCmd.AddCommand(showConfig) - showCmd.AddCommand(keyConfig) - showCmd.PersistentFlags().BoolVar(&showAll, "all", false, - "Show all configuration") showConfig.PersistentFlags().BoolVar(&showConfAll, "all", false, "Show all configuration") diff --git a/cmd/agentctl/cmd/vppcli.go b/cmd/agentctl/commands/vppcli.go similarity index 52% rename from cmd/agentctl/cmd/vppcli.go rename to cmd/agentctl/commands/vppcli.go index 9c59812e53..b0ea1e9c1d 100644 --- a/cmd/agentctl/cmd/vppcli.go +++ b/cmd/agentctl/commands/vppcli.go @@ -1,25 +1,27 @@ -package cmd +package commands import ( + "encoding/json" "fmt" "os" "strings" - "github.com/ligato/vpp-agent/cmd/agentctl/restapi" "github.com/spf13/cobra" + + "github.com/ligato/vpp-agent/cmd/agentctl/restapi" ) -// RootCmd represents the base command when called without any subcommands. -var cliConfig = &cobra.Command{ - Use: "vppcli", - Short: "CLI command for vppagent", - Long: ` +func vppcliCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "vppcli", + Short: "Execute VPP CLI command", + Long: ` A CLI tool to connect to vppagent and run VPP CLI command. Use the 'ETCD_ENDPOINTS'' environment variable or the 'endpoints' flag in the command line to specify vppagent instances to connect to. `, - Example: `Specify the vppagent to connect to and run VPP CLI command: + Example: `Specify the vppagent to connect to and run VPP CLI command: $ export ETCD_ENDPOINTS=172.17.0.3:9191 $ ./agentctl vppcli 'show int' @@ -27,27 +29,30 @@ Do as above, but with a command line flag: $ ./agentctl --endpoints 172.17.0.3:9191 vppcli 'show int' `, - Args: cobra.MinimumNArgs(1), - Run: cliFunction, -} - -func init() { - RootCmd.AddCommand(cliConfig) + Args: cobra.MinimumNArgs(1), + RunE: vppcliFunction, + } + return cmd } -func cliFunction(cmd *cobra.Command, args []string) { - var cli string +func vppcliFunction(cmd *cobra.Command, args []string) error { + cli := strings.Join(args, " ") + fmt.Fprintf(os.Stdout, "VPP CLI: %s\n", cli) - for _, str := range args { - cli = cli + " " + str + data := map[string]interface{}{ + "vppclicommand": cli, } - msg := fmt.Sprintf("{\"vppclicommand\":\"%v\"}", cli) - - fmt.Fprintf(os.Stdout, "%s\n", msg) + b, err := json.MarshalIndent(data, "", " ") + if err != nil { + return err + } + msg := string(b) resp := restapi.PostMsg(globalFlags.Endpoints, "/vpp/command", msg) tmp := strings.Replace(resp, "\\n", "\n", -1) fmt.Fprintf(os.Stdout, "%s\n", tmp) + + return nil }