refactor: cleanup

This commit is contained in:
ayn2op 2023-10-28 20:03:13 +05:30
parent dd416b3305
commit fb1945b5b4
3 changed files with 23 additions and 24 deletions

View file

@ -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
}

View file

@ -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")
}

View file

@ -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)