From 4d463d2dd37fd92f3a5abacbba26f2c8809311ee Mon Sep 17 00:00:00 2001 From: Aliaksandr Kazlou Date: Fri, 11 May 2018 16:16:51 +0300 Subject: [PATCH] #63 Add "hosts" command which lists all defined hosts --- cli_commands.go | 10 ++++++++- command/{list.go => list_commands.go} | 0 command/list_hosts.go | 29 +++++++++++++++++++++++++++ config/init.go | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) rename command/{list.go => list_commands.go} (100%) create mode 100644 command/list_hosts.go diff --git a/cli_commands.go b/cli_commands.go index 3cba588..3c38d60 100644 --- a/cli_commands.go +++ b/cli_commands.go @@ -58,10 +58,18 @@ var Commands = []cli.Command{ Name: "list", Usage: "List available custom commands", Description: `Example of usage is below: - list => list available custom commands defined in the ~/.vmx/commands`, + list => list available custom commands defined in the ~/.vmx/commands or in the corresponding profile if specified`, Action: command.CmdList, Flags: []cli.Flag{}, }, + { + Name: "hosts", + Usage: "List available hosts", + Description: `Example of usage is below: + hosts => list available hosts defined in the ~/.vmx/hosts or in the corresponding profile if specified`, + Action: command.CmdHosts, + Flags: []cli.Flag{}, + }, } // CommandNotFound is used by cli to display an error message when unknown command is asked diff --git a/command/list.go b/command/list_commands.go similarity index 100% rename from command/list.go rename to command/list_commands.go diff --git a/command/list_hosts.go b/command/list_hosts.go new file mode 100644 index 0000000..4d60c93 --- /dev/null +++ b/command/list_hosts.go @@ -0,0 +1,29 @@ +package command + +import ( + "fmt" + "sort" + + "gopkg.in/urfave/cli.v1" + + "github.com/zshamrock/vmx/config" +) + +// CmdHosts lists available hosts +func CmdHosts(c *cli.Context) { + CheckUpdate(c) + hostsGroups := config.GetHostsGroups() + groupNames := make([]string, 0, len(hostsGroups)) + for groupName := range hostsGroups { + groupNames = append(groupNames, groupName) + } + sort.Strings(groupNames) + for _, groupName := range groupNames { + fmt.Printf("[%s]\n", groupName) + hosts := hostsGroups[groupName] + for _, host := range hosts { + fmt.Println(host) + } + fmt.Println() + } +} diff --git a/config/init.go b/config/init.go index 976360e..7b189e1 100644 --- a/config/init.go +++ b/config/init.go @@ -87,6 +87,7 @@ func readHostsGroups(config VMXConfig, profile string) map[string][]string { continue } groups[name] = section.KeyStrings() + sort.Strings(groups[name]) } return groups }