Skip to content

Commit

Permalink
vetu list: stable sort order: first local, then oci (#48)
Browse files Browse the repository at this point in the history
* vetu list: stable sort order: first local, then oci

* Fix "nolintlint" linter error
  • Loading branch information
edigaryev authored Apr 17, 2024
1 parent f896d93 commit c971cb3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
30 changes: 18 additions & 12 deletions internal/command/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"github.com/spf13/cobra"
)

type listFunc func() ([]lo.Tuple2[string, *vmdirectory.VMDirectory], error)
type desiredSource struct {
Name string
ListFunc func() ([]lo.Tuple2[string, *vmdirectory.VMDirectory], error)
}

var source string
var quiet bool
Expand All @@ -33,25 +36,28 @@ func NewCommand() *cobra.Command {
}

func runList(cmd *cobra.Command, args []string) error {
desiredSources := map[string]listFunc{}
var desiredSources []desiredSource

// Support --source
switch source {
case "local":
desiredSources["local"] = local.List
desiredSources = append(desiredSources,
desiredSource{"local", local.List})
case "oci":
desiredSources["oci"] = remote.List
desiredSources = append(desiredSources,
desiredSource{"oci", remote.List})
case "":
desiredSources["local"] = local.List
desiredSources["oci"] = remote.List
desiredSources = append(desiredSources,
desiredSource{"local", local.List},
desiredSource{"oci", remote.List})
default:
return fmt.Errorf("cannot display VMs from an unsupported source %q", source)
}

// Support -q/--quiet
if quiet {
for _, list := range desiredSources {
vms, err := list()
vms, err := list.ListFunc()
if err != nil {
return err
}
Expand All @@ -72,8 +78,8 @@ func runList(cmd *cobra.Command, args []string) error {

// Retrieve VMs metadata under a global lock
_, err := globallock.With(cmd.Context(), func() (struct{}, error) {
for source, list := range desiredSources {
if err := addVMsToTable(table, source, list); err != nil {
for _, desiredSource := range desiredSources {
if err := addVMsToTable(table, desiredSource); err != nil {
return struct{}{}, err
}
}
Expand All @@ -89,8 +95,8 @@ func runList(cmd *cobra.Command, args []string) error {
return nil
}

func addVMsToTable(table *uitable.Table, source string, list listFunc) error {
vms, err := list()
func addVMsToTable(table *uitable.Table, desiredSource desiredSource) error {
vms, err := desiredSource.ListFunc()
if err != nil {
return err
}
Expand All @@ -103,7 +109,7 @@ func addVMsToTable(table *uitable.Table, source string, list listFunc) error {
return err
}

table.AddRow(source, name, humanize.Bytes(size), vmDir.State())
table.AddRow(desiredSource.Name, name, humanize.Bytes(size), vmDir.State())
}

return nil
Expand Down
1 change: 0 additions & 1 deletion internal/name/remotename/remotename_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:goconst // it's OK to have multiple occurrences of the same string in tests
package remotename_test

import (
Expand Down

0 comments on commit c971cb3

Please sign in to comment.