2026-07-24 03:54:29 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-24 17:24:07 +01:00
|
|
|
"fmt"
|
2026-07-24 03:54:29 +01:00
|
|
|
"os"
|
|
|
|
|
"path"
|
2026-07-24 17:24:07 +01:00
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
TwitchConfig struct {
|
|
|
|
|
ChannelName string `toml:"channel_name"`
|
|
|
|
|
ClientID string `toml:"client_id"`
|
|
|
|
|
ClientSecret string `toml:"client_secret"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
|
Host string `toml:"host"`
|
|
|
|
|
Port int16 `toml:"port"`
|
|
|
|
|
Twitch TwitchConfig `toml:"twitch"`
|
|
|
|
|
}
|
2026-07-24 03:54:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var CONFIG_DIR = func() string {
|
|
|
|
|
baseDir, _ := os.UserConfigDir()
|
|
|
|
|
dir := path.Join(baseDir, "ari-stream-tools")
|
|
|
|
|
if err := os.MkdirAll(dir, 0750); err != nil { panic(err) }
|
|
|
|
|
return dir
|
|
|
|
|
}()
|
2026-07-24 17:24:07 +01:00
|
|
|
var configFilePath = path.Join(CONFIG_DIR, "config.toml")
|
|
|
|
|
|
|
|
|
|
var DEFAULT_HOST string = "127.0.0.1"
|
|
|
|
|
var DEFAULT_PORT int16 = 8080
|
|
|
|
|
|
|
|
|
|
func DefaultConfig() *Config {
|
|
|
|
|
return &Config{
|
|
|
|
|
Host: DEFAULT_HOST,
|
|
|
|
|
Port: DEFAULT_PORT,
|
|
|
|
|
Twitch: TwitchConfig{
|
|
|
|
|
ChannelName: "",
|
|
|
|
|
ClientID: "",
|
|
|
|
|
ClientSecret: "",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LoadConfig() (*Config, error) {
|
|
|
|
|
configFile, err := os.OpenFile(configFilePath, os.O_CREATE | os.O_RDWR, 0600)
|
|
|
|
|
if err != nil { return nil, fmt.Errorf("open %s: %v", configFilePath, err) }
|
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
|
|
config := DefaultConfig()
|
|
|
|
|
if stat, err := configFile.Stat(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("stat %s: %v", configFilePath, err)
|
|
|
|
|
} else if stat.Size() == 0 {
|
|
|
|
|
if err := toml.NewEncoder(configFile).Encode(config); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("write %s: %v", configFilePath, err)
|
|
|
|
|
}
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := toml.NewDecoder(configFile).Decode(config); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("read %s: %v", configFilePath, err)
|
|
|
|
|
}
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|