[lxc-devel] [lxd/master] Add CSV format to list output (fixes #2363)
techtonik on Github
lxc-bot at linuxcontainers.org
Sat Apr 15 10:09:17 UTC 2017
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 424 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170415/84a93e24/attachment.bin>
-------------- next part --------------
From 3da689cb338652feb6a19400e55eabb4bb2df974 Mon Sep 17 00:00:00 2001
From: anatoly techtonik <techtonik at gmail.com>
Date: Sat, 15 Apr 2017 13:06:15 +0300
Subject: [PATCH] Add CSV format to list output (fixes #2363)
See https://golang.org/pkg/encoding/csv/#pkg-overview for details
Signed-off-by: anatoly techtonik <techtonik at gmail.com>
---
lxc/list.go | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/lxc/list.go b/lxc/list.go
index 2e53d0c..a573815 100644
--- a/lxc/list.go
+++ b/lxc/list.go
@@ -1,6 +1,7 @@
package main
import (
+ "encoding/csv"
"encoding/json"
"fmt"
"os"
@@ -31,6 +32,7 @@ type columnData func(api.Container, *api.ContainerState, []api.ContainerSnapshot
const (
listFormatTable = "table"
listFormatJSON = "json"
+ listFormatCSV = "csv"
)
type listCmd struct {
@@ -45,7 +47,7 @@ func (c *listCmd) showByDefault() bool {
func (c *listCmd) usage() string {
return i18n.G(
- `Usage: lxc list [<remote>:] [filters] [--format table|json] [-c <columns>] [--fast]
+ `Usage: lxc list [<remote>:] [filters] [--format table|json|csv] [-c <columns>] [--fast]
List the existing containers.
@@ -329,6 +331,25 @@ func (c *listCmd) listContainers(d *lxd.Client, cinfos []api.Container, filters
cSnapshotsWg.Wait()
switch c.format {
+ case listFormatCSV:
+ data := [][]string{}
+ for _, cInfo := range cinfos {
+ if !c.shouldShow(filters, &cInfo) {
+ continue
+ }
+
+ col := []string{}
+ for _, column := range columns {
+ col = append(col, column.Data(cInfo, cStates[cInfo.Name], cSnapshots[cInfo.Name]))
+ }
+ data = append(data, col)
+ }
+
+ w := csv.NewWriter(os.Stdout)
+ w.WriteAll(data)
+ if err := w.Error(); err != nil {
+ return err
+ }
case listFormatTable:
data := [][]string{}
for _, cInfo := range cinfos {
More information about the lxc-devel
mailing list