Skip to content

Commit

Permalink
#63 Add "hosts" command which lists all defined hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
zshamrock committed May 11, 2018
1 parent 0d6b555 commit 4d463d2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cli_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions command/list_hosts.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
1 change: 1 addition & 0 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func readHostsGroups(config VMXConfig, profile string) map[string][]string {
continue
}
groups[name] = section.KeyStrings()
sort.Strings(groups[name])
}
return groups
}
Expand Down

0 comments on commit 4d463d2

Please sign in to comment.