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