Skip to content

Commit 7a4a43a

Browse files
committed
refactor: move config to internal
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
1 parent 10e62d5 commit 7a4a43a

File tree

7 files changed

+129
-119
lines changed

7 files changed

+129
-119
lines changed

registry/remote/credentials/file_store.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"errors"
2222
"fmt"
2323
"strings"
24+
25+
"oras.land/oras-go/v2/registry/remote/internal/configuration"
2426
)
2527

2628
// FileStore implements a credentials store using the docker configuration file
@@ -32,7 +34,7 @@ type FileStore struct {
3234
// If DisablePut is set to true, Put() will return ErrPlaintextPutDisabled.
3335
DisablePut bool
3436

35-
config *Config
37+
config *configuration.Config
3638
}
3739

3840
var (
@@ -48,15 +50,15 @@ var (
4850
//
4951
// Reference: https://docs.docker.com/engine/reference/commandline/cli/#docker-cli-configuration-file-configjson-properties
5052
func NewFileStore(configPath string) (*FileStore, error) {
51-
cfg, err := Load(configPath)
53+
cfg, err := configuration.Load(configPath)
5254
if err != nil {
5355
return nil, err
5456
}
5557
return newFileStore(cfg), nil
5658
}
5759

5860
// newFileStore creates a file credentials store based on the given config instance.
59-
func newFileStore(cfg *Config) *FileStore {
61+
func newFileStore(cfg *configuration.Config) *FileStore {
6062
return &FileStore{config: cfg}
6163
}
6264

@@ -100,8 +102,8 @@ func validateCredentialFormat(cred Credential) error {
100102
}
101103

102104
// NewAuthConfig creates an authConfig based on cred.
103-
func NewAuthConfig(cred Credential) AuthConfig {
104-
return AuthConfig{
105+
func NewAuthConfig(cred Credential) configuration.AuthConfig {
106+
return configuration.AuthConfig{
105107
Auth: encodeAuth(cred.Username, cred.Password),
106108
IdentityToken: cred.RefreshToken,
107109
RegistryToken: cred.AccessToken,
@@ -117,7 +119,7 @@ func encodeAuth(username, password string) string {
117119
}
118120

119121
// NewCredential creates a Credential based on authCfg.
120-
func NewCredential(authCfg AuthConfig) (Credential, error) {
122+
func NewCredential(authCfg configuration.AuthConfig) (Credential, error) {
121123
cred := Credential{
122124
Username: authCfg.Username,
123125
Password: authCfg.Password,
@@ -129,7 +131,7 @@ func NewCredential(authCfg AuthConfig) (Credential, error) {
129131
// override username and password
130132
cred.Username, cred.Password, err = decodeAuth(authCfg.Auth)
131133
if err != nil {
132-
return EmptyCredential, fmt.Errorf("failed to decode auth field: %w: %v", ErrInvalidConfigFormat, err)
134+
return EmptyCredential, fmt.Errorf("failed to decode auth field: %w: %v", configuration.ErrInvalidConfigFormat, err)
133135
}
134136
}
135137
return cred, nil

registry/remote/credentials/memory_store.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"sync"
23+
24+
"oras.land/oras-go/v2/registry/remote/internal/configuration"
2325
)
2426

2527
// memoryStore is a store that keeps credentials in memory.
@@ -37,16 +39,16 @@ func NewMemoryStore() Store {
3739
// Reference: https://docs.docker.com/engine/reference/commandline/cli/#docker-cli-configuration-file-configjson-properties
3840
func NewMemoryStoreFromDockerConfig(c []byte) (Store, error) {
3941
cfg := struct {
40-
Auths map[string]AuthConfig `json:"auths"`
42+
Auths map[string]configuration.AuthConfig `json:"auths"`
4143
}{}
4244
if err := json.Unmarshal(c, &cfg); err != nil {
43-
return nil, fmt.Errorf("failed to unmarshal auth field: %w: %v", ErrInvalidConfigFormat, err)
45+
return nil, fmt.Errorf("failed to unmarshal auth field: %w: %v", configuration.ErrInvalidConfigFormat, err)
4446
}
4547

4648
s := &memoryStore{}
4749
for addr, auth := range cfg.Auths {
4850
// Normalize the auth key to hostname.
49-
hostname := ToHostname(addr)
51+
hostname := configuration.ToHostname(addr)
5052
cred, err := NewCredential(auth)
5153
if err != nil {
5254
return nil, err

registry/remote/credentials/memory_store_from_config_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"os"
2222
"reflect"
2323
"testing"
24+
25+
"oras.land/oras-go/v2/registry/remote/internal/configuration"
2426
)
2527

2628
func TestMemoryStore_Create_fromInvalidConfig(t *testing.T) {
@@ -29,8 +31,8 @@ func TestMemoryStore_Create_fromInvalidConfig(t *testing.T) {
2931
t.Fatalf("failed to read file: %v", err)
3032
}
3133
_, err = NewMemoryStoreFromDockerConfig(f)
32-
if !errors.Is(err, ErrInvalidConfigFormat) {
33-
t.Fatalf("Error: %s is expected", ErrInvalidConfigFormat)
34+
if !errors.Is(err, configuration.ErrInvalidConfigFormat) {
35+
t.Fatalf("Error: %s is expected", configuration.ErrInvalidConfigFormat)
3436
}
3537
}
3638

registry/remote/credentials/store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"path/filepath"
2828

2929
"oras.land/oras-go/v2/internal/syncutil"
30+
"oras.land/oras-go/v2/registry/remote/internal/configuration"
3031
)
3132

3233
const (
@@ -48,7 +49,7 @@ type Store interface {
4849
// DynamicStore dynamically determines which store to use based on the settings
4950
// in the config file.
5051
type DynamicStore struct {
51-
config *Config
52+
config *configuration.Config
5253
options StoreOptions
5354
detectedCredsStore string
5455
setCredsStoreOnce syncutil.OnceOrRetry
@@ -93,7 +94,7 @@ type StoreOptions struct {
9394
// - https://docs.docker.com/engine/reference/commandline/login/#credentials-store
9495
// - https://docs.docker.com/engine/reference/commandline/cli/#docker-cli-configuration-file-configjson-properties
9596
func NewStore(configPath string, opts StoreOptions) (*DynamicStore, error) {
96-
cfg, err := Load(configPath)
97+
cfg, err := configuration.Load(configPath)
9798
if err != nil {
9899
return nil, err
99100
}

registry/remote/credentials/config.go renamed to registry/remote/internal/configuration/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16-
package credentials
16+
package configuration
1717

1818
import (
1919
"bytes"
@@ -26,7 +26,7 @@ import (
2626
"strings"
2727
"sync"
2828

29-
"oras.land/oras-go/v2/registry/remote/credentials/internal/ioutil"
29+
"oras.land/oras-go/v2/registry/remote/internal/ioutil"
3030
)
3131

3232
const (

0 commit comments

Comments
 (0)