@@ -14,12 +14,12 @@ import (
14
14
15
15
func newListCommand () * cobra.Command {
16
16
listCommand := & cobra.Command {
17
- Use : "list" ,
17
+ Use : "list [flags] [INSTANCE]... " ,
18
18
Aliases : []string {"ls" },
19
19
Short : "List instances of Lima." ,
20
- Args : cobra .NoArgs ,
20
+ Args : cobra .ArbitraryArgs ,
21
21
RunE : listAction ,
22
- ValidArgsFunction : cobra . NoFileCompletions ,
22
+ ValidArgsFunction : listBashComplete ,
23
23
}
24
24
25
25
listCommand .Flags ().Bool ("json" , false , "JSONify output" )
@@ -28,6 +28,16 @@ func newListCommand() *cobra.Command {
28
28
return listCommand
29
29
}
30
30
31
+ func instanceMatches (arg string , instances []string ) []string {
32
+ matches := []string {}
33
+ for _ , instance := range instances {
34
+ if instance == arg {
35
+ matches = append (matches , instance )
36
+ }
37
+ }
38
+ return matches
39
+ }
40
+
31
41
func listAction (cmd * cobra.Command , args []string ) error {
32
42
quiet , err := cmd .Flags ().GetBool ("quiet" )
33
43
if err != nil {
@@ -42,11 +52,25 @@ func listAction(cmd *cobra.Command, args []string) error {
42
52
return errors .New ("option --quiet conflicts with --json" )
43
53
}
44
54
45
- instances , err := store .Instances ()
55
+ allinstances , err := store .Instances ()
46
56
if err != nil {
47
57
return err
48
58
}
49
59
60
+ instances := []string {}
61
+ if len (args ) > 0 {
62
+ for _ , arg := range args {
63
+ matches := instanceMatches (arg , allinstances )
64
+ if len (matches ) > 0 {
65
+ instances = append (instances , matches ... )
66
+ } else {
67
+ logrus .Warnf ("No instance matching %v found." , arg )
68
+ }
69
+ }
70
+ } else {
71
+ instances = allinstances
72
+ }
73
+
50
74
if quiet {
51
75
for _ , instName := range instances {
52
76
fmt .Fprintln (cmd .OutOrStdout (), instName )
@@ -73,7 +97,7 @@ func listAction(cmd *cobra.Command, args []string) error {
73
97
w := tabwriter .NewWriter (cmd .OutOrStdout (), 4 , 8 , 4 , ' ' , 0 )
74
98
fmt .Fprintln (w , "NAME\t STATUS\t SSH\t ARCH\t CPUS\t MEMORY\t DISK\t DIR" )
75
99
76
- if len (instances ) == 0 {
100
+ if len (allinstances ) == 0 {
77
101
logrus .Warn ("No instance found. Run `limactl start` to create an instance." )
78
102
}
79
103
@@ -100,3 +124,7 @@ func listAction(cmd *cobra.Command, args []string) error {
100
124
101
125
return w .Flush ()
102
126
}
127
+
128
+ func listBashComplete (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
129
+ return bashCompleteInstanceNames (cmd )
130
+ }
0 commit comments