overhaul config, refactor twitch labels, init satellite
This commit is contained in:
parent
b2485eda79
commit
097a36ac69
6 changed files with 161 additions and 152 deletions
|
|
@ -1,8 +1,25 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"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"`
|
||||
}
|
||||
)
|
||||
|
||||
var CONFIG_DIR = func() string {
|
||||
|
|
@ -11,3 +28,40 @@ var CONFIG_DIR = func() string {
|
|||
if err := os.MkdirAll(dir, 0750); err != nil { panic(err) }
|
||||
return dir
|
||||
}()
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue