Skip to content

Commit

Permalink
Merge pull request #413 from afbjorklund/list-instance
Browse files Browse the repository at this point in the history
Add possibility to list particular instances
  • Loading branch information
jandubois authored Nov 17, 2021
2 parents 908c5a7 + f8e750f commit 8dc4f8c
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

func newListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list",
Use: "list [flags] [INSTANCE]...",
Aliases: []string{"ls"},
Short: "List instances of Lima.",
Args: cobra.NoArgs,
Args: cobra.ArbitraryArgs,
RunE: listAction,
ValidArgsFunction: cobra.NoFileCompletions,
ValidArgsFunction: listBashComplete,
}

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

func instanceMatches(arg string, instances []string) []string {
matches := []string{}
for _, instance := range instances {
if instance == arg {
matches = append(matches, instance)
}
}
return matches
}

func listAction(cmd *cobra.Command, args []string) error {
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
Expand All @@ -42,11 +52,25 @@ func listAction(cmd *cobra.Command, args []string) error {
return errors.New("option --quiet conflicts with --json")
}

instances, err := store.Instances()
allinstances, err := store.Instances()
if err != nil {
return err
}

instances := []string{}
if len(args) > 0 {
for _, arg := range args {
matches := instanceMatches(arg, allinstances)
if len(matches) > 0 {
instances = append(instances, matches...)
} else {
logrus.Warnf("No instance matching %v found.", arg)
}
}
} else {
instances = allinstances
}

if quiet {
for _, instName := range instances {
fmt.Fprintln(cmd.OutOrStdout(), instName)
Expand All @@ -73,7 +97,7 @@ func listAction(cmd *cobra.Command, args []string) error {
w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "NAME\tSTATUS\tSSH\tARCH\tCPUS\tMEMORY\tDISK\tDIR")

if len(instances) == 0 {
if len(allinstances) == 0 {
logrus.Warn("No instance found. Run `limactl start` to create an instance.")
}

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

return w.Flush()
}

func listBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return bashCompleteInstanceNames(cmd)
}

0 comments on commit 8dc4f8c

Please sign in to comment.