default.go 774 B

1234567891011121314151617181920212223242526272829
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. )
  7. // DefaultPath returns a path to a config file alongside the running executable, preferring config.yaml over config.yml.
  8. // If neither file exists an error is returned that names both paths checked.
  9. func DefaultPath() (string, error) {
  10. ex, err := os.Executable()
  11. if err != nil {
  12. return "", err
  13. }
  14. return defaultPathInDir(filepath.Dir(ex))
  15. }
  16. func defaultPathInDir(dir string) (string, error) {
  17. yamlPath := filepath.Join(dir, "config.yaml")
  18. if _, err := os.Stat(yamlPath); err == nil {
  19. return yamlPath, nil
  20. }
  21. ymlPath := filepath.Join(dir, "config.yml")
  22. if _, err := os.Stat(ymlPath); err == nil {
  23. return ymlPath, nil
  24. }
  25. return "", fmt.Errorf("no default config found at %s or %s", yamlPath, ymlPath)
  26. }