added config

This commit is contained in:
2025-03-19 23:37:23 -05:00
parent 8db9d6cf20
commit 2e8f15d91b
5 changed files with 174 additions and 2 deletions
+77
View File
@@ -0,0 +1,77 @@
package backend
import (
"fmt"
"os"
"path"
"github.com/BurntSushi/toml"
)
type Config struct {
LibraryDirPath string
MyConfigValue string
}
var defaultConfig *Config = &Config{
LibraryDirPath: "",
MyConfigValue: "testtesttest",
}
func GetConfig() (*Config, error) {
configDir, err := GetUserConfigDirPath()
if err != nil {
return nil, fmt.Errorf("could not get user config directory path: %w", err)
}
configFilePath := path.Join(configDir, "config.toml")
// create the config file and load defaults if it doesn't exist
_, err = os.Stat(configFilePath)
if os.IsNotExist(err) {
defaultConfig.WriteConfig(configFilePath)
}
conf, err := LoadConfig(configFilePath)
if err != nil {
return nil, fmt.Errorf("could not load config file %s: %w", configFilePath, err)
}
return conf, nil
}
func LoadConfig(configFilePath string) (*Config, error) {
// read in the file
confFileData, err := os.ReadFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("problem reading config file %s: %w", configFilePath, err)
}
// parse it into the config struct
var conf Config
_, err = toml.Decode(string(confFileData), &conf)
if err != nil {
return nil, fmt.Errorf("problem parsing config file %s: %w", configFilePath, err)
}
// validate the config
if err = conf.validate(); err != nil {
return nil, fmt.Errorf("invalid config file at %s: %w", configFilePath, err)
}
return &conf, nil
}
func (c *Config) WriteConfig(configFileWritePath string) error {
confFileData, err := toml.Marshal(c)
if err != nil {
return fmt.Errorf("could not marshal config struct: %w", err)
}
err = os.WriteFile(configFileWritePath, confFileData, os.FileMode(int(0666)))
if err != nil {
return fmt.Errorf("could not write config file: %w", err)
}
return nil
}
// return errors if there is a *breaking* issue with the config
func (c *Config) validate() error {
return nil
}
+82
View File
@@ -0,0 +1,82 @@
package backend
import (
"fmt"
"os"
"os/user"
"runtime"
)
// user config directories vary by operating system and purpose
// Linux/Mac: ~/.config
// Windows: C:\Users\<username>\AppData
func GetUserConfigDirPath() (string, error) {
path := ""
currentUser, err := user.Current()
if err != nil {
return "", fmt.Errorf("could not get current user: %w", err)
}
switch currentOS := runtime.GOOS; currentOS {
case "darwin":
fallthrough
case "linux":
path = fmt.Sprintf("/home/%s/.config/yellowjacket", currentUser.Username)
case "windows":
path = fmt.Sprintf(`C:\Users\%s\AppData\local\yellowjacket\config`, currentUser.Username)
default:
return "", fmt.Errorf("unsupported OS: %s", currentOS)
}
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return "", fmt.Errorf("could not make user config directory: %w", err)
}
// final check
dirInfo, err := os.Stat(path)
if err != nil {
return "", fmt.Errorf("could not stat the user config directory %s: %w", path, err)
}
if !dirInfo.IsDir() {
return "", fmt.Errorf("not a directory: %s", path)
}
return path, nil
}
// user data directories vary by operating system and purpose
// Linux/Mac: ~/.local/share
// Windows: C:\Users\<username>\AppData\Local
func getUserDataDirPath() (string, error) {
path := ""
currentUser, err := user.Current()
if err != nil {
return "", fmt.Errorf("could not get current user: %w", err)
}
switch currentOS := runtime.GOOS; currentOS {
case "darwin":
fallthrough
case "linux":
path = fmt.Sprintf("/home/%s/.local/share/yellowjacket", currentUser.Username)
case "windows":
path = fmt.Sprintf(`C:\Users\%s\AppData\local\yellowjacket\config`, currentUser.Username)
default:
return "", fmt.Errorf("unsupported OS: %s", currentOS)
}
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return "", fmt.Errorf("could not make user data directory: %w", err)
}
// final check
dirInfo, err := os.Stat(path)
if err != nil {
return "", fmt.Errorf("could not stat the user data directory %s: %w", path, err)
}
if !dirInfo.IsDir() {
return "", fmt.Errorf("not a directory: %s", path)
}
return path, nil
}