From 73a3c4e70a55eab5b88c4181a9762587ae15bdeb Mon Sep 17 00:00:00 2001 From: Tim Burks Date: Mon, 16 Oct 2023 12:40:40 -0700 Subject: [PATCH] connection.ActiveConfig() should use config.Active() instead of reading configuration directly (#1232) --- pkg/config/config.go | 6 +++++- pkg/config/configuration_test.go | 17 +++++++++++++++++ pkg/connection/config.go | 11 ++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 3fd7f53e0..a00317d1f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 { diff --git a/pkg/config/configuration_test.go b/pkg/config/configuration_test.go index a20f163e6..2952be3d9 100644 --- a/pkg/config/configuration_test.go +++ b/pkg/config/configuration_test.go @@ -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) + } } diff --git a/pkg/connection/config.go b/pkg/connection/config.go index 24c8abd5f..7f77b742f 100644 --- a/pkg/connection/config.go +++ b/pkg/connection/config.go @@ -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