Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection.ActiveConfig() should use config.Active() instead of reading configuration directly #1232

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ func ReadValid(name string) (c Configuration, err error) {
var r io.Reader = &bytes.Buffer{}
if file != "" {
if dir == "" {
name = filepath.Join(Directory, file)
// If name refers to a local file, preferentially read the local file.
// Otherwise assume name refers to a file in the config directory.
if info, err := os.Stat(file); errors.Is(err, os.ErrNotExist) || info.IsDir() {
name = filepath.Join(Directory, file)
}
}
r, err = os.Open(name)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,21 @@ func TestReadExternalFile(t *testing.T) {
if c3.Registry.Address != c.Registry.Address {
t.Errorf("want: %s, got: %s", c3.Registry.Address, c.Registry.Address)
}
// Repeat the above with config.ReadValid
// Verify that we can read a file using its full path name.
c4, err := config.ReadValid(f.Name())
if err != nil {
t.Fatal(err)
}
if c4.Registry.Address != c.Registry.Address {
t.Errorf("want: %s, got: %s", c4.Registry.Address, c.Registry.Address)
}
// Verify that we can read a local file using its base name.
c5, err := config.ReadValid(filepath.Base(f.Name()))
if err != nil {
t.Fatal(err)
}
if c5.Registry.Address != c.Registry.Address {
t.Errorf("want: %s, got: %s", c5.Registry.Address, c.Registry.Address)
}
}
11 changes: 8 additions & 3 deletions pkg/connection/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ func ActiveConfig() (Config, error) {
return *active, nil
}

name, err := config.ActiveName()
c, err := config.Active()
if err != nil {
return Config{}, err
}

return ReadConfig(name)
return Config{
Address: c.Registry.Address,
Insecure: c.Registry.Insecure,
Location: c.Registry.Location,
Project: c.Registry.Project,
Token: c.Registry.Token,
}, nil
}

// Reads a Config from a file. If name is empty, no
Expand Down