mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
36 lines
984 B
Go
36 lines
984 B
Go
package completion
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/psviderski/uncloud/internal/cli"
|
|
"github.com/psviderski/uncloud/pkg/api"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func Volumes(ctx context.Context, uncli *cli.CLI, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
|
// Disable the connection progress output to not interfere with the shell completion output.
|
|
client, err := uncli.ConnectClusterWithOptions(ctx, cli.ConnectOptions{})
|
|
if err != nil {
|
|
return nil, cobra.ShellCompDirectiveError
|
|
}
|
|
defer client.Close()
|
|
|
|
volumes, err := client.ListVolumes(ctx, &api.VolumeFilter{})
|
|
if err != nil {
|
|
return nil, cobra.ShellCompDirectiveError
|
|
}
|
|
names := []cobra.Completion{}
|
|
for _, volume := range volumes {
|
|
if slices.Contains(args, volume.Volume.Name) {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(volume.Volume.Name, toComplete) {
|
|
names = append(names, volume.Volume.Name)
|
|
}
|
|
}
|
|
return names, cobra.ShellCompDirectiveNoFileComp
|
|
}
|