Skip to content

Commit 6a16a6b

Browse files
committed
Exposing default config path
1 parent 26bad8c commit 6a16a6b

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

goflags.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/cnf/structhash"
19+
"github.com/projectdiscovery/fileutil"
1920
"gopkg.in/yaml.v2"
2021
)
2122

@@ -98,21 +99,16 @@ func (flagSet *FlagSet) Parse() error {
9899
flagSet.CommandLine.Usage = flagSet.usageFunc
99100
_ = flagSet.CommandLine.Parse(os.Args[1:])
100101

101-
appName := filepath.Base(os.Args[0])
102-
// trim extension from app name
103-
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
104-
homePath, err := os.UserHomeDir()
102+
configFilePath, err := GetConfigFilePath()
105103
if err != nil {
106104
return err
107105
}
108-
109-
config := filepath.Join(homePath, ".config", appName, "config.yaml")
110-
_ = os.MkdirAll(filepath.Dir(config), os.ModePerm)
111-
if _, err := os.Stat(config); os.IsNotExist(err) {
106+
_ = os.MkdirAll(filepath.Dir(configFilePath), os.ModePerm)
107+
if !fileutil.FileExists(configFilePath) {
112108
configData := flagSet.generateDefaultConfig()
113-
return ioutil.WriteFile(config, configData, os.ModePerm)
109+
return ioutil.WriteFile(configFilePath, configData, os.ModePerm)
114110
}
115-
_ = flagSet.MergeConfigFile(config) // try to read default config after parsing flags
111+
_ = flagSet.MergeConfigFile(configFilePath) // try to read default config after parsing flags
116112
return nil
117113
}
118114

path.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package goflags
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
// GetConfigFilePath returns the default config file path
10+
func GetConfigFilePath() (string, error) {
11+
appName := filepath.Base(os.Args[0])
12+
// trim extension from app name
13+
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
14+
homePath, err := os.UserHomeDir()
15+
if err != nil {
16+
return "", err
17+
}
18+
19+
return filepath.Join(homePath, ".config", appName, "config.yaml"), nil
20+
}

0 commit comments

Comments
 (0)