Skip to content

Commit

Permalink
connection.ActiveConfig() should use config.Active() instead of readi…
Browse files Browse the repository at this point in the history
…ng configuration directly (#1232)
  • Loading branch information
timburks authored Oct 16, 2023
1 parent f805290 commit 73a3c4e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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

0 comments on commit 73a3c4e

Please sign in to comment.