mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
36 lines
979 B
Go
36 lines
979 B
Go
// Package completion implements completion functions for the uc cli.
|
|
package completion
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/psviderski/uncloud/internal/cli"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func Services(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()
|
|
|
|
services, err := client.ListServices(ctx)
|
|
if err != nil {
|
|
return nil, cobra.ShellCompDirectiveError
|
|
}
|
|
names := []cobra.Completion{}
|
|
for _, service := range services {
|
|
if slices.Contains(args, service.Name) {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(service.Name, toComplete) {
|
|
names = append(names, service.Name)
|
|
}
|
|
}
|
|
return names, cobra.ShellCompDirectiveNoFileComp
|
|
}
|