Skip to content

Commit 192b264

Browse files
committed
feat(database): add title resolver and argument completion
1 parent 3d7aebb commit 192b264

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

internal/commands/database/show.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package database
22

33
import (
44
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
5+
"github.com/UpCloudLtd/upcloud-cli/internal/completion"
56
"github.com/UpCloudLtd/upcloud-cli/internal/output"
7+
"github.com/UpCloudLtd/upcloud-cli/internal/resolver"
68
"github.com/UpCloudLtd/upcloud-cli/internal/ui"
79
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
810
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
@@ -23,6 +25,8 @@ func ShowCommand() commands.Command {
2325

2426
type showCommand struct {
2527
*commands.BaseCommand
28+
resolver.CachingDatabase
29+
completion.Database
2630
}
2731

2832
// Execute implements commands.MultipleArgumentCommand

internal/completion/database.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package completion
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/internal/service"
5+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Database implements argument completion for databases, by uuid or title.
10+
type Database struct {
11+
}
12+
13+
// make sure Database implements the interface
14+
var _ Provider = Database{}
15+
16+
// CompleteArgument implements completion.Provider
17+
func (s Database) CompleteArgument(svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
18+
databases, err := svc.GetManagedDatabases(&request.GetManagedDatabasesRequest{})
19+
if err != nil {
20+
return None(toComplete)
21+
}
22+
var vals []string
23+
for _, db := range databases {
24+
vals = append(vals, db.UUID, db.Title)
25+
}
26+
return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
27+
}

internal/resolver/database.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package resolver
2+
3+
import (
4+
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
5+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
6+
)
7+
8+
// CachingDatabase implements resolver for servers, caching the results
9+
type CachingDatabase struct{}
10+
11+
// make sure we implement the ResolutionProvider interface
12+
var _ ResolutionProvider = CachingDatabase{}
13+
14+
// Get implements ResolutionProvider.Get
15+
func (s CachingDatabase) Get(svc internal.AllServices) (Resolver, error) {
16+
databases, err := svc.GetManagedDatabases(&request.GetManagedDatabasesRequest{})
17+
if err != nil {
18+
return nil, err
19+
}
20+
return func(arg string) (uuid string, err error) {
21+
rv := ""
22+
for _, db := range databases {
23+
if db.Title == arg || db.UUID == arg {
24+
if rv != "" {
25+
return "", AmbiguousResolutionError(arg)
26+
}
27+
rv = db.UUID
28+
}
29+
}
30+
if rv != "" {
31+
return rv, nil
32+
}
33+
return "", NotFoundError(arg)
34+
}, nil
35+
}
36+
37+
// PositionalArgumentHelp implements resolver.ResolutionProvider
38+
func (s CachingDatabase) PositionalArgumentHelp() string {
39+
return "<UUID/Title...>"
40+
}

0 commit comments

Comments
 (0)