diff --git a/cmd/run.go b/cmd/run.go index dd9fd5a..0d37e23 100644 --- a/cmd/run.go +++ b/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) - - path := oldConfig.DefaultLogPath() - err = os.MkdirAll(filepath.Dir(path), os.ModePerm) + logPath, err := os.UserCacheDir() if err != nil { return err } - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm) + logPath = filepath.Join(logPath, constants.Name) + err = os.MkdirAll(logPath, os.ModePerm) + if err != nil { + return err + } + + logPath = filepath.Join(logPath, "logs.txt") + f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, os.ModePerm) if err != nil { return err } diff --git a/config/config.go b/config/config.go deleted file mode 100644 index 4b4f481..0000000 --- a/config/config.go +++ /dev/null @@ -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") -} diff --git a/internal/config/config.go b/internal/config/config.go index 2082970..60b732e 100644 --- a/internal/config/config.go +++ b/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)