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

5 years ago
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) {
5 years ago
err := os.MkdirAll(filepath.Dir(path), 0755)
Check(err, "Failed to make directories for %q, err: %s", path, err)
5 years ago
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)
5 years ago
if cfg.Zerooo.Location == "" {
cfg.Zerooo.Location = "/etc/zerooo"
}
5 years ago
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"
}
5 years ago
Check(err, "Couldn't parse %q, err: %s", path, err)
}