-
Notifications
You must be signed in to change notification settings - Fork 16
/
path.go
43 lines (38 loc) · 1.16 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package goflags
import (
"os"
"path/filepath"
"strings"
)
// GetConfigFilePath returns the config file path
func (flagSet *FlagSet) GetConfigFilePath() (string, error) {
// return configFilePath if already set
if flagSet.configFilePath != "" {
return flagSet.configFilePath, nil
}
// generate default config name
appName := filepath.Base(os.Args[0])
// trim extension from app name
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
homePath, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homePath, ".config", appName, "config.yaml"), nil
}
// SetConfigFilePath sets custom config file path
func (flagSet *FlagSet) SetConfigFilePath(filePath string) {
flagSet.configFilePath = filePath
}
// Deprecated: Use FlagSet.GetConfigFilePath instead.
// GetConfigFilePath returns the default config file path
func GetConfigFilePath() (string, error) {
appName := filepath.Base(os.Args[0])
// trim extension from app name
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
homePath, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homePath, ".config", appName, "config.yaml"), nil
}