Skip to content

Commit

Permalink
client/setec: make GetIfChanged fall back to Get for version 0 (#96)
Browse files Browse the repository at this point in the history
As a convenience, if the caller passes version 0 to GetIfChanged in the client,
treat it as a call to plain Get. Document that this is the case.
  • Loading branch information
creachadair authored Feb 12, 2024
1 parent 70d1ccd commit 3648fd9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions client/setec/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c Client) List(ctx context.Context) ([]*api.SecretInfo, error) {
return do[[]*api.SecretInfo](ctx, c, "/api/list", api.ListRequest{})
}

// Get fetches a secret value by name.
// Get fetches the current active secret value for name.
func (c Client) Get(ctx context.Context, name string) (*api.SecretValue, error) {
return do[*api.SecretValue](ctx, c, "/api/get", api.GetRequest{
Name: name,
Expand All @@ -101,16 +101,21 @@ func (c Client) Get(ctx context.Context, name string) (*api.SecretValue, error)
// GetIfChanged fetches a secret value by name, if the active version on the
// server is different from oldVersion. If the active version on the server is
// the same as oldVersion, it reports api.ErrValueNotChanged without returning
// a secret.
// a secret. As a special case, if oldVersion == 0 then GetIfVersion behaves as
// Get and retrieves the current active version.
func (c Client) GetIfChanged(ctx context.Context, name string, oldVersion api.SecretVersion) (*api.SecretValue, error) {
if oldVersion == api.SecretVersionDefault {
return c.Get(ctx, name)
}
return do[*api.SecretValue](ctx, c, "/api/get", api.GetRequest{
Name: name,
Version: oldVersion,
UpdateIfChanged: true,
})
}

// Get fetches a secret value by name and version.
// Get fetches a secret value by name and version. If version == 0, GetVersion
// retrieves the current active version.
func (c Client) GetVersion(ctx context.Context, name string, version api.SecretVersion) (*api.SecretValue, error) {
return do[*api.SecretValue](ctx, c, "/api/get", api.GetRequest{
Name: name,
Expand Down

0 comments on commit 3648fd9

Please sign in to comment.