From 24da8e6983e2d95fc7acca7cdbd9e896076ba06a Mon Sep 17 00:00:00 2001 From: Nick Boughton Date: Wed, 9 Dec 2020 15:07:18 +0000 Subject: [PATCH] use systemd timers to run package check services and poll output in tmp --- sway/config | 4 +- waybar/config | 16 +++- waybar/modules/aur-vcheck/aur-vcheck.go | 117 +++++++++++++++++++++++ waybar/modules/aur-vcheck/config.json | 8 ++ waybar/modules/aur-vcheck/go.mod | 9 ++ waybar/modules/aur-vcheck/go.sum | 7 ++ waybar/modules/aur-vcheck/vcheck.service | 9 ++ waybar/modules/aur-vcheck/vcheck.timer | 9 ++ waybar/modules/updates/go.mod | 3 + waybar/modules/updates/update-check.go | 10 +- waybar/modules/updates/update-run.sh | 12 --- waybar/modules/updates/updates.service | 7 ++ waybar/modules/updates/updates.timer | 9 ++ waybar/style.css | 8 +- 14 files changed, 206 insertions(+), 22 deletions(-) create mode 100644 waybar/modules/aur-vcheck/aur-vcheck.go create mode 100644 waybar/modules/aur-vcheck/config.json create mode 100644 waybar/modules/aur-vcheck/go.mod create mode 100644 waybar/modules/aur-vcheck/go.sum create mode 100644 waybar/modules/aur-vcheck/vcheck.service create mode 100644 waybar/modules/aur-vcheck/vcheck.timer create mode 100644 waybar/modules/updates/go.mod delete mode 100755 waybar/modules/updates/update-run.sh create mode 100644 waybar/modules/updates/updates.service create mode 100644 waybar/modules/updates/updates.timer diff --git a/sway/config b/sway/config index e9e2ceb..1405d04 100644 --- a/sway/config +++ b/sway/config @@ -86,7 +86,7 @@ exec /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 exec mako exec nm-applet --indicator exec gammastep-indicator -#exec insync start +exec insync start ### Window config for_window [app_id=aptus-upgrade] floating enable @@ -98,7 +98,7 @@ for_window [app_id=ristretto] floating enable for_window [app_id=ranger] floating enable for_window [app_id=transmission-gtk] floating enable for_window [app_id=io.github.celluloid_player.Celluloid] floating enable -for_window [app_id=".*"] border pixel 2 +for_window [app_id=".*"] border pixel ### Key bindings # diff --git a/waybar/config b/waybar/config index 9cd095e..a69cb11 100644 --- a/waybar/config +++ b/waybar/config @@ -28,6 +28,7 @@ "custom/separator", "battery", "custom/pacman", + "custom/aurcheck", "clock" ], "sway/workspaces": { @@ -122,13 +123,20 @@ "custom/pacman": { "format": " {}", "return-type": "json", - "interval": 3600, // every hour - "exec": "go run ~/.config/waybar/modules/updates/update-check.go", - "exec-if": "~/.config/waybar/modules/updates/update-run.sh", - "on-click": "~/.config/waybar/modules/updates/update-system.sh; pkill -RTMIN+8 waybar", + "interval": 5, + "exec": "cat ~/tmp/updates.json", + "exec-if": "exit 0", + "on-click": "~/.config/waybar/modules/updates/update-system.sh; systemctl --user restart updates.timer; pkill -RTMIN+8 waybar", "signal": 8, "tooltip": true }, + "custom/aurcheck": { + "format": " {}", + "return-type": "json", + "exec": "cat ~/tmp/aur-vcheck.json", + "interval": 5, + "tooltip": true + }, "custom/swap": { "format": " {}", "interval": 5, diff --git a/waybar/modules/aur-vcheck/aur-vcheck.go b/waybar/modules/aur-vcheck/aur-vcheck.go new file mode 100644 index 0000000..8ed8415 --- /dev/null +++ b/waybar/modules/aur-vcheck/aur-vcheck.go @@ -0,0 +1,117 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "os" + "os/exec" + "strings" + + "github.com/esiqveland/notify" + "github.com/godbus/dbus/v5" + regex "github.com/nboughton/go-utils/regex/common" +) + +type config struct { + Versions []string `json:"versions,omitempty"` +} + +type jsonOutput struct { + Text string `json:"text,omitempty"` + Alt string `json:"alt,omitempty"` + Tooltip string `json:"tooltip,omitempty"` + Class string `json:"class,omitempty"` + Percentage int `json:"percentage,omitempty"` +} + +var outfile = fmt.Sprintf("%s/tmp/aur-vcheck.json", os.Getenv(("HOME"))) + +func main() { + confPath := "config.json" + // Check for config path + if len(os.Args) > 1 { + confPath = os.Args[1] + } + + // Read config + log.Println("Reading config") + f, err := os.Open(confPath) + if err != nil { + log.Fatalf("cannot open config: %s", err) + } + + c := config{} + if err := json.NewDecoder(f).Decode(&c); err != nil { + log.Fatalf("failed to decode config: %s", err) + } + f.Close() + + // Check Versions + log.Println("Checking versions") + out := []string{} + for _, appVer := range c.Versions { + app := fmt.Sprintf("@%s", strings.Split(appVer, "@")[1]) + o, err := exec.Command("npm", "info", app).CombinedOutput() + if err != nil { + log.Fatalf("npm exec failed: %s", err) + } + + v := regex.ANSI.ReplaceAllString(strings.Fields(strings.Split(string(o), "\n")[1])[0], "") + if appVer != v { + out = append(out, fmt.Sprintf("%s -> %s", appVer, v)) + } + } + + if len(out) > 0 { + log.Println("Connecting to DBUS") + conn, err := dbus.SessionBusPrivate() + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + if err = conn.Auth(nil); err != nil { + log.Fatal(err) + } + + if err = conn.Hello(); err != nil { + log.Fatal(err) + } + + // Send notification + log.Println("Sending notification") + notify.SendNotification(conn, notify.Notification{ + AppName: "AUR VCHECK", + ReplacesID: uint32(0), + AppIcon: "mail-message-new", + Summary: "AUR NPM Packages Need Updating", + Body: strings.Join(out, "\n"), + Hints: map[string]dbus.Variant{}, + ExpireTimeout: 10000, + }) + } + + // Write output for waybar module + n := len(out) + o := jsonOutput{ + Text: fmt.Sprintf("%d", n), + Alt: fmt.Sprintf("%d", n), + Tooltip: strings.Join(out, "\n"), + } + if n > 0 { + o.Class = "updates" + o.Percentage = n + } else { + o.Class = "no-updates" + o.Percentage = 0 + } + + f, err = os.Create(outfile) + if err != nil { + log.Println(err) + return + } + json.NewEncoder(f).Encode(o) + f.Close() +} diff --git a/waybar/modules/aur-vcheck/config.json b/waybar/modules/aur-vcheck/config.json new file mode 100644 index 0000000..0584ab9 --- /dev/null +++ b/waybar/modules/aur-vcheck/config.json @@ -0,0 +1,8 @@ +{ + "versions": [ + "@vue/cli@4.5.8", + "@vue/cli-service-global@4.5.8", + "@quasar/cli@1.1.2", + "@quasar/icongenie@2.3.3" + ] +} diff --git a/waybar/modules/aur-vcheck/go.mod b/waybar/modules/aur-vcheck/go.mod new file mode 100644 index 0000000..8e1f7de --- /dev/null +++ b/waybar/modules/aur-vcheck/go.mod @@ -0,0 +1,9 @@ +module github.com/nboughton/dotfiles/waybar/modules/aur-vcheck + +go 1.15 + +require ( + github.com/esiqveland/notify v0.9.1 + github.com/godbus/dbus/v5 v5.0.3 + github.com/nboughton/go-utils v0.0.0-20200108161841-5007e997f484 +) diff --git a/waybar/modules/aur-vcheck/go.sum b/waybar/modules/aur-vcheck/go.sum new file mode 100644 index 0000000..10aff0a --- /dev/null +++ b/waybar/modules/aur-vcheck/go.sum @@ -0,0 +1,7 @@ +github.com/esiqveland/notify v0.9.1 h1:hX6ZD3FCQJXI46AzUM/iWekcMfnZ9TPE4uIu9Hrn1D4= +github.com/esiqveland/notify v0.9.1/go.mod h1:63UbVSaeJwF0LVJARHFuPgUAoM7o1BEvCZyknsuonBc= +github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4= +github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME= +github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/nboughton/go-utils v0.0.0-20200108161841-5007e997f484 h1:mqED7SC5umrSOUC4eUleOuHUsrwvbVl/DQy+jVHFlvo= +github.com/nboughton/go-utils v0.0.0-20200108161841-5007e997f484/go.mod h1:iSex0rTt7j+NRoVAEwO0ADC0jkx53W/3OsbYgNMxSWU= diff --git a/waybar/modules/aur-vcheck/vcheck.service b/waybar/modules/aur-vcheck/vcheck.service new file mode 100644 index 0000000..215c9d9 --- /dev/null +++ b/waybar/modules/aur-vcheck/vcheck.service @@ -0,0 +1,9 @@ +[Unit] +Description=Check for new upstream AUR versions of maintained NPM packages + +[Service] +User=nick +Type=oneshot +Environment=DISPLAY=:0.0 +Environment=XAUTHORITY=/home/nick/.Xauthority +ExecStart=go run /home/nick/.config/waybar/modules/aur-vcheck/aur-vcheck.go /home/nick/.config/waybar/modules/aur-vcheck/config.json \ No newline at end of file diff --git a/waybar/modules/aur-vcheck/vcheck.timer b/waybar/modules/aur-vcheck/vcheck.timer new file mode 100644 index 0000000..10f1c4a --- /dev/null +++ b/waybar/modules/aur-vcheck/vcheck.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Check for new upstream AUR versions of NPM packages + +[Timer] +OnBootSec=15min +OnUnitActiveSec=1h + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/waybar/modules/updates/go.mod b/waybar/modules/updates/go.mod new file mode 100644 index 0000000..d603a99 --- /dev/null +++ b/waybar/modules/updates/go.mod @@ -0,0 +1,3 @@ +module github.com/nboughton/dotfiles/waybar/modules/updates + +go 1.15 diff --git a/waybar/modules/updates/update-check.go b/waybar/modules/updates/update-check.go index f633f29..8ec269e 100644 --- a/waybar/modules/updates/update-check.go +++ b/waybar/modules/updates/update-check.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "log" "os" "os/exec" "strings" @@ -16,6 +17,8 @@ type jsonOutput struct { Percentage int `json:"percentage,omitempty"` } +var outfile = fmt.Sprintf("%s/tmp/updates.json", os.Getenv(("HOME"))) + func main() { o := jsonOutput{} @@ -52,7 +55,12 @@ func main() { o.Percentage = n } - json.NewEncoder(os.Stdout).Encode(o) + f, err := os.Create(outfile) + if err != nil { + log.Println(err) + } + json.NewEncoder(f).Encode(o) + f.Close() } func removeEmptyLines(list []string) []string { diff --git a/waybar/modules/updates/update-run.sh b/waybar/modules/updates/update-run.sh deleted file mode 100755 index 8eb2df5..0000000 --- a/waybar/modules/updates/update-run.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -pgrep checkupdates -pac=$? - -pgrep yay -aur=$? - -if [[ pac -eq 1 ]] && [[ aur -eq 1 ]]; then - exit 0 -fi - -exit 1 \ No newline at end of file diff --git a/waybar/modules/updates/updates.service b/waybar/modules/updates/updates.service new file mode 100644 index 0000000..14ee621 --- /dev/null +++ b/waybar/modules/updates/updates.service @@ -0,0 +1,7 @@ +[Unit] +Description=Check for Pacman and AUR/git package updates + +[Service] +User=nick +Type=oneshot +ExecStart=go run /home/nick/.config/waybar/modules/updates/update-check.go \ No newline at end of file diff --git a/waybar/modules/updates/updates.timer b/waybar/modules/updates/updates.timer new file mode 100644 index 0000000..3bf6619 --- /dev/null +++ b/waybar/modules/updates/updates.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Check for Pacman/AUR/git package updates + +[Timer] +OnBootSec=15min +OnUnitActiveSec=1h + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/waybar/style.css b/waybar/style.css index 08f32b1..b782842 100644 --- a/waybar/style.css +++ b/waybar/style.css @@ -26,7 +26,7 @@ window#waybar { #backlight, #disk, #custom-pacman, -#custom-aur, +#custom-aurcheck, #custom-swap, #custom-spotify, #custom-separator, @@ -64,10 +64,12 @@ window#waybar { border-bottom-right-radius: 7px; } -#custom-pacman.no-updates { +#custom-pacman.no-updates, +#custom-aurcheck.no-updates { background: rgba(163, 190, 140, 0.75); } -#custom-pacman.updates { +#custom-pacman.updates, +#custom-aurcheck.updates { background: rgba(180, 142, 173, 0.75); } #custom-pacman.error {