Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
Signed-off-by: wangxiaoxuan273 <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Apr 20, 2023
1 parent 00eba81 commit 3678f50
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 66 deletions.
47 changes: 47 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,50 @@ type Store interface {
// Delete removes credentials from the store for the given server address.
Delete(ctx context.Context, serverAddress string) error
}

// storeWithFallbacks is a store that has multiple fallback stores.
// Please use the NewStoreWithFallbacks to create new instances of
// storeWithFallbacks.
type storeWithFallbacks struct {
stores []Store
}

// NewStoreWithFallbacks returns a new store based on the given stores.
// The first store is used as the primary store. The second and the
// subsequent stores will be used as fallbacks for the first store.
func NewStoreWithFallbacks(store Store, fallbacks ...Store) Store {
if fallbacks == nil {
return store
}
return &storeWithFallbacks{
stores: append([]Store{store}, fallbacks...),
}
}

// Get retrieves credentials from the StoreWithFallbacks for the given server.
// It searches the primary and the fallback stores for the credentials of serverAddress
// and returns when it finds the credentials in any of the stores.
func (sf *storeWithFallbacks) Get(ctx context.Context, serverAddress string) (auth.Credential, error) {
for _, s := range sf.stores {
cred, err := s.Get(ctx, serverAddress)
if err != nil {
return auth.EmptyCredential, err
}
if cred != auth.EmptyCredential {
return cred, nil
}
}
return auth.EmptyCredential, nil
}

// Put saves credentials into the StoreWithFallbacks. It puts
// the credentials into the primary store.
func (sf *storeWithFallbacks) Put(ctx context.Context, serverAddress string, cred auth.Credential) error {
return sf.stores[0].Put(ctx, serverAddress, cred)
}

// Delete removes credentials from the StoreWithFallbacks for the given server.
// It deletes the credentials from the primary store.
func (sf *storeWithFallbacks) Delete(ctx context.Context, serverAddress string) error {
return sf.stores[0].Delete(ctx, serverAddress)
}
File renamed without changes.
66 changes: 0 additions & 66 deletions store_with_fallbacks.go

This file was deleted.

0 comments on commit 3678f50

Please sign in to comment.