-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlist.go
66 lines (55 loc) · 1.42 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cli
import (
"fmt"
"os"
"github.com/govm-project/govm/engines/docker"
"github.com/intel/tfortools"
cli "gopkg.in/urfave/cli.v2"
)
var listCommand = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List VMs",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "list VMs from all namespaces",
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "string containing the template code to execute",
},
},
Action: func(c *cli.Context) error {
engine := docker.Engine{}
engine.Init()
result, err := engine.List(c.String("namespace"), c.Bool("all"))
if err != nil {
return err
}
// Required due a limitation on intel/tfortools for embedded structures
// https://github.com/intel/tfortools/issues/18
type outInstance struct {
ID string
Name string
Namespace string
IP string
}
instances := []outInstance{}
for _, elem := range result {
instances = append(instances, outInstance{elem.ID, elem.Name, elem.Namespace, elem.NetOpts.IP})
}
format := c.String("format")
if format == "" {
format = `{{table .}}`
}
err = tfortools.OutputToTemplate(os.Stdout, "format", format, instances, nil)
if err != nil {
fmt.Fprintln(os.Stderr, tfortools.GenerateUsageDecorated("format", instances, nil))
return fmt.Errorf("Unable to execute template : %v", err)
}
return nil
},
}