You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.1 KiB
Go

package service
import (
"github.com/BurntSushi/toml"
"log"
"os"
"path/filepath"
)
type ZeroooConfig struct {
Location string `toml:"location"`
Endpoint string `toml:"endpoint"`
}
type Config struct {
Zerooo ZeroooConfig `toml:"zerooo"`
}
func LoadConfig(path string) (*Config, error) {
config := Config{}
_, err := toml.DecodeFile(path, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func CreateConfig(path string, cfg *Config) {
err := os.MkdirAll(filepath.Dir(path), 0755)
Check(err, "Failed to make directories for %q, err: %s", path, err)
conf, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
Check(err, "Couldn't open %q, err: %s", path, err)
encoder := toml.NewEncoder(conf)
log.Printf("ZeroooConfig: %#v", cfg)
err = encoder.Encode(cfg)
if cfg.Zerooo.Location == "" {
cfg.Zerooo.Location = "/etc/zerooo"
}
if cfg.Zerooo.Location[0] != '/' {
cfg.Zerooo.Location = filepath.Join(filepath.Dir(path), cfg.Zerooo.Location)
}
if cfg.Zerooo.Endpoint == "" {
cfg.Zerooo.Endpoint = "https://zer.ooo"
}
Check(err, "Couldn't parse %q, err: %s", path, err)
}