gocc/config.go

83 lines
1.5 KiB
Go
Raw Normal View History

2022-08-17 19:48:36 +02:00
package main
2022-08-17 20:37:12 +02:00
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"git.milar.in/milarin/configfile"
)
2022-08-17 19:48:36 +02:00
type Config struct {
2022-08-17 20:37:12 +02:00
OutputDir string `json:"output_dir"`
OS []string `json:"os"`
Arch []string `json:"arch"`
Silent bool `json:"silent"`
NoCompress bool `json:"no_compress"`
NumThreads int `json:"num_threads"`
}
func LoadConfig() error {
configFilePath, err := configfile.Path("json")
if err != nil {
return err
}
file, err := os.Open(configFilePath)
if err != nil {
return err
}
defer file.Close()
cfg := &Config{}
if err := json.NewDecoder(file).Decode(cfg); err != nil {
return err
}
*OutputDir = cfg.OutputDir
*OS = strings.Join(cfg.OS, ",")
*Arch = strings.Join(cfg.Arch, ",")
*Silent = cfg.Silent
*NoCompress = cfg.NoCompress
*NumThreads = cfg.NumThreads
return nil
}
func SaveConfig() (string, error) {
configFilePath, err := configfile.Path("json")
if err != nil {
return "", err
}
if err := os.MkdirAll(filepath.Dir(configFilePath), 0744); err != nil {
return configFilePath, err
}
file, err := os.Create(configFilePath)
if err != nil {
return configFilePath, err
}
defer file.Close()
enc := json.NewEncoder(file)
enc.SetIndent("", "\t")
cfg := &Config{
OutputDir: *OutputDir,
OS: strings.Split(*OS, ","),
Arch: strings.Split(*Arch, ","),
Silent: *Silent,
NoCompress: *NoCompress,
NumThreads: *NumThreads,
}
if err := enc.Encode(cfg); err != nil {
return configFilePath, err
}
return configFilePath, nil
2022-08-17 19:48:36 +02:00
}