From 7800717abc7e29fdd514a0d9638dd528562130b1 Mon Sep 17 00:00:00 2001 From: pepa65 Date: Mon, 25 Sep 2023 15:51:50 +0000 Subject: [PATCH 1/8] Use sh instead of bash --- README.md | 2 +- bat.service | 2 +- main_linux.go | 10 +++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 485a831..fdca707 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,4 @@ sudo bat persist Linux kernel version later than 5.4-rc1 which is the [earliest version to expose the battery charging threshold variable](https://github.com/torvalds/linux/commit/7973353e92ee1e7ca3b2eb361a4b7cb66c92abee). -To persist the threshold setting between restarts, the application relies on [systemd](https://systemd.io/), particularly a version later than 244, and [Bash](https://www.gnu.org/software/bash/) which are bundled with most Linux distributions. +To persist the threshold setting between restarts, the application relies on [systemd](https://systemd.io/), particularly a version later than 244, and a POSIX shell `sh`, which are bundled with most Linux distributions. diff --git a/bat.service b/bat.service index 99aeae5..f9cdf9e 100644 --- a/bat.service +++ b/bat.service @@ -5,7 +5,7 @@ StartLimitBurst=0 [Service] Type=oneshot -ExecStart={{.Shell}} -c 'echo {{.Threshold}} > /sys/class/power_supply/BAT?/charge_control_end_threshold' +ExecStart={{.Shell}} -c 'echo {{.Threshold}} >{{.Path}}}}' Restart=on-failure RemainAfterExit=true diff --git a/main_linux.go b/main_linux.go index 9d54137..c46e07a 100644 --- a/main_linux.go +++ b/main_linux.go @@ -69,6 +69,9 @@ func main() { os.Exit(1) } first := batteries[0] + if len(batteries) > 1 { + fmt.Fprintln(os.Stderr, "More than 1 battery device found, using " + first) + } data := make([]byte, 32) mustRead := func(variable string) string { f, err := os.Open(filepath.Join(first, variable)) @@ -143,10 +146,10 @@ func main() { log.Fatal(err) } - shell, err := exec.LookPath("bash") + shell, err := exec.LookPath("sh") if err != nil { if errors.Is(err, exec.ErrNotFound) { - fmt.Fprintln(os.Stderr, "Could not find Bash on your system.") + fmt.Fprintln(os.Stderr, "Could not find 'sh' on your system.") os.Exit(1) } log.Fatal(err) @@ -173,7 +176,8 @@ func main() { Event string Shell string Threshold int - }{event, shell, current} + Path string + }{event, shell, current, first} if err := tmpl.Execute(f, service); err != nil { log.Fatal(err) } From 0b6223781bc1cd381d4e37be36ae225c10a99e49 Mon Sep 17 00:00:00 2001 From: pepa65 Date: Mon, 25 Sep 2023 15:55:18 +0000 Subject: [PATCH 2/8] Edit --- bat.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bat.service b/bat.service index f9cdf9e..7b9c493 100644 --- a/bat.service +++ b/bat.service @@ -5,7 +5,7 @@ StartLimitBurst=0 [Service] Type=oneshot -ExecStart={{.Shell}} -c 'echo {{.Threshold}} >{{.Path}}}}' +ExecStart={{.Shell}} -c 'echo {{.Threshold}} >{{.Path}}' Restart=on-failure RemainAfterExit=true From c6110b3d869d7af4c32c802f9ceb8883c4d87dc8 Mon Sep 17 00:00:00 2001 From: pepa65 Date: Thu, 28 Sep 2023 22:41:03 +0700 Subject: [PATCH 3/8] Update bat.service --- bat.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bat.service b/bat.service index 7b9c493..bdcce71 100644 --- a/bat.service +++ b/bat.service @@ -5,7 +5,7 @@ StartLimitBurst=0 [Service] Type=oneshot -ExecStart={{.Shell}} -c 'echo {{.Threshold}} >{{.Path}}' +ExecStart={{.Shell}} -c 'echo {{.Threshold}} > {{.Path}}' Restart=on-failure RemainAfterExit=true From 88c9dc4bb9101f89612b8e676a6a232ee5d1543a Mon Sep 17 00:00:00 2001 From: Tshaka Lekholoane Date: Fri, 29 Sep 2023 20:13:05 +0200 Subject: [PATCH 4/8] refactor: remove superficial message --- main_linux.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/main_linux.go b/main_linux.go index c46e07a..034763e 100644 --- a/main_linux.go +++ b/main_linux.go @@ -69,9 +69,6 @@ func main() { os.Exit(1) } first := batteries[0] - if len(batteries) > 1 { - fmt.Fprintln(os.Stderr, "More than 1 battery device found, using " + first) - } data := make([]byte, 32) mustRead := func(variable string) string { f, err := os.Open(filepath.Join(first, variable)) From 2f80eadb609dfd05344ea2e066e8b8f53ace0f7a Mon Sep 17 00:00:00 2001 From: Tshaka Lekholoane Date: Fri, 29 Sep 2023 20:15:02 +0200 Subject: [PATCH 5/8] refactor: use optimal order in service struct --- main_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main_linux.go b/main_linux.go index 034763e..c714e51 100644 --- a/main_linux.go +++ b/main_linux.go @@ -171,10 +171,10 @@ func main() { service := struct { Event string + Path string Shell string Threshold int - Path string - }{event, shell, current, first} + }{event, first, shell, current} if err := tmpl.Execute(f, service); err != nil { log.Fatal(err) } From b57e3b0c0afc996a40c9cd84f5c5dcf3216715db Mon Sep 17 00:00:00 2001 From: Tshaka Lekholoane Date: Fri, 29 Sep 2023 20:17:52 +0200 Subject: [PATCH 6/8] docs: assume POSIX shell exists on every Linux distribution --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fdca707..d6e6170 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,4 @@ sudo bat persist Linux kernel version later than 5.4-rc1 which is the [earliest version to expose the battery charging threshold variable](https://github.com/torvalds/linux/commit/7973353e92ee1e7ca3b2eb361a4b7cb66c92abee). -To persist the threshold setting between restarts, the application relies on [systemd](https://systemd.io/), particularly a version later than 244, and a POSIX shell `sh`, which are bundled with most Linux distributions. +To persist the threshold setting between restarts, the application relies on [systemd](https://systemd.io/), particularly a version later than 244 which is bundled with most Linux distributions. From 59efc74981675b71de79ef00d5db95c721d0169c Mon Sep 17 00:00:00 2001 From: Tshaka Lekholoane Date: Fri, 29 Sep 2023 20:21:30 +0200 Subject: [PATCH 7/8] docs: clarify dependency requirement --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d6e6170..466cf8b 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,4 @@ sudo bat persist Linux kernel version later than 5.4-rc1 which is the [earliest version to expose the battery charging threshold variable](https://github.com/torvalds/linux/commit/7973353e92ee1e7ca3b2eb361a4b7cb66c92abee). -To persist the threshold setting between restarts, the application relies on [systemd](https://systemd.io/), particularly a version later than 244 which is bundled with most Linux distributions. +To persist the threshold setting between restarts, the application relies on [systemd](https://systemd.io/) version 244 or later, which is bundled with most Linux distributions. From 43df3a9cb5ce37b84dc62034e63f22ea33c4490a Mon Sep 17 00:00:00 2001 From: Tshaka Lekholoane Date: Fri, 29 Sep 2023 20:41:18 +0200 Subject: [PATCH 8/8] fix: specify the correct charging threshold path --- main_linux.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main_linux.go b/main_linux.go index c714e51..8882cf7 100644 --- a/main_linux.go +++ b/main_linux.go @@ -11,6 +11,7 @@ import ( "log" "os" "os/exec" + "path" "path/filepath" "strconv" "strings" @@ -152,6 +153,8 @@ func main() { log.Fatal(err) } + path := path.Join(first, threshold) + for _, event := range events { tmpl, err := template.New("unit").Parse(unit) if err != nil { @@ -174,7 +177,7 @@ func main() { Path string Shell string Threshold int - }{event, first, shell, current} + }{event, path, shell, current} if err := tmpl.Execute(f, service); err != nil { log.Fatal(err) }