-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objectstorage): add support for managed object storage names (#299)
- Loading branch information
Showing
6 changed files
with
88 additions
and
0 deletions.
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
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,29 @@ | ||
package completion | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/service" | ||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ObjectStorage implements argument completion for gateways, by uuid or name. | ||
type ObjectStorage struct{} | ||
|
||
// make sure ObjectStorage implements the interface | ||
var _ Provider = ObjectStorage{} | ||
|
||
// CompleteArgument implements completion.Provider | ||
func (s ObjectStorage) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{}) | ||
if err != nil { | ||
return None(toComplete) | ||
} | ||
var vals []string | ||
for _, objsto := range objectstorages { | ||
vals = append(vals, objsto.UUID, objsto.Name) | ||
} | ||
|
||
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,42 @@ | ||
package resolver | ||
|
||
import ( | ||
"context" | ||
|
||
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service" | ||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" | ||
) | ||
|
||
// CachingObjectStorage implements resolver for ObjectStorages, caching the results | ||
type CachingObjectStorage struct{} | ||
|
||
// make sure we implement the ResolutionProvider interface | ||
var _ ResolutionProvider = CachingObjectStorage{} | ||
|
||
// Get implements ResolutionProvider.Get | ||
func (s CachingObjectStorage) Get(ctx context.Context, svc internal.AllServices) (Resolver, error) { | ||
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return func(arg string) (uuid string, err error) { | ||
rv := "" | ||
for _, objsto := range objectstorages { | ||
if MatchArgWithWhitespace(arg, objsto.Name) || objsto.UUID == arg { | ||
if rv != "" { | ||
return "", AmbiguousResolutionError(arg) | ||
} | ||
rv = objsto.UUID | ||
} | ||
} | ||
if rv != "" { | ||
return rv, nil | ||
} | ||
return "", NotFoundError(arg) | ||
}, nil | ||
} | ||
|
||
// PositionalArgumentHelp implements resolver.ResolutionProvider | ||
func (s CachingObjectStorage) PositionalArgumentHelp() string { | ||
return helpUUIDTitle | ||
} |