File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ package database
22
33import (
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
2426type showCommand struct {
2527 * commands.BaseCommand
28+ resolver.CachingDatabase
29+ completion.Database
2630}
2731
2832// Execute implements commands.MultipleArgumentCommand
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments