mirror of https://github.com/containers/podman.git
Add JSON version of the machine list
[NO TESTS NEEDED] Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
This commit is contained in:
parent
1dba601b56
commit
4ecaf85d96
|
@ -3,8 +3,10 @@
|
||||||
package machine
|
package machine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/completion"
|
"github.com/containers/common/pkg/completion"
|
||||||
|
@ -41,7 +43,9 @@ type listFlagType struct {
|
||||||
|
|
||||||
type machineReporter struct {
|
type machineReporter struct {
|
||||||
Name string
|
Name string
|
||||||
|
Default bool
|
||||||
Created string
|
Created string
|
||||||
|
Running bool
|
||||||
LastUp string
|
LastUp string
|
||||||
VMType string
|
VMType string
|
||||||
CPUs uint64
|
CPUs uint64
|
||||||
|
@ -57,7 +61,7 @@ func init() {
|
||||||
|
|
||||||
flags := lsCmd.Flags()
|
flags := lsCmd.Flags()
|
||||||
formatFlagName := "format"
|
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")
|
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, completion.AutocompleteNone)
|
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, completion.AutocompleteNone)
|
||||||
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
|
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
|
||||||
}
|
}
|
||||||
|
@ -78,6 +82,21 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
sort.Slice(listResponse, func(i, j int) bool {
|
sort.Slice(listResponse, func(i, j int) bool {
|
||||||
return listResponse[i].Running
|
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)
|
machineReporter, err := toHumanFormat(listResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -121,6 +140,42 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error {
|
||||||
return tmpl.Execute(w, responses)
|
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) {
|
func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) {
|
||||||
cfg, err := config.ReadCustomConfig()
|
cfg, err := config.ReadCustomConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue