mirror of
https://github.com/ko-build/ko.git
synced 2025-04-23 11:37:40 +02:00
Validate KO_CONFIG_PATH (#471)
* Validate KO_CONFIG_PATH to avoid ignored options * Remove dup
This commit is contained in:
parent
1425e4be74
commit
3e5ee5b718
@ -91,14 +91,23 @@ func (bo *BuildOptions) LoadConfig() error {
|
|||||||
}
|
}
|
||||||
// If omitted, use this base image.
|
// If omitted, use this base image.
|
||||||
v.SetDefault("defaultBaseImage", configDefaultBaseImage)
|
v.SetDefault("defaultBaseImage", configDefaultBaseImage)
|
||||||
v.SetConfigName(".ko") // .yaml is implicit
|
const configName = ".ko"
|
||||||
|
|
||||||
|
v.SetConfigName(configName) // .yaml is implicit
|
||||||
v.SetEnvPrefix("KO")
|
v.SetEnvPrefix("KO")
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
|
|
||||||
if override := os.Getenv("KO_CONFIG_PATH"); override != "" {
|
if override := os.Getenv("KO_CONFIG_PATH"); override != "" {
|
||||||
|
path := filepath.Join(override, configName+".yaml")
|
||||||
|
file, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error looking for config file: %w", err)
|
||||||
|
}
|
||||||
|
if !file.Mode().IsRegular() {
|
||||||
|
return fmt.Errorf("config file %s is not a regular file", path)
|
||||||
|
}
|
||||||
v.AddConfigPath(override)
|
v.AddConfigPath(override)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.AddConfigPath(bo.WorkingDirectory)
|
v.AddConfigPath(bo.WorkingDirectory)
|
||||||
|
|
||||||
if err := v.ReadInConfig(); err != nil {
|
if err := v.ReadInConfig(); err != nil {
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
package options
|
package options
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/ko/pkg/build"
|
"github.com/google/ko/pkg/build"
|
||||||
@ -97,3 +99,41 @@ func TestAddBuildOptionsSetsDefaultsForNonFlagOptions(t *testing.T) {
|
|||||||
t.Error("expected Trimpath=true")
|
t.Error("expected Trimpath=true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOverrideConfigPath(t *testing.T) {
|
||||||
|
const envName = "KO_CONFIG_PATH"
|
||||||
|
bo := &BuildOptions{}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
koConfigPath string
|
||||||
|
err string
|
||||||
|
}{{
|
||||||
|
name: ".ko.yaml does not exist",
|
||||||
|
koConfigPath: "testdata",
|
||||||
|
err: "testdata/.ko.yaml: no such file or directory",
|
||||||
|
}, {
|
||||||
|
name: ".ko.yaml is dir",
|
||||||
|
koConfigPath: "testdata/bad-config",
|
||||||
|
err: "testdata/bad-config/.ko.yaml is not a regular file",
|
||||||
|
}, {
|
||||||
|
name: "good",
|
||||||
|
koConfigPath: "testdata/config",
|
||||||
|
}} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
oldEnv := os.Getenv(envName)
|
||||||
|
defer os.Setenv(envName, oldEnv)
|
||||||
|
|
||||||
|
os.Setenv(envName, tc.koConfigPath)
|
||||||
|
err := bo.LoadConfig()
|
||||||
|
if err == nil {
|
||||||
|
if tc.err == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
3
pkg/commands/options/testdata/bad-config/.ko.yaml/.gitignore
vendored
Normal file
3
pkg/commands/options/testdata/bad-config/.ko.yaml/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.# We just want to have a practically empty directory.
|
||||||
|
*
|
||||||
|
!.gitignore
|
Loading…
x
Reference in New Issue
Block a user