From 2a1c583b2bbfc7c523fbff329c45ad99fe7e8365 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Fri, 16 Dec 2022 19:05:43 +0530 Subject: [PATCH] Create the configuration and log file in configuration directory --- config/config.go | 11 +++++++++-- main.go | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index be83e39..b156f48 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/main.go b/main.go index 7247ae0..521cf52 100644 --- a/main.go +++ b/main.go @@ -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)