github.com/mattevans/edward@v1.9.2/config/load.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func GetConfigPathFromWorkingDirectory(homeDir string) (string, error) {
    13  	wd, err := os.Getwd()
    14  	if err != nil {
    15  		return "", errors.WithStack(err)
    16  	}
    17  	return GetConfigPath(homeDir, wd), nil
    18  }
    19  
    20  // GetConfigPath identifies the location of edward.json, if any exists
    21  func GetConfigPath(homeDir string, wd string) string {
    22  	var pathOptions []string
    23  
    24  	// Config file in Edward Config dir
    25  	pathOptions = append(pathOptions, filepath.Join(homeDir, "edward.json"))
    26  
    27  	// Config file in current working directory
    28  	pathOptions = append(pathOptions, filepath.Join(wd, "edward.json"))
    29  	for path.Dir(wd) != wd {
    30  		wd = path.Dir(wd)
    31  		pathOptions = append(pathOptions, filepath.Join(wd, "edward.json"))
    32  	}
    33  
    34  	for _, path := range pathOptions {
    35  		_, err := os.Stat(path)
    36  		if err != nil {
    37  			continue
    38  		}
    39  		absfp, absErr := filepath.Abs(path)
    40  		if absErr != nil {
    41  			fmt.Println("Error getting config file: ", absErr)
    42  			return ""
    43  		}
    44  		return absfp
    45  	}
    46  
    47  	return ""
    48  }