LoadWithPrefix load a credential from the repo config with a prefix
(repo repository.RepoKeyring, prefix string)
| 74 | |
| 75 | // LoadWithPrefix load a credential from the repo config with a prefix |
| 76 | func LoadWithPrefix(repo repository.RepoKeyring, prefix string) (Credential, error) { |
| 77 | keys, err := repo.Keyring().Keys() |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | // preallocate but empty |
| 83 | matching := make([]Credential, 0, 5) |
| 84 | |
| 85 | for _, key := range keys { |
| 86 | if !strings.HasPrefix(key, keyringKeyPrefix+prefix) { |
| 87 | continue |
| 88 | } |
| 89 | |
| 90 | item, err := repo.Keyring().Get(key) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | |
| 95 | cred, err := decode(item) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | matching = append(matching, cred) |
| 101 | } |
| 102 | |
| 103 | if len(matching) > 1 { |
| 104 | ids := make([]entity.Id, len(matching)) |
| 105 | for i, cred := range matching { |
| 106 | ids[i] = cred.ID() |
| 107 | } |
| 108 | return nil, NewErrMultipleMatchCredential(ids) |
| 109 | } |
| 110 | |
| 111 | if len(matching) == 0 { |
| 112 | return nil, ErrCredentialNotExist |
| 113 | } |
| 114 | |
| 115 | return matching[0], nil |
| 116 | } |
| 117 | |
| 118 | // decode is a helper to construct a Credential from the keyring Item |
| 119 | func decode(item repository.Item) (Credential, error) { |