Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace dependency on Bash with /bin/sh for cross-platform compatibility #69

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion bat.service
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down