Same changes
This commit is contained in:
4
internal/config/conf.sh
Normal file
4
internal/config/conf.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
news=true
|
||||
quote=true
|
||||
70
internal/config/config.go
Normal file
70
internal/config/config.go
Normal 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)
|
||||
}
|
||||
}
|
||||
3
internal/config/wrapper.sh
Normal file
3
internal/config/wrapper.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
. $1
|
||||
printf '{"news":%s,"quote":%s,"ramblings":%s,"ramblings_path":"%s"}\n' "$news" "$quote" "$ramblings" "$ramblings_path"
|
||||
Reference in New Issue
Block a user