diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-09-24 14:24:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-24 14:24:55 -0400 |
commit | cd28f5783a2549533278d2f5023f939c225bbe0c (patch) | |
tree | 51b649e5aa3b1d32759280a2d9e34e74cdedab25 /cmd/podman | |
parent | 85d9b3705126dfb6dfa5d0074df6787599273a08 (diff) | |
parent | ba46613a6cfb43735d313280c191f2ed67c4ed23 (diff) | |
download | podman-cd28f5783a2549533278d2f5023f939c225bbe0c.tar.gz podman-cd28f5783a2549533278d2f5023f939c225bbe0c.tar.bz2 podman-cd28f5783a2549533278d2f5023f939c225bbe0c.zip |
Merge pull request #11709 from afbjorklund/machine-json
Add JSON version of the machine list
Diffstat (limited to 'cmd/podman')
-rw-r--r-- | cmd/podman/machine/list.go | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/cmd/podman/machine/list.go b/cmd/podman/machine/list.go index fe9d712e3..95b7d860f 100644 --- a/cmd/podman/machine/list.go +++ b/cmd/podman/machine/list.go @@ -3,13 +3,16 @@ package machine import ( + "encoding/json" "os" "sort" + "strconv" "time" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/config" "github.com/containers/common/pkg/report" + "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/machine" @@ -41,7 +44,9 @@ type listFlagType struct { type machineReporter struct { Name string + Default bool Created string + Running bool LastUp string VMType string CPUs uint64 @@ -57,8 +62,8 @@ func init() { flags := lsCmd.Flags() formatFlagName := "format" - flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using Go template") - _ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, completion.AutocompleteNone) + flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template") + _ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(machineReporter{})) flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers") } @@ -78,6 +83,21 @@ func list(cmd *cobra.Command, args []string) error { sort.Slice(listResponse, func(i, j int) bool { return listResponse[i].Running }) + + if report.IsJSON(listFlag.format) { + machineReporter, err := toMachineFormat(listResponse) + if err != nil { + return err + } + + b, err := json.Marshal(machineReporter) + if err != nil { + return err + } + os.Stdout.Write(b) + return nil + } + machineReporter, err := toHumanFormat(listResponse) if err != nil { return err @@ -121,6 +141,42 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error { return tmpl.Execute(w, responses) } +func strTime(t time.Time) string { + iso, err := t.MarshalText() + if err != nil { + return "" + } + return string(iso) +} + +func strUint(u uint64) string { + return strconv.FormatUint(u, 10) +} + +func toMachineFormat(vms []*machine.ListResponse) ([]*machineReporter, error) { + cfg, err := config.ReadCustomConfig() + if err != nil { + return nil, err + } + + machineResponses := make([]*machineReporter, 0, len(vms)) + for _, vm := range vms { + response := new(machineReporter) + response.Default = vm.Name == cfg.Engine.ActiveService + response.Name = vm.Name + response.Running = vm.Running + response.LastUp = strTime(vm.LastUp) + response.Created = strTime(vm.CreatedAt) + response.VMType = vm.VMType + response.CPUs = vm.CPUs + response.Memory = strUint(vm.Memory * units.MiB) + response.DiskSize = strUint(vm.DiskSize * units.GiB) + + machineResponses = append(machineResponses, response) + } + return machineResponses, nil +} + func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) { cfg, err := config.ReadCustomConfig() if err != nil { |