mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat: add uc CLI argument completion for machines, services, volumes (#309)
* 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>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package completion
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
composecli "github.com/compose-spec/compose-go/v2/cli"
|
||||
"github.com/psviderski/uncloud/pkg/client/compose"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func ComposeServices(ctx context.Context, args []string, toComplete string, files, profiles []string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||
project, err := compose.LoadProject(ctx, files, composecli.WithDefaultProfiles(profiles...))
|
||||
if err != nil {
|
||||
return nil, cobra.ShellCompDirectiveError
|
||||
}
|
||||
|
||||
services := project.ServiceNames()
|
||||
names := []cobra.Completion{}
|
||||
for _, service := range services {
|
||||
if slices.Contains(args, service) {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(service, toComplete) {
|
||||
names = append(names, service)
|
||||
}
|
||||
}
|
||||
return names, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package completion
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/psviderski/uncloud/internal/cli"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func Contexts(ctx context.Context, uncli *cli.CLI, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||
contexts := slices.Sorted(maps.Keys(uncli.Config.Contexts))
|
||||
|
||||
names := []cobra.Completion{}
|
||||
for _, context := range contexts {
|
||||
if slices.Contains(args, context) {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(context, toComplete) {
|
||||
names = append(names, context)
|
||||
}
|
||||
names = append(names, context)
|
||||
}
|
||||
|
||||
return names, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package completion
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/psviderski/uncloud/internal/cli"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func Machines(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()
|
||||
|
||||
machines, err := client.ListMachines(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, cobra.ShellCompDirectiveError
|
||||
}
|
||||
names := []cobra.Completion{}
|
||||
for _, machine := range machines {
|
||||
if slices.Contains(args, machine.Machine.Name) {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(machine.Machine.Name, toComplete) {
|
||||
names = append(names, machine.Machine.Name)
|
||||
}
|
||||
}
|
||||
return names, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func MachinesFlag(cmd *cobra.Command) {
|
||||
cmd.RegisterFlagCompletionFunc("machine",
|
||||
func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||
return Machines(cmd.Context(), uncli, args, toComplete)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// 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) {
|
||||
client, err := uncli.ConnectCluster(ctx)
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user