Repository: google/ko (go)

Issue:
KO_CONFIG_PATH is confusing
I have been bitten twice by this, so I'm gonna file a bug now :)

If you set `KO_CONFIG_PATH` to a filepath to the `.ko.yaml` file, ko will silently ignore this setting. It seems really unintuitive that it points to the **directory** with a `.ko.yaml` in it, rather than just pointing to the file itself. Either way it should error if `KO_CONFIG_PATH` is set but not readable.

Pull request description:
Allow KO_CONFIG_PATH to be a file
Fixes #245

Patch:
diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go
index e6787ef4..799eebd5 100644
--- a/pkg/commands/options/build.go
+++ b/pkg/commands/options/build.go
@@ -98,11 +98,21 @@ func (bo *BuildOptions) LoadConfig() error {
 	v.AutomaticEnv()
 
 	if override := os.Getenv("KO_CONFIG_PATH"); override != "" {
-		path := filepath.Join(override, configName+".yaml")
-		file, err := os.Stat(path)
+		file, err := os.Stat(override)
 		if err != nil {
 			return fmt.Errorf("error looking for config file: %w", err)
 		}
+		var path string
+		if file.IsDir() {
+			path = filepath.Join(override, configName+".yaml")
+			file, err = os.Stat(path)
+			if err != nil {
+				return fmt.Errorf("error looking for config file: %w", err)
+			}
+		} else {
+			path = override
+		}
+
 		if !file.Mode().IsRegular() {
 			return fmt.Errorf("config file %s is not a regular file", path)
 		}


Test patch:
diff --git a/pkg/commands/options/build_test.go b/pkg/commands/options/build_test.go
index 3d303500..1f066cb6 100644
--- a/pkg/commands/options/build_test.go
+++ b/pkg/commands/options/build_test.go
@@ -112,12 +112,15 @@ func TestOverrideConfigPath(t *testing.T) {
 		koConfigPath: "testdata",
 		err:          "testdata/.ko.yaml: no such file or directory",
 	}, {
-		name:         ".ko.yaml is dir",
+		name:         "config path does not contain .ko.yaml",
 		koConfigPath: "testdata/bad-config",
 		err:          "testdata/bad-config/.ko.yaml is not a regular file",
 	}, {
-		name:         "good",
+		name:         "config path is a directory containing a .ko.yaml",
 		koConfigPath: "testdata/config",
+	}, {
+		name:         "config path points to a file",
+		koConfigPath: "testdata/config/my-ko.yaml",
 	}} {
 		t.Run(tc.name, func(t *testing.T) {
 			oldEnv := os.Getenv(envName)
@@ -126,13 +129,16 @@ func TestOverrideConfigPath(t *testing.T) {
 			os.Setenv(envName, tc.koConfigPath)
 			err := bo.LoadConfig()
 			if err == nil {
+				if tc.err != "" {
+					t.Fatalf("expected error %q, saw nil", tc.err)
+				}
+			} else {
 				if tc.err == "" {
-					return
+					t.Errorf("expected no error, saw: %v", err)
+				}
+				if !strings.Contains(err.Error(), tc.err) {
+					t.Errorf("expected error to contain %q, saw: %v", tc.err, err)
 				}
-				t.Fatalf("expected error %q, saw nil", tc.err)
-			}
-			if !strings.Contains(err.Error(), tc.err) {
-				t.Errorf("expected error to contain %q, saw: %v", tc.err, err)
 			}
 		})
 	}
diff --git a/pkg/commands/options/testdata/config/my-ko.yaml b/pkg/commands/options/testdata/config/my-ko.yaml
new file mode 100644
index 00000000..f69d98f5
--- /dev/null
+++ b/pkg/commands/options/testdata/config/my-ko.yaml
@@ -0,0 +1,1 @@
+defaultBaseImage: wow