Create the configuration and log file in configuration directory

This commit is contained in:
ayn2op 2022-12-16 19:05:43 +05:30
parent d0027b0add
commit 2a1c583b2b
2 changed files with 16 additions and 3 deletions

View file

@ -82,8 +82,15 @@ func New() *Config {
func (cfg *Config) Load() error {
path, _ := os.UserConfigDir()
path = filepath.Join(path, Name+".yml")
// Open the existing configuration file with read-only flag.
// Create the configuration directory if it does not exist already.
path = filepath.Join(path, Name)
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
path = filepath.Join(path, "config.yml")
// Open the configuration file with create and read-write flag.
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return err

View file

@ -17,7 +17,13 @@ func init() {
flag.StringVar(&tokenFlag, "token", "", "The authentication token.")
path, _ := os.UserCacheDir()
path = filepath.Join(path, config.Name+".log")
path = filepath.Join(path, config.Name)
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Fatal(err)
}
path = filepath.Join(path, "logs.txt")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal(err)