package main import ( "encoding/json" "os" "path/filepath" "strings" "git.milar.in/milarin/configfile" ) type Config struct { 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 }