package config import ( _ "embed" "encoding/json" "log" "os" "os/exec" "path/filepath" ) //go:embed conf.sh var defaultConfig []byte //go:embed wrapper.sh var wrapper string type Config struct { News bool `json:"news"` Quotes bool `json:"quote"` Ramble bool `json:"ramblings"` Ramblings_Path string `json:"ramblings_path"` Articles_Path string `json:"article_path"` } func NewConfig() (c Config) { c.News = true c.Quotes = true c.Ramble = true ramblingsDir := os.Getenv("XDG_DOCUMENTS_DIR") if ramblingsDir == "" { ramblingsDir = filepath.Join(os.Getenv("HOME"), "Documents") } c.Ramblings_Path = filepath.Join(ramblingsDir, "wire") return } func (c *Config) ReadConfig() { configDir := os.Getenv("XDG_CONFIG_HOME") if configDir == "" { configDir = filepath.Join(os.Getenv("HOME"), ".config") } fp := filepath.Join(configDir, "wire", "conf.sh") dir := filepath.Dir(fp) if err := os.MkdirAll(dir, 0755); err != nil { log.Fatal(err) } _, err := os.ReadFile(fp) if err != nil { if os.IsNotExist(err) { if err := os.WriteFile(fp, defaultConfig, 0644); err != nil { log.Fatal(err) } } else { log.Fatal(err) } } out, err := exec.Command("sh", "-c", wrapper, "--", fp).Output() if err != nil { log.Fatal(err) } if err := json.Unmarshal(out, c); err != nil { log.Fatal(err) } }