Skip to content

Commit f8e750f

Browse files
committed
Add possibility to list particular instances
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 2d0572d commit f8e750f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

cmd/limactl/list.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414

1515
func newListCommand() *cobra.Command {
1616
listCommand := &cobra.Command{
17-
Use: "list",
17+
Use: "list [flags] [INSTANCE]...",
1818
Aliases: []string{"ls"},
1919
Short: "List instances of Lima.",
20-
Args: cobra.NoArgs,
20+
Args: cobra.ArbitraryArgs,
2121
RunE: listAction,
22-
ValidArgsFunction: cobra.NoFileCompletions,
22+
ValidArgsFunction: listBashComplete,
2323
}
2424

2525
listCommand.Flags().Bool("json", false, "JSONify output")
@@ -28,6 +28,16 @@ func newListCommand() *cobra.Command {
2828
return listCommand
2929
}
3030

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+
3141
func listAction(cmd *cobra.Command, args []string) error {
3242
quiet, err := cmd.Flags().GetBool("quiet")
3343
if err != nil {
@@ -42,11 +52,25 @@ func listAction(cmd *cobra.Command, args []string) error {
4252
return errors.New("option --quiet conflicts with --json")
4353
}
4454

45-
instances, err := store.Instances()
55+
allinstances, err := store.Instances()
4656
if err != nil {
4757
return err
4858
}
4959

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+
5074
if quiet {
5175
for _, instName := range instances {
5276
fmt.Fprintln(cmd.OutOrStdout(), instName)
@@ -73,7 +97,7 @@ func listAction(cmd *cobra.Command, args []string) error {
7397
w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
7498
fmt.Fprintln(w, "NAME\tSTATUS\tSSH\tARCH\tCPUS\tMEMORY\tDISK\tDIR")
7599

76-
if len(instances) == 0 {
100+
if len(allinstances) == 0 {
77101
logrus.Warn("No instance found. Run `limactl start` to create an instance.")
78102
}
79103

@@ -100,3 +124,7 @@ func listAction(cmd *cobra.Command, args []string) error {
100124

101125
return w.Flush()
102126
}
127+
128+
func listBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
129+
return bashCompleteInstanceNames(cmd)
130+
}

0 commit comments

Comments
 (0)