77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"gitea.kleinsense.nl/DariusKlein/kleinTodo/common"
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
type Config struct {
|
|
Settings Settings `toml:"settings"`
|
|
Server Server `toml:"server"`
|
|
}
|
|
|
|
type Settings struct {
|
|
Environment string `toml:"environment"`
|
|
}
|
|
|
|
type Server struct {
|
|
Credentials common.Credentials `toml:"credentials"`
|
|
Token string `toml:"token"`
|
|
Url string `toml:"url"`
|
|
}
|
|
|
|
var config Config
|
|
|
|
var DefaultConfig = Config{
|
|
Settings: Settings{Environment: "user"},
|
|
Server: Server{
|
|
Credentials: common.Credentials{
|
|
Username: "user",
|
|
Password: "password",
|
|
},
|
|
Token: "token",
|
|
Url: "https://EXAMPLE.com",
|
|
},
|
|
}
|
|
|
|
func GetConfigPath() (path string, configPath string, err error) {
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
path = filepath.Dir(homeDir + "\\AppData\\Local\\kleinTodo\\client\\")
|
|
case "linux":
|
|
path = filepath.Dir(homeDir + "/.config/kleinTodo/client/")
|
|
default:
|
|
return "", "", errors.New("unsupported platform")
|
|
}
|
|
|
|
configPath = filepath.Join(path, "/config.toml")
|
|
|
|
return path, configPath, nil
|
|
}
|
|
|
|
func ReadConfig() (Config, error) {
|
|
_, configPath, err := GetConfigPath()
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
|
|
file, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
|
|
_, err = toml.Decode(string(file), &config)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|