3
3
package machine
4
4
5
5
import (
6
+ "encoding/json"
6
7
"os"
7
8
"sort"
9
+ "strconv"
8
10
"time"
9
11
10
12
"github.com/containers/common/pkg/completion"
11
13
"github.com/containers/common/pkg/config"
12
14
"github.com/containers/common/pkg/report"
15
+ "github.com/containers/podman/v3/cmd/podman/common"
13
16
"github.com/containers/podman/v3/cmd/podman/registry"
14
17
"github.com/containers/podman/v3/cmd/podman/validate"
15
18
"github.com/containers/podman/v3/pkg/machine"
@@ -41,8 +44,11 @@ type listFlagType struct {
41
44
42
45
type machineReporter struct {
43
46
Name string
47
+ Default bool
44
48
Created string
49
+ Running bool
45
50
LastUp string
51
+ Stream string
46
52
VMType string
47
53
CPUs uint64
48
54
Memory string
@@ -57,8 +63,8 @@ func init() {
57
63
58
64
flags := lsCmd .Flags ()
59
65
formatFlagName := "format"
60
- 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" )
61
- _ = lsCmd .RegisterFlagCompletionFunc (formatFlagName , completion . AutocompleteNone )
66
+ 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" )
67
+ _ = lsCmd .RegisterFlagCompletionFunc (formatFlagName , common . AutocompleteFormat ( machineReporter {}) )
62
68
flags .BoolVar (& listFlag .noHeading , "noheading" , false , "Do not print headers" )
63
69
}
64
70
@@ -78,6 +84,21 @@ func list(cmd *cobra.Command, args []string) error {
78
84
sort .Slice (listResponse , func (i , j int ) bool {
79
85
return listResponse [i ].Running
80
86
})
87
+
88
+ if report .IsJSON (listFlag .format ) {
89
+ machineReporter , err := toMachineFormat (listResponse )
90
+ if err != nil {
91
+ return err
92
+ }
93
+
94
+ b , err := json .Marshal (machineReporter )
95
+ if err != nil {
96
+ return err
97
+ }
98
+ os .Stdout .Write (b )
99
+ return nil
100
+ }
101
+
81
102
machineReporter , err := toHumanFormat (listResponse )
82
103
if err != nil {
83
104
return err
@@ -121,6 +142,50 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error {
121
142
return tmpl .Execute (w , responses )
122
143
}
123
144
145
+ func strTime (t time.Time ) string {
146
+ iso , err := t .MarshalText ()
147
+ if err != nil {
148
+ return ""
149
+ }
150
+ return string (iso )
151
+ }
152
+
153
+ func strUint (u uint64 ) string {
154
+ return strconv .FormatUint (u , 10 )
155
+ }
156
+
157
+ func streamName (imageStream string ) string {
158
+ if imageStream == "" {
159
+ return "default"
160
+ }
161
+ return imageStream
162
+ }
163
+
164
+ func toMachineFormat (vms []* machine.ListResponse ) ([]* machineReporter , error ) {
165
+ cfg , err := config .ReadCustomConfig ()
166
+ if err != nil {
167
+ return nil , err
168
+ }
169
+
170
+ machineResponses := make ([]* machineReporter , 0 , len (vms ))
171
+ for _ , vm := range vms {
172
+ response := new (machineReporter )
173
+ response .Default = vm .Name == cfg .Engine .ActiveService
174
+ response .Name = vm .Name
175
+ response .Running = vm .Running
176
+ response .LastUp = strTime (vm .LastUp )
177
+ response .Created = strTime (vm .CreatedAt )
178
+ response .Stream = streamName (vm .Stream )
179
+ response .VMType = vm .VMType
180
+ response .CPUs = vm .CPUs
181
+ response .Memory = strUint (vm .Memory * units .MiB )
182
+ response .DiskSize = strUint (vm .DiskSize * units .GiB )
183
+
184
+ machineResponses = append (machineResponses , response )
185
+ }
186
+ return machineResponses , nil
187
+ }
188
+
124
189
func toHumanFormat (vms []* machine.ListResponse ) ([]* machineReporter , error ) {
125
190
cfg , err := config .ReadCustomConfig ()
126
191
if err != nil {
@@ -132,11 +197,13 @@ func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) {
132
197
response := new (machineReporter )
133
198
if vm .Name == cfg .Engine .ActiveService {
134
199
response .Name = vm .Name + "*"
200
+ response .Default = true
135
201
} else {
136
202
response .Name = vm .Name
137
203
}
138
204
if vm .Running {
139
205
response .LastUp = "Currently running"
206
+ response .Running = true
140
207
} else {
141
208
response .LastUp = units .HumanDuration (time .Since (vm .LastUp )) + " ago"
142
209
}
0 commit comments