Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen/config: restore current working directory after changing it #1511

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func LoadDefaultConfig() (*Config, error) {

// LoadConfigFromDefaultLocations looks for a config file in the current directory, and all parent directories
// walking up the tree. The closest config file will be returned.
func LoadConfigFromDefaultLocations() (*Config, error) {
cfgFile, err := findCfg()
func LoadConfigFromDefaultLocations() (cfg *Config, err error) {
cfgFile, cwd, err := findCfg()
if err != nil {
return nil, err
}
Expand All @@ -80,6 +80,12 @@ func LoadConfigFromDefaultLocations() (*Config, error) {
if err != nil {
return nil, errors.Wrap(err, "unable to enter config dir")
}
defer func() {
if cerr := os.Chdir(cwd); cerr != nil {
cfg = nil
err = errors.Wrap(cerr, "unable to restore working directory")
}
}()
return LoadConfig(cfgFile)
}

Expand Down Expand Up @@ -467,24 +473,24 @@ func inStrSlice(haystack []string, needle string) bool {

// findCfg searches for the config file in this directory and all parents up the tree
// looking for the closest match
func findCfg() (string, error) {
dir, err := os.Getwd()
func findCfg() (string, string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", errors.Wrap(err, "unable to get working dir to findCfg")
return "", "", errors.Wrap(err, "unable to get working dir to findCfg")
}

cfg := findCfgInDir(dir)
cfg := findCfgInDir(cwd)

for cfg == "" && dir != filepath.Dir(dir) {
for dir := cwd; cfg == "" && dir != filepath.Dir(dir); {
dir = filepath.Dir(dir)
cfg = findCfgInDir(dir)
}

if cfg == "" {
return "", os.ErrNotExist
return "", "", os.ErrNotExist
}

return cfg, nil
return cfg, cwd, nil
}

func findCfgInDir(dir string) string {
Expand Down
5 changes: 5 additions & 0 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ func TestLoadConfigFromDefaultLocation(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "otherdir"))
require.NoError(t, err)

before, err := os.Getwd()
require.NoError(t, err)
cfg, err = LoadConfigFromDefaultLocations()
require.NoError(t, err)
require.Equal(t, StringList{"outer"}, cfg.SchemaFilename)
after, err := os.Getwd()
require.NoError(t, err)
require.Equal(t, before, after)
})

t.Run("will return error if config doesn't exist", func(t *testing.T) {
Expand Down