diff --git a/src/go/rpk/pkg/config/config_test.go b/src/go/rpk/pkg/config/config_test.go index f54d990a1cb20..3b6275c40a937 100644 --- a/src/go/rpk/pkg/config/config_test.go +++ b/src/go/rpk/pkg/config/config_test.go @@ -1724,6 +1724,7 @@ func TestReadOrGenerate(t *testing.T) { name string setup func(afero.Fs) error configFile string + check func(st *testing.T, conf *Config) expectError bool }{ { @@ -1751,7 +1752,12 @@ func TestReadOrGenerate(t *testing.T) { }, { name: "it should set config_file to the right value", - configFile: "/some/arbitrary/path/redpanda.yaml", + configFile: "./redpanda.yaml", + check: func(st *testing.T, conf *Config) { + path, err := filepath.Abs("./redpanda.yaml") + require.NoError(st, err) + require.Exactly(st, conf.ConfigFile, path) + }, }, } @@ -1771,7 +1777,9 @@ func TestReadOrGenerate(t *testing.T) { require.NoError(t, err) conf, err := mgr.Read(tt.configFile) require.NoError(t, err) - require.Exactly(t, tt.configFile, conf.ConfigFile) + if tt.check != nil { + tt.check(t, conf) + } }) } } diff --git a/src/go/rpk/pkg/config/manager.go b/src/go/rpk/pkg/config/manager.go index eb3c70db09bce..c6b03345a5935 100644 --- a/src/go/rpk/pkg/config/manager.go +++ b/src/go/rpk/pkg/config/manager.go @@ -98,8 +98,12 @@ func (m *manager) FindOrGenerate(path string) (*Config, error) { // Tries reading a config file at the given path, or generates a default config // and writes it to the path. func readOrGenerate(fs afero.Fs, v *viper.Viper, path string) (*Config, error) { - v.SetConfigFile(path) - err := v.ReadInConfig() + abs, err := absPath(path) + if err != nil { + return nil, err + } + v.SetConfigFile(abs) + err = v.ReadInConfig() if err == nil { // The config file's there, there's nothing to do. return unmarshal(v) @@ -109,25 +113,25 @@ func readOrGenerate(fs afero.Fs, v *viper.Viper, path string) (*Config, error) { if err != nil && !notFound && !notExist { return nil, fmt.Errorf( "An error happened while trying to read %s: %v", - path, + abs, err, ) } log.Debug(err) log.Infof( "Couldn't find config file at %s. Generating it.", - path, + abs, ) - v.Set("config_file", path) - err = createConfigDir(fs, path) + v.Set("config_file", abs) + err = createConfigDir(fs, abs) if err != nil { return nil, err } - err = v.WriteConfigAs(path) + err = v.WriteConfigAs(abs) if err != nil { return nil, fmt.Errorf( "Couldn't write config to %s: %v", - path, + abs, err, ) }