Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vetu list: stable sort order: first local, then oci #48

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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