ayn2op 2 лет назад
Родитель
Сommit
fb1945b5b4
3 измененных файлов с 22 добавлено и 23 удалено
  1. 9 6
      cmd/run.go
  2. 0 13
      config/config.go
  3. 13 4
      internal/config/config.go

+ 9 - 6
cmd/run.go

@@ -1,13 +1,12 @@
 package cmd
 
 import (
-	"fmt"
 	"log"
 	"os"
 	"path/filepath"
 
-	oldConfig "github.com/ayn2op/discordo/config"
 	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/constants"
 	"github.com/ayn2op/discordo/ui"
 	"github.com/rivo/tview"
 )
@@ -27,15 +26,19 @@ func Run(token string) error {
 		return err
 	}
 
-	fmt.Printf("%+v\n", cfg)
+	logPath, err := os.UserCacheDir()
+	if err != nil {
+		return err
+	}
 
-	path := oldConfig.DefaultLogPath()
-	err = os.MkdirAll(filepath.Dir(path), os.ModePerm)
+	logPath = filepath.Join(logPath, constants.Name)
+	err = os.MkdirAll(logPath, os.ModePerm)
 	if err != nil {
 		return err
 	}
 
-	f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
+	logPath = filepath.Join(logPath, "logs.txt")
+	f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
 	if err != nil {
 		return err
 	}

+ 0 - 13
config/config.go

@@ -1,13 +0,0 @@
-package config
-
-import (
-	"os"
-	"path/filepath"
-
-	"github.com/ayn2op/discordo/internal/constants"
-)
-
-func DefaultLogPath() string {
-	path, _ := os.UserCacheDir()
-	return filepath.Join(path, constants.Name, "logs.txt")
-}

+ 13 - 4
internal/config/config.go

@@ -76,18 +76,27 @@ type Config struct {
 	} `yaml:"theme"`
 }
 
-func Load() (*Config, error) {
+// Recursively creates the configuration directory if it does not exist already and returns the path to the configuration file.
+func initialize() (string, error) {
 	path, err := os.UserConfigDir()
 	if err != nil {
-		return nil, err
+		return "", err
 	}
 
 	path = filepath.Join(path, constants.Name)
 	if err := os.MkdirAll(path, os.ModePerm); err != nil {
-		return nil, err
+		return "", err
 	}
 
-	path = filepath.Join(path, "config.yml")
+	return filepath.Join(path, "config.yml"), nil
+}
+
+// Recursively creates the configuration file if it does not exist already and writes the default configuration to it; otherwise, the existing configuration file is read, and returns the parsed configuration.
+func Load() (*Config, error) {
+	path, err := initialize()
+	if err != nil {
+		return nil, err
+	}
 
 	f, err := os.Open(path)
 	reader := io.Reader(f)