-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(token): completion improvements
- Loading branch information
Showing
6 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package completion | ||
|
||
import ( | ||
"context" | ||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" | ||
|
||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/service" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Token implements argument completion for tokens, by id. | ||
type Token struct{} | ||
|
||
// make sure Token implements the interface | ||
var _ Provider = Token{} | ||
|
||
// CompleteArgument implements completion.Provider | ||
func (s Token) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
tokens, err := svc.GetTokens(ctx, &request.GetTokensRequest{}) | ||
if err != nil { | ||
return None(toComplete) | ||
} | ||
var vals []string | ||
for _, t := range *tokens { | ||
vals = append(vals, t.ID) | ||
} | ||
return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package completion_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion" | ||
smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock" | ||
|
||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
var mockTokens = &upcloud.Tokens{ | ||
{Name: "mock1", ID: "0c1eadbe-efde-adbe-efde-adbeefdeadbe"}, | ||
{Name: "mock2", ID: "0c2eadbe-efde-adbe-efde-adbeefdeadbe"}, | ||
{Name: "bock1", ID: "0c3eadbe-efde-adbe-efde-adbeefdeadbe"}, | ||
{Name: "bock2", ID: "0c4eadbe-efde-adbe-efde-adbeefdeadbe"}, | ||
{Name: "dock1", ID: "0c5eadbe-efde-adbe-efde-adbeefdeadbe"}, | ||
} | ||
|
||
func TestToken_CompleteArgument(t *testing.T) { | ||
for _, test := range []struct { | ||
name string | ||
complete string | ||
expectedMatches []string | ||
expectedDirective cobra.ShellCompDirective | ||
}{ | ||
{name: "basic id", complete: "0c2", expectedMatches: []string{"0c2eadbe-efde-adbe-efde-adbeefdeadbe"}, expectedDirective: cobra.ShellCompDirectiveNoFileComp}, | ||
{name: "multiple ids", complete: "0c", expectedMatches: []string{ | ||
"0c1eadbe-efde-adbe-efde-adbeefdeadbe", | ||
"0c2eadbe-efde-adbe-efde-adbeefdeadbe", | ||
"0c3eadbe-efde-adbe-efde-adbeefdeadbe", | ||
"0c4eadbe-efde-adbe-efde-adbeefdeadbe", | ||
"0c5eadbe-efde-adbe-efde-adbeefdeadbe", | ||
}, expectedDirective: cobra.ShellCompDirectiveNoFileComp}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
mService := new(smock.Service) | ||
mService.On("GetTokens", mock.Anything).Return(mockTokens, nil) | ||
tokens, directive := completion.Token{}.CompleteArgument(context.TODO(), mService, test.complete) | ||
assert.Equal(t, test.expectedMatches, tokens) | ||
assert.Equal(t, test.expectedDirective, directive) | ||
}) | ||
} | ||
} | ||
|
||
func TestToken_CompleteArgumentServiceFail(t *testing.T) { | ||
mService := new(smock.Service) | ||
mService.On("GetTokens", mock.Anything).Return(nil, fmt.Errorf("MOCKFAIL")) | ||
tokens, directive := completion.Token{}.CompleteArgument(context.TODO(), mService, "FOO") | ||
assert.Nil(t, tokens) | ||
assert.Equal(t, cobra.ShellCompDirectiveDefault, directive) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package resolver | ||
|
||
import ( | ||
"context" | ||
|
||
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service" | ||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" | ||
) | ||
|
||
// CachingToken implements resolver for tokens, caching the results. | ||
type CachingToken struct{} | ||
|
||
// make sure we implement the ResolutionProvider interface | ||
var _ ResolutionProvider = CachingToken{} | ||
|
||
// Get implements ResolutionProvider.Get | ||
func (s CachingToken) Get(ctx context.Context, svc internal.AllServices) (Resolver, error) { | ||
tokens, err := svc.GetTokens(ctx, &request.GetTokensRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return func(arg string) Resolved { | ||
rv := Resolved{Arg: arg} | ||
for _, token := range *tokens { | ||
rv.AddMatch(token.ID, MatchArgWithWhitespace(arg, token.ID)) | ||
} | ||
return rv | ||
}, nil | ||
} | ||
|
||
// PositionalArgumentHelp implements resolver.ResolutionProvider | ||
func (s CachingToken) PositionalArgumentHelp() string { | ||
return "<ID...>" | ||
} |