Same changes

This commit is contained in:
vxclutch
2026-05-24 13:53:50 -04:00
parent 877d785e15
commit cbf9f0e695
6 changed files with 178 additions and 13 deletions

70
internal/config/config.go Normal file
View File

@@ -0,0 +1,70 @@
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"`
Ramblings bool `json:"ramblings"`
Ramblings_Path string `json:"ramblings_path"`
}
func NewConfig() (c Config) {
c.News = true
c.Quotes = true
c.Ramblings = 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)
}
}