From 5ad012e3d7be1127706b9c8a3da0378df3a98ec1 Mon Sep 17 00:00:00 2001 From: MichaelJCompton Date: Thu, 15 Apr 2021 16:58:09 +1000 Subject: [PATCH] Revert "Merge pull request #1511 from a8m/a8m/restore-cwd" This reverts commit f4bf1f591b6a3884041876deb64ce0dd70c3c883, reversing changes made to 3f68ea27a1a9fea2064caf877f7e24d00aa439e6. Reverting this because it will break existing setups, moving where generated files get put. --- codegen/config/config.go | 24 +++++++++--------------- codegen/config/config_test.go | 5 ----- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/codegen/config/config.go b/codegen/config/config.go index 24ea9e7ef04..ba939fcf59a 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -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() (cfg *Config, err error) { - cfgFile, cwd, err := findCfg() +func LoadConfigFromDefaultLocations() (*Config, error) { + cfgFile, err := findCfg() if err != nil { return nil, err } @@ -80,12 +80,6 @@ func LoadConfigFromDefaultLocations() (cfg *Config, err 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) } @@ -473,24 +467,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, string, error) { - cwd, err := os.Getwd() +func findCfg() (string, error) { + dir, 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(cwd) + cfg := findCfgInDir(dir) - for dir := cwd; cfg == "" && dir != filepath.Dir(dir); { + for cfg == "" && dir != filepath.Dir(dir) { dir = filepath.Dir(dir) cfg = findCfgInDir(dir) } if cfg == "" { - return "", "", os.ErrNotExist + return "", os.ErrNotExist } - return cfg, cwd, nil + return cfg, nil } func findCfgInDir(dir string) string { diff --git a/codegen/config/config_test.go b/codegen/config/config_test.go index de63b15d3f0..b16e90c11a5 100644 --- a/codegen/config/config_test.go +++ b/codegen/config/config_test.go @@ -72,14 +72,9 @@ 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) {