Skip to content

Commit

Permalink
Add support for golang templating in list sub-command
Browse files Browse the repository at this point in the history
This commit adds support for templating on 'list' sub-command output.
It's helping on automation and will allow more custom filtering in output.

It's using the https://github.com/intel/tfortools/ project

Signed-off-by: Obed N Munoz <[email protected]>
  • Loading branch information
obedmr committed Mar 29, 2019
1 parent eacb444 commit 1166148
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,29 @@ list
----
Lists all virtual machines that were created with the ``govm`` tool. It also shows the VNC access url and name.

| Flag | Description | Required |
|-------------------|-----------------------------------|----------|
| --all | Show VMs from all namespaces | No |
| --namespace value | Show VMs from the given namespace | No |
| Flag | Description | Required |
|--------------------------|------------------------------------------------|----------|
| --all | Show VMs from all namespaces | No |
| --namespace value | Show VMs from the given namespace | No |
| --format value, -f value | String containing the template code to execute | No |

*Output example:*
*Output example*
```
ID IP VNC_URL NAME
bd0088a3eb 172.17.0.2 http://localhost:40669 clear-test
db3ea1be1d 172.17.0.3 http://localhost:35957 cirros-test
f8a4cd7e93 172.17.0.4 http://localhost:42161 test-30310
# govm list
ID Name Namespace IP
b9b5d3a288 test-14731 onmunoz 172.17.0.6
4d6731b571 test-29652 onmunoz 172.17.0.5
ef004385d4 test-20024 onmunoz 172.17.0.4
5e5f42047b happy-poitras onmunoz 172.17.0.3
e90608db45 wonderful-varahamihira onmunoz 172.17.0.2
```

*Filtered output*
```
# govm list -f '{{select (filterRegexp . "Name" "test-*") "IP"}}'
172.17.0.6
172.17.0.5
172.17.0.4
```

compose
Expand Down
1 change: 0 additions & 1 deletion engines/docker/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ func (e Engine) setVNC(vncID string, port string) error {
_, err := e.docker.Create(containerConfig, hostConfig,
&network.NetworkingConfig{}, VNCServerContainerName)
if err != nil {
fmt.Println(err)
return err
}
}
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module github.com/govm-project/govm

require (
github.com/codegangsta/cli v1.20.0
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/docker/distribution v2.6.2+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.3.3 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/go-cmp v0.2.0
github.com/moby/moby v1.13.1
github.com/intel/tfortools v0.2.0
github.com/kr/pretty v0.1.0 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/sirupsen/logrus v1.1.0
github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4
golang.org/x/net v0.0.0-20181003013248-f5e5bdd77824
golang.org/x/sys v0.0.0-20181003145944-af653ce8b74f
Expand Down
43 changes: 28 additions & 15 deletions pkg/cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package cli
import (
"fmt"
"os"
"text/tabwriter"

"github.com/govm-project/govm/engines/docker"

log "github.com/sirupsen/logrus"
"github.com/intel/tfortools"
cli "gopkg.in/urfave/cli.v2"
)

Expand All @@ -21,31 +20,45 @@ var listCommand = cli.Command{
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()

instances, err := engine.List(c.String("namespace"), c.Bool("all"))
result, err := engine.List(c.String("namespace"), c.Bool("all"))
if err != nil {
return err
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "ID\tName\tNamespace\tIP\tVNC")
for _, instance := range instances {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
instance.ID,
instance.Name,
instance.Namespace,
instance.NetOpts.IP,
fmt.Sprintf("http://localhost:%v", instance.VNCPort),
)
// 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})
}
err = w.Flush()

format := c.String("format")
if format == "" {
format = `{{table .}}`
}

err = tfortools.OutputToTemplate(os.Stdout, "format", format, instances, nil)
if err != nil {
log.Fatal(err)
fmt.Fprintln(os.Stderr, tfortools.GenerateUsageDecorated("format", instances, nil))
return fmt.Errorf("Unable to execute template : %v", err)
}

return nil
Expand Down

0 comments on commit 1166148

Please sign in to comment.