From b560c8f46c1e0761d406f41fb87547e6b533d0ae Mon Sep 17 00:00:00 2001 From: pepa65 Date: Sat, 30 Sep 2023 01:49:07 +0700 Subject: [PATCH] feat: replace dependency on Bash with /bin/sh for cross-platform compatibility This ensures cross-platform compatibility and accessibility for a wider range of users. Resolves: #68. --- 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..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, 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/) version 244 or later, which is bundled with most Linux distributions. diff --git a/bat.service b/bat.service index 99aeae5..bdcce71 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..8882cf7 100644 --- a/main_linux.go +++ b/main_linux.go @@ -11,6 +11,7 @@ import ( "log" "os" "os/exec" + "path" "path/filepath" "strconv" "strings" @@ -143,15 +144,17 @@ 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) } + path := path.Join(first, threshold) + for _, event := range events { tmpl, err := template.New("unit").Parse(unit) if err != nil { @@ -171,9 +174,10 @@ func main() { service := struct { Event string + Path string Shell string Threshold int - }{event, shell, current} + }{event, path, shell, current} if err := tmpl.Execute(f, service); err != nil { log.Fatal(err) }