From 3c5e384952267546b58df69f700151368e37fec8 Mon Sep 17 00:00:00 2001 From: Nick Boughton Date: Thu, 10 Dec 2020 23:30:28 +0000 Subject: [PATCH] rewrote aur package check for my maintained npm pgs --- waybar/config | 43 +++-- waybar/modules/aur-vcheck/aur-vcheck.go | 85 --------- waybar/modules/aur-vcheck/config.json | 8 - waybar/modules/aur-vcheck/go.mod | 8 - waybar/modules/aur-vcheck/vcheck.service | 9 - waybar/modules/auroch/auroch.go | 168 ++++++++++++++++++ waybar/modules/auroch/auroch.service | 7 + .../vcheck.timer => auroch/auroch.timer} | 0 waybar/modules/auroch/go.mod | 8 + waybar/modules/{aur-vcheck => auroch}/go.sum | 8 +- waybar/style.css | 96 +++++----- 11 files changed, 261 insertions(+), 179 deletions(-) delete mode 100644 waybar/modules/aur-vcheck/aur-vcheck.go delete mode 100644 waybar/modules/aur-vcheck/config.json delete mode 100644 waybar/modules/aur-vcheck/go.mod delete mode 100644 waybar/modules/aur-vcheck/vcheck.service create mode 100644 waybar/modules/auroch/auroch.go create mode 100644 waybar/modules/auroch/auroch.service rename waybar/modules/{aur-vcheck/vcheck.timer => auroch/auroch.timer} (100%) create mode 100644 waybar/modules/auroch/go.mod rename waybar/modules/{aur-vcheck => auroch}/go.sum (56%) diff --git a/waybar/config b/waybar/config index 09f6bb1..2bd3a16 100644 --- a/waybar/config +++ b/waybar/config @@ -8,10 +8,10 @@ "margin-left": 5, "modules-left": [ "sway/workspaces", - "idle_inhibitor", "sway/mode", ], "modules-center": [ + "clock" ], "modules-right": [ "cpu", @@ -22,14 +22,14 @@ "disk#1", "disk#2", "custom/separator", - "pulseaudio", - "custom/separator", "backlight", "custom/separator", "battery", + "custom/separator", + "pulseaudio", "custom/pacman", - "custom/aurcheck", - "clock" + "custom/auroch", + "tray" ], "sway/workspaces": { "disable-scroll": true, @@ -48,6 +48,10 @@ "sway/mode": { "format": "{}" }, + "tray": { + "icon-size": 12, + "spacing": 10 + }, "backlight": { "format": "{icon} {percent}%", "format-icons": ["", ""], @@ -88,13 +92,6 @@ "interval": 1, "format": " {usage:2}%" }, - "idle_inhibitor": { - "format": "{icon}", - "format-icons": { - "activated": "", - "deactivated": "" - } - }, "battery": { "states": { "good": 95, @@ -126,15 +123,14 @@ "interval": 5, "exec": "cat ~/tmp/updates.json", "exec-if": "exit 0", - "on-click": "alacritty --class aptus-upgrade -e ~/.config/waybar/modules/updates/update-system.sh; systemctl --user start updates.service; pkill -RTMIN+8 waybar", - "signal": 8, + "on-click": "alacritty --class aptus-upgrade -e ~/.config/waybar/modules/updates/update-system.sh; systemctl --user start updates.service", "tooltip": true }, - "custom/aurcheck": { + "custom/auroch": { "format": " {}", "return-type": "json", - "exec": "cat ~/tmp/aur-vcheck.json", - "on-click": "systemctl --user start vcheck.service", + "exec": "cat ~/tmp/auroch.json", + "on-click": "systemctl --user start auroch.service", "interval": 5, "tooltip": true }, @@ -156,7 +152,7 @@ "margin-bottom": 5, "modules-left": ["custom/weather", "wlr/taskbar"], "modules-center": [], - "modules-right": ["tray", "custom/spotify"], + "modules-right": ["idle_inhibitor", "custom/spotify"], "wlr/taskbar": { "all-outputs": false, "format": "{icon} {title:.15}...", @@ -166,15 +162,18 @@ "on-middle-click": "close", "on-right-click": "minimize" }, - "tray": { - "icon-size": 12, - "spacing": 10 - }, "custom/weather": { "format": "{:.20}", "exec": "~/.config/waybar/modules/weather.sh", "interval": 3600 }, + "idle_inhibitor": { + "format": "{icon}", + "format-icons": { + "activated": "", + "deactivated": "" + } + }, "custom/spotify": { "interval": 1, "return-type": "json", diff --git a/waybar/modules/aur-vcheck/aur-vcheck.go b/waybar/modules/aur-vcheck/aur-vcheck.go deleted file mode 100644 index 7b54521..0000000 --- a/waybar/modules/aur-vcheck/aur-vcheck.go +++ /dev/null @@ -1,85 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "log" - "os" - "os/exec" - "strings" - - "github.com/nboughton/dotfiles/waybar/modules/gobar" - regex "github.com/nboughton/go-utils/regex/common" -) - -type config struct { - Versions []string `json:"versions,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)) - } - } - - n := len(out) - m := gobar.Module{ - Name: "AUR VERSION CHECK", - Summary: "AUR NPM Packages Out Of Date", - JSON: gobar.JSONOutput{ - Text: fmt.Sprintf("%d", n), - Alt: fmt.Sprintf("%d", n), - Tooltip: strings.Join(out, "\n"), - Class: "no-updates", - Percentage: n, - }, - } - - if n > 0 { - log.Println("Sending DBUS notification") - m.JSON.Class = "updates" - m.Notify(m.JSON.Tooltip, 10000) - } - - // Write output for waybar module - log.Println("Writing JSON output") - f, err = os.Create(outfile) - if err != nil { - log.Println(err) - return - } - defer f.Close() - m.JSON.Write(f) -} diff --git a/waybar/modules/aur-vcheck/config.json b/waybar/modules/aur-vcheck/config.json deleted file mode 100644 index 2eb8575..0000000 --- a/waybar/modules/aur-vcheck/config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "versions": [ - "@vue/cli@4.5.9", - "@vue/cli-service-global@4.5.9", - "@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 deleted file mode 100644 index ae611e8..0000000 --- a/waybar/modules/aur-vcheck/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module github.com/nboughton/dotfiles/waybar/modules/aur-vcheck - -go 1.15 - -require ( - github.com/nboughton/dotfiles/waybar/modules/gobar v0.0.0-20201209215918-92c9d95cf046 - github.com/nboughton/go-utils v0.0.0-20200108161841-5007e997f484 -) diff --git a/waybar/modules/aur-vcheck/vcheck.service b/waybar/modules/aur-vcheck/vcheck.service deleted file mode 100644 index 9538691..0000000 --- a/waybar/modules/aur-vcheck/vcheck.service +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Check for new upstream AUR versions of maintained NPM packages - -[Service] -Type=oneshot -WorkingDirectory=%h/.config/waybar/modules/aur-vcheck -Environment=DISPLAY=:0.0 -Environment=XAUTHORITY=%h/.Xauthority -ExecStart=go run %h/.config/waybar/modules/aur-vcheck/aur-vcheck.go %h/.config/waybar/modules/aur-vcheck/config.json \ No newline at end of file diff --git a/waybar/modules/auroch/auroch.go b/waybar/modules/auroch/auroch.go new file mode 100644 index 0000000..5dd9ff0 --- /dev/null +++ b/waybar/modules/auroch/auroch.go @@ -0,0 +1,168 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "regexp" + "sort" + "strings" + + "github.com/mikkeloscar/aur" + "github.com/nboughton/dotfiles/waybar/modules/gobar" +) + +const ( + npm = "npm" + github = "github" + errClass = "error" + npmRegistry = "https://registry.npmjs.org/" +) + +var outfile = fmt.Sprintf("%s/tmp/auroch.json", os.Getenv("HOME")) + +// Define relevant package data +type pkg struct { + AurName string `json:"aur_name,omitempty"` // Name on the AUR (i.e vue-cli) + AurVer string `json:"aur_ver,omitempty"` + UpstreamName string `json:"upstream_name,omitempty"` // Name on the upstream source (i.e @vue/cli) + UpstreamType string `json:"upstream_type,omitempty"` // Upstream type: npm|github + UpstreamVer string `json:"upstream_ver,omitempty"` +} + +// Just the bit of NPM registry info that I care about +type npmInfo struct { + Versions map[string]interface{} `json:"versions,omitempty"` +} + +// Retrieve the version of the current AUR package +func (p *pkg) getAurVer() error { + // Request pkg info + res, err := aur.Info([]string{p.AurName}) + if err != nil { + return err + } + + // Copy it to pkg struct + p.AurVer = res[0].Version + + // Strip revision + p.AurVer = regexp.MustCompile(`-\d$`).ReplaceAllString(p.AurVer, "") + + return nil +} + +func (p *pkg) getNpmVer() error { + // Request and decode the versions list + n := npmInfo{} + resp, err := http.Get(fmt.Sprintf("%s/%s", npmRegistry, p.UpstreamName)) + if err != nil { + return err + } + defer resp.Body.Close() + + if err = json.NewDecoder(resp.Body).Decode(&n); err != nil { + return err + } + + // Copy the keys into an array and sort it, last item should be most recent + v := []string{} + for key := range n.Versions { + v = append(v, key) + } + + sort.Strings(v) + p.UpstreamVer = v[len(v)-1] + + return nil +} + +func main() { + packages := []*pkg{ + { + AurName: "vue-cli", + UpstreamName: "@vue/cli", + UpstreamType: npm, + }, + { + AurName: "vue-cli-service-global", + UpstreamName: "@vue/cli-service-global", + UpstreamType: npm, + }, + { + AurName: "quasar-cli", + UpstreamName: "@quasar/cli", + UpstreamType: npm, + }, + { + AurName: "quasar-icongenie", + UpstreamName: "@quasar/icongenie", + UpstreamType: npm, + }, + } + + var ( + out []string + class string + err error + ) + + class = "no-updates" + for _, p := range packages { + log.Printf("Checking package: %s\n", p.AurName) + if err = p.getAurVer(); err != nil { + class = errClass + break + + } + + // I'll add a switch or if block here to retrieve ver data from non-npm sources when I have some to care about + if err = p.getNpmVer(); err != nil { + class = errClass + break + } + + log.Printf("%s %s -> %s\n", p.AurName, p.AurVer, p.UpstreamVer) + if p.AurVer != p.UpstreamVer { + out = append(out, fmt.Sprintf("%s %s -> %s", p.AurName, p.AurVer, p.UpstreamVer)) + } + } + + n := len(out) + + txt := fmt.Sprintf("%d", n) + if class == errClass { + txt = "!" + } + + if n > 0 { + class = "updates" + } + + m := gobar.Module{ + Name: "AUROCH", + Summary: "Outdated AUR Packages", + JSON: gobar.JSONOutput{ + Text: txt, + Alt: txt, + Class: class, + Tooltip: strings.Join(out, "\n"), + Percentage: n, + }, + } + + if n > 0 { + log.Println("Sending DBUS Notification") + m.Notify(m.JSON.Tooltip, 10000) + } + + log.Println("Writing JSON data") + f, err := os.Create(outfile) + if err != nil { + log.Fatalf("could not open %s for writing", outfile) + } + defer f.Close() + m.JSON.Write(f) +} diff --git a/waybar/modules/auroch/auroch.service b/waybar/modules/auroch/auroch.service new file mode 100644 index 0000000..95fb3e4 --- /dev/null +++ b/waybar/modules/auroch/auroch.service @@ -0,0 +1,7 @@ +[Unit] +Description=Check for new upstream AUR versions of maintained NPM packages + +[Service] +Type=oneshot +WorkingDirectory=%h/.config/waybar/modules/auroch +ExecStart=go run %h/.config/waybar/modules/auroch/auroch.go \ No newline at end of file diff --git a/waybar/modules/aur-vcheck/vcheck.timer b/waybar/modules/auroch/auroch.timer similarity index 100% rename from waybar/modules/aur-vcheck/vcheck.timer rename to waybar/modules/auroch/auroch.timer diff --git a/waybar/modules/auroch/go.mod b/waybar/modules/auroch/go.mod new file mode 100644 index 0000000..16be1d6 --- /dev/null +++ b/waybar/modules/auroch/go.mod @@ -0,0 +1,8 @@ +module github.com/nboughton/dotfiles/waybar/modules/auroch + +go 1.15 + +require ( + github.com/mikkeloscar/aur v0.0.0-20200113170522-1cb4e2949656 + github.com/nboughton/dotfiles/waybar/modules/gobar v0.0.0-20201210092537-0188f79629ba +) diff --git a/waybar/modules/aur-vcheck/go.sum b/waybar/modules/auroch/go.sum similarity index 56% rename from waybar/modules/aur-vcheck/go.sum rename to waybar/modules/auroch/go.sum index 631a715..5c66c38 100644 --- a/waybar/modules/aur-vcheck/go.sum +++ b/waybar/modules/auroch/go.sum @@ -2,7 +2,7 @@ github.com/esiqveland/notify v0.9.1 h1:hX6ZD3FCQJXI46AzUM/iWekcMfnZ9TPE4uIu9Hrn1 github.com/esiqveland/notify v0.9.1/go.mod h1:63UbVSaeJwF0LVJARHFuPgUAoM7o1BEvCZyknsuonBc= 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/dotfiles/waybar/modules/gobar v0.0.0-20201209215918-92c9d95cf046 h1:dRU4VhJuR2nyC8lWNwuFgWPxDEueb6pKENNZzUpur8c= -github.com/nboughton/dotfiles/waybar/modules/gobar v0.0.0-20201209215918-92c9d95cf046/go.mod h1:9MC4JW6muedO0QtIPqhZLM01B7ZjUGD6mDjdePRA1M0= -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= +github.com/mikkeloscar/aur v0.0.0-20200113170522-1cb4e2949656 h1:j679+jxcDkCFblYk+I+G71HQTFxM3PacYbVCiYmhRhU= +github.com/mikkeloscar/aur v0.0.0-20200113170522-1cb4e2949656/go.mod h1:nYOKcK8tIj69ZZ8uDOWoiT+L25NvlOQaraDqTec/idA= +github.com/nboughton/dotfiles/waybar/modules/gobar v0.0.0-20201210092537-0188f79629ba h1:UPeWYDsV9XBH0ItUpSNF9RZu5KuCe7u2ZsG50feC8lc= +github.com/nboughton/dotfiles/waybar/modules/gobar v0.0.0-20201210092537-0188f79629ba/go.mod h1:9MC4JW6muedO0QtIPqhZLM01B7ZjUGD6mDjdePRA1M0= diff --git a/waybar/style.css b/waybar/style.css index 19a75f1..eb9d6cd 100644 --- a/waybar/style.css +++ b/waybar/style.css @@ -13,6 +13,7 @@ window#waybar { font-family: "Symbols Nerd Font"; } +/* Base styling for modules */ #workspaces, #mode, #window, @@ -26,7 +27,7 @@ window#waybar { #backlight, #disk, #custom-pacman, -#custom-aurcheck, +#custom-auroch, #custom-swap, #custom-spotify, #custom-separator, @@ -37,6 +38,7 @@ window#waybar { border-radius: 0px; } +/* Separator */ #custom-separator { color: #d8dee9; background: none; @@ -44,10 +46,37 @@ window#waybar { opacity: 0.3; } -#mode { - background: rgba(191, 97, 106, 0.6); +/* TOP BAR (left)*/ +#workspaces { + padding: 0px; +} +#workspaces button { + padding: 0px 5px 0px 5px; + border-radius: 0px; +} +#workspaces button#sway-workspace-1 { + border-top-left-radius: 7px; + border-bottom-left-radius: 7px; +} +#workspaces button.focused { + background: rgba(94,129,172, 0.7); } +#idle_inhibitor { + margin: 0px; +} +#idle_inhibitor.activated { + background: rgba(163, 190, 140, 0.6); +} +#idle_inhibitor.deactivated { + background: rgba(208, 135, 112, 0.6); +} + +#mode { + background: rgba(191, 97, 106, 0.7); +} + +/* TOP BAR (right) */ #disk.1, #memory { padding-right: 3px; @@ -57,42 +86,27 @@ window#waybar { padding-left: 3px; } -#clock, -#custom-spotify { - background: rgba(94,129,172, 0.6); +#custom-pacman.no-updates, +#custom-auroch.no-updates { + background: rgba(163, 190, 140, 0.7); +} +#custom-pacman.updates, +#custom-auroch.updates { + background: rgba(180, 142, 173, 0.7); +} +#custom-pacman.error { + background: rgba(191, 97, 106, 0.7); +} + +#tray { + background: rgba(94,129,172, 0.7); border-top-right-radius: 7px; border-bottom-right-radius: 7px; } -#custom-pacman.no-updates, -#custom-aurcheck.no-updates { - background: rgba(163, 190, 140, 0.6); -} -#custom-pacman.updates, -#custom-aurcheck.updates { - background: rgba(180, 142, 173, 0.6); -} -#custom-pacman.error { - background: rgba(191, 97, 106, 0.6); -} - +/* BOTTOM BAR (left to right)*/ #custom-weather { - background: rgba(94,129,172, 0.6); - border-top-left-radius: 7px; - border-bottom-left-radius: 7px; -} - -#workspaces { - padding: 0px; -} -#workspaces button { - padding: 0px 5px 0px 5px; - border-radius: 0px; -} -#workspaces button.focused { - background: rgba(94,129,172, 0.6); -} -#workspaces button#sway-workspace-1 { + background: rgba(94,129,172, 0.7); border-top-left-radius: 7px; border-bottom-left-radius: 7px; } @@ -108,15 +122,11 @@ window#waybar { text-shadow: 1px 1px 1px #2e3440; } #taskbar button.active { - background: rgba(180, 142, 173, 0.6); + background: rgba(180, 142, 173, 0.7); } -#idle_inhibitor { - margin: 0px; -} -#idle_inhibitor.activated { - background: rgba(163, 190, 140, 0.6); -} -#idle_inhibitor.deactivated { - background: rgba(208, 135, 112, 0.6); +#custom-spotify { + background: rgba(94,129,172, 0.7); + border-top-right-radius: 7px; + border-bottom-right-radius: 7px; }