Skip to content

Commit

Permalink
Revert "Merge pull request #1511 from a8m/a8m/restore-cwd"
Browse files Browse the repository at this point in the history
This reverts commit f4bf1f5, reversing
changes made to 3f68ea2.

Reverting this because it will break existing setups, moving where
generated files get put.
  • Loading branch information
MichaelJCompton committed Apr 15, 2021
1 parent bb59cc4 commit 5ad012e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
24 changes: 9 additions & 15 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() (cfg *Config, err error) {
cfgFile, cwd, err := findCfg()
func LoadConfigFromDefaultLocations() (*Config, error) {
cfgFile, err := findCfg()
if err != nil {
return nil, err
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5ad012e

Please sign in to comment.