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

61
internal/api/fetch.go Normal file
View File

@@ -0,0 +1,61 @@
package api
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"github.com/mmcdole/gofeed"
)
func FetchPlain(url string) string {
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
return string(body)
}
func FetchJSON[T any](url string) (T, error) {
var result T
resp, err := http.Get(url)
if err != nil {
return result, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return result, fmt.Errorf("unexpected status: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return result, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return result, err
}
return result, nil
}
func FetchRSS(url string) (ret []gofeed.Item, err error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
for _, v := range feed.Items {
ret = append(ret, *v)
}
return
}

4
internal/config/conf.sh Normal file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
news=true
quote=true

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)
}
}

View File

@@ -0,0 +1,3 @@
#!/bin/sh
. $1
printf '{"news":%s,"quote":%s,"ramblings":%s,"ramblings_path":"%s"}\n' "$news" "$quote" "$ramblings" "$ramblings_path"