Skip to content
Snippets Groups Projects
Commit 51094a16 authored by Florent Gluck's avatar Florent Gluck
Browse files

All "list" commands have short output (i.e. only VM name and ID are displayed).

However, the vmlist command has an optional long output (-l) which displays full details of the VMs
parent 48928b9e
No related branches found
No related tags found
No related merge requests found
......@@ -52,9 +52,16 @@ func (vm *VM)String() string {
return string(output)
}
func printUsage(c cmd.Command, action string) {
func printUsage(c cmd.Command, action string, showLongOutputFlag bool) {
u.PrintlnErr(c.GetDesc())
u.PrintlnErr("Usage: ",c.GetName()," [ID ...] [regex ...]")
longOutputFlag := ""
if showLongOutputFlag {
longOutputFlag = " [-l]"
}
u.PrintlnErr("Usage: ",c.GetName(),longOutputFlag+" [ID ...] [regex ...]")
if showLongOutputFlag {
u.PrintlnErr("Use \"-l\" to specify detailed VMs output.")
}
u.PrintlnErr("Only VMs matching the specified IDs or regexes will be "+action+".")
const usage string = `Any number of IDs or regexes can be specified.
The regex only matches the VM's name and is case-insensitive.
......@@ -68,11 +75,28 @@ Regex examples:
// Prints a list of filtered VMs for a given route.
// Return 0 if everything went well or 1 in case of failure.
func printFilteredVMs(c cmd.Command, args []string, route string) int {
if len(args) < 1 {
if len(args) == 0 {
c.PrintUsage()
return 1
}
// Helper function to remove an element from a string array at a given index
removeArgAtIndex := func (slice []string, index int) []string {
return append(slice[:index], slice[index+1:]...)
}
// Check if a "-l" argument is specified
foundLongOutputFlag := -1
for idx, arg := range args {
if arg == "-l" {
foundLongOutputFlag = idx
}
}
if foundLongOutputFlag >= 0 {
removeArgAtIndex(args, foundLongOutputFlag)
}
vms, err := getFilteredVMs(route, args)
if err != nil {
u.PrintlnErr("Error: "+err.Error())
......@@ -80,7 +104,11 @@ func printFilteredVMs(c cmd.Command, args []string, route string) int {
}
for _, vm := range vms {
if foundLongOutputFlag >= 0 {
u.Println(vm.String())
} else {
u.Println(vm.Name+" ("+vm.ID.String()+")")
}
}
return 0
......
......@@ -21,7 +21,7 @@ func (cmd *Attach)GetDesc() string {
}
func (cmd *Attach)PrintUsage() {
printUsage(cmd, "attached to")
printUsage(cmd, "attached to", false)
}
func (cmd *Attach)Run(args []string) int {
......
......@@ -18,7 +18,7 @@ func (cmd *Del)GetDesc() string {
}
func (cmd *Del)PrintUsage() {
printUsage(cmd, "deleted")
printUsage(cmd, "deleted", false)
}
func (cmd *Del)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *List)GetDesc() string {
}
func (cmd *List)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", true)
}
func (cmd *List)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *ListAttach)GetDesc() string {
}
func (cmd *ListAttach)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", false)
}
func (cmd *ListAttach)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *ListDel)GetDesc() string {
}
func (cmd *ListDel)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", false)
}
func (cmd *ListDel)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *ListEdit)GetDesc() string {
}
func (cmd *ListEdit)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", false)
}
func (cmd *ListEdit)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *ListEditAccess)GetDesc() string {
}
func (cmd *ListEditAccess)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", false)
}
func (cmd *ListEditAccess)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *ListStart)GetDesc() string {
}
func (cmd *ListStart)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", false)
}
func (cmd *ListStart)Run(args []string) int {
......
......@@ -13,7 +13,7 @@ func (cmd *ListStop)GetDesc() string {
}
func (cmd *ListStop)PrintUsage() {
printUsage(cmd, "listed")
printUsage(cmd, "listed", false)
}
func (cmd *ListStop)Run(args []string) int {
......
......@@ -18,7 +18,7 @@ func (cmd *Start)GetDesc() string {
}
func (cmd *Start)PrintUsage() {
printUsage(cmd, "started")
printUsage(cmd, "started", false)
}
func (cmd *Start)Run(args []string) int {
......
......@@ -18,7 +18,7 @@ func (cmd *Stop)GetDesc() string {
}
func (cmd *Stop)PrintUsage() {
printUsage(cmd, "stopped")
printUsage(cmd, "stopped", false)
}
func (cmd *Stop)Run(args []string) int {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment