Ver Fonte

Create the configuration and log file in configuration directory

ayn2op há 3 anos atrás
pai
commit
2a1c583b2b
2 ficheiros alterados com 16 adições e 3 exclusões
  1. 9 2
      config/config.go
  2. 7 1
      main.go

+ 9 - 2
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

+ 7 - 1
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)