Skip to content

Commit 6de1809

Browse files
committed
list action
Signed-off-by: Luca Stocchi <[email protected]>
1 parent 5b7cb5d commit 6de1809

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

cmd/macadam/list.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"strconv"
7+
"time"
8+
9+
"github.com/cfergeau/macadam/cmd/macadam/registry"
10+
macadam "github.com/cfergeau/macadam/pkg/machinedriver"
11+
"github.com/containers/common/pkg/completion"
12+
"github.com/crc-org/machine/libmachine/state"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
var (
17+
lsCmd = &cobra.Command{
18+
Use: "list [options]",
19+
Aliases: []string{"ls"},
20+
Short: "List machines",
21+
Long: "List managed virtual machines.",
22+
// do not use machinePreRunE, as that pre-sets the provider
23+
RunE: list,
24+
Args: cobra.MaximumNArgs(0),
25+
ValidArgsFunction: completion.AutocompleteNone,
26+
Example: `macadam list,
27+
macadam list --format json
28+
macadam ls`,
29+
}
30+
listFlag = listFlagType{}
31+
)
32+
33+
type listFlagType struct {
34+
format string
35+
}
36+
37+
type ListReporter struct {
38+
Image string
39+
Created string
40+
Running bool
41+
Starting bool
42+
LastUp string
43+
CPUs uint64
44+
Memory string
45+
DiskSize string
46+
Port int
47+
RemoteUsername string
48+
IdentityPath string
49+
VMType string
50+
}
51+
52+
func init() {
53+
registry.Commands = append(registry.Commands, registry.CliCommand{
54+
Command: lsCmd,
55+
})
56+
57+
flags := lsCmd.Flags()
58+
formatFlagName := "format"
59+
flags.StringVar(&listFlag.format, formatFlagName, "{{range .}}{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n{{end -}}", "Format volume output using JSON or a Go template")
60+
}
61+
62+
func list(cmd *cobra.Command, args []string) error {
63+
driver, err := macadam.GetDriverByMachineName(defaultMachineName)
64+
if err != nil {
65+
return nil
66+
}
67+
68+
machineReporter := toMachineFormat(driver)
69+
b, err := json.MarshalIndent(machineReporter, "", " ")
70+
if err != nil {
71+
return err
72+
}
73+
os.Stdout.Write(b)
74+
return nil
75+
76+
}
77+
78+
func strTime(t time.Time) string {
79+
iso, err := t.MarshalText()
80+
if err != nil {
81+
return ""
82+
}
83+
return string(iso)
84+
}
85+
86+
func strUint(u uint64) string {
87+
return strconv.FormatUint(u, 10)
88+
}
89+
90+
func toMachineFormat(d *macadam.Driver) []ListReporter {
91+
machineResponses := make([]ListReporter, 0, 1)
92+
93+
vm := d.GetVmConfig()
94+
95+
vmState, err := d.GetState()
96+
if err != nil {
97+
return machineResponses
98+
}
99+
100+
response := new(ListReporter)
101+
response.Image = vm.ImagePath.Path
102+
response.Running = vmState == state.Running
103+
response.LastUp = strTime(vm.LastUp)
104+
response.Created = strTime(vm.Created)
105+
response.CPUs = vm.Resources.CPUs
106+
response.Memory = strUint(uint64(vm.Resources.Memory.ToBytes()))
107+
response.DiskSize = strUint(uint64(vm.Resources.DiskSize.ToBytes()))
108+
response.Port = vm.SSH.Port
109+
response.RemoteUsername = vm.SSH.RemoteUsername
110+
response.IdentityPath = vm.SSH.IdentityPath
111+
response.Starting = vm.Starting
112+
response.VMType = d.GetVMType().String()
113+
114+
machineResponses = append(machineResponses, *response)
115+
116+
return machineResponses
117+
}

pkg/machinedriver/driver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,11 @@ func (d *Driver) UpdateSSHConfig(sshConfig drivers.SSHConfig) error {
610610

611611
return d.vmConfig.Write()
612612
}
613+
614+
func (d *Driver) GetVmConfig() *vmconfigs.MachineConfig {
615+
return d.vmConfig
616+
}
617+
618+
func (d *Driver) GetVMType() define.VMType {
619+
return d.vmProvider.VMType()
620+
}

0 commit comments

Comments
 (0)