mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
* Allow for argument completion This shows for a single command on how to complete service names in the cluster: `uc inspect <TAB>` will complete using ListServices See: #307 Signed-off-by: Miek Gieben <miek@miek.nl> * All cluster interactions can be completed Signed-off-by: Miek Gieben <miek@miek.nl> * Machines -m flag completion Signed-off-by: Miek Gieben <miek@miek.nl> * Add completion for "ctx use" Signed-off-by: Miek Gieben <miek@miek.nl> * Complete from compose file too Signed-off-by: Miek Gieben <miek@miek.nl> * Allow for argument completion This shows for a single command on how to complete service names in the cluster: `uc inspect <TAB>` will complete using ListServices See: #307 Signed-off-by: Miek Gieben <miek@miek.nl> * All cluster interactions can be completed Signed-off-by: Miek Gieben <miek@miek.nl> * Machines -m flag completion Signed-off-by: Miek Gieben <miek@miek.nl> * Add completion for "ctx use" Signed-off-by: Miek Gieben <miek@miek.nl> * Complete from compose file too Signed-off-by: Miek Gieben <miek@miek.nl> * Move to better place Signed-off-by: Miek Gieben <miek@miek.nl> * Adjust imports Signed-off-by: Miek Gieben <miek@miek.nl> * prefix match for context too Signed-off-by: Miek Gieben <miek@miek.nl> --------- Signed-off-by: Miek Gieben <miek@miek.nl>
35 lines
857 B
Go
35 lines
857 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) {
|
|
client, err := uncli.ConnectCluster(ctx)
|
|
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
|
|
}
|