Skip to content

Commit

Permalink
Merge pull request #1469 from 0x5d/rpk-config-set-abs-file
Browse files Browse the repository at this point in the history
rpk/config: Set the abs path to where the config was generated
  • Loading branch information
emaxerrno authored May 27, 2021
2 parents 636f91e + b4cd17d commit 30b57b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/go/rpk/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
{
Expand Down Expand Up @@ -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)
},
},
}

Expand All @@ -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)
}
})
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/go/rpk/pkg/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
)
}
Expand Down

0 comments on commit 30b57b2

Please sign in to comment.