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

feat(server): take stopped/started state into account in completions #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion internal/commands/server/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func DeleteCommand() commands.Command {
type deleteCommand struct {
*commands.BaseCommand
resolver.CachingServer
completion.Server
completion.StoppedServer
deleteStorages config.OptionalBoolean
}

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/server/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func RestartCommand() commands.Command {
type restartCommand struct {
*commands.BaseCommand
resolver.CachingServer
completion.Server
completion.StartedServer
WaitForServerToStart bool
StopType string
Host int
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func StartCommand() commands.Command {

type startCommand struct {
*commands.BaseCommand
completion.Server
completion.StoppedServer
resolver.CachingServer
host int
avoidHost int
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/server/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type stopCommand struct {
StopType string
wait config.OptionalBoolean
resolver.CachingServer
completion.Server
completion.StartedServer
}

// InitCommand implements Command.InitCommand
Expand Down
31 changes: 30 additions & 1 deletion internal/completion/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package completion

import (
"context"
"slices"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
"github.com/spf13/cobra"
Expand All @@ -15,13 +16,41 @@ var _ Provider = Server{}

// CompleteArgument implements completion.Provider
func (s Server) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeServers(ctx, svc, toComplete)
}

// StartedServer implements argument completion for started servers, by uuid, name or hostname.
type StartedServer struct{}

// make sure StartedServer implements the interface
var _ Provider = StartedServer{}

// CompleteArgument implements completion.Provider
func (s StartedServer) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeServers(ctx, svc, toComplete, "started")
}

// Stopped implements argument completion for stopped servers, by uuid, name or hostname.
type StoppedServer struct{}

// make sure StoppedServer implements the interface
var _ Provider = StoppedServer{}

// CompleteArgument implements completion.Provider
func (s StoppedServer) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeServers(ctx, svc, toComplete, "stopped")
}

func completeServers(ctx context.Context, svc service.AllServices, toComplete string, states ...string) ([]string, cobra.ShellCompDirective) {
servers, err := svc.GetServers(ctx)
if err != nil {
return None(toComplete)
}
var vals []string
for _, v := range servers.Servers {
vals = append(vals, v.UUID, v.Hostname, v.Title)
if len(states) == 0 || slices.Contains(states, v.State) {
vals = append(vals, v.UUID, v.Hostname, v.Title)
}
}
return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
}
23 changes: 19 additions & 4 deletions internal/completion/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ var mockServers = &upcloud.Servers{Servers: []upcloud.Server{
{Title: "bock1", UUID: "jklmno", Hostname: "faa"},
{Title: "bock2", UUID: "pqrstu", Hostname: "fii"},
{Title: "dock1", UUID: "vwxyzä", Hostname: "bfoo"},
{Title: "case started", UUID: "started", Hostname: "astarted", State: "started"},
{Title: "case stopped", UUID: "stopped", Hostname: "astopped", State: "stopped"},
}}

func TestServer_CompleteArgument(t *testing.T) {
mService := new(smock.Service)
mService.On("GetServers", mock.Anything).Return(mockServers, nil)

for _, test := range []struct {
name string
complete string
Expand All @@ -37,13 +42,23 @@ func TestServer_CompleteArgument(t *testing.T) {
{name: "hostnames and titles", complete: "b", expectedMatches: []string{"bock1", "bock2", "bfoo"}, expectedDirective: cobra.ShellCompDirectiveNoFileComp},
} {
t.Run(test.name, func(t *testing.T) {
mService := new(smock.Service)
mService.On("GetServers", mock.Anything).Return(mockServers, nil)
ips, directive := completion.Server{}.CompleteArgument(context.TODO(), mService, test.complete)
assert.Equal(t, test.expectedMatches, ips)
servers, directive := completion.Server{}.CompleteArgument(context.TODO(), mService, test.complete)
assert.Equal(t, test.expectedMatches, servers)
assert.Equal(t, test.expectedDirective, directive)
})
}

t.Run("stopped", func(t *testing.T) {
servers, directive := completion.StoppedServer{}.CompleteArgument(context.TODO(), mService, "s")
assert.Equal(t, []string{"stopped"}, servers)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})

t.Run("started", func(t *testing.T) {
servers, directive := completion.StartedServer{}.CompleteArgument(context.TODO(), mService, "s")
assert.Equal(t, []string{"started"}, servers)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})
}

func TestServer_CompleteArgumentServiceFail(t *testing.T) {
Expand Down
Loading