Skip to content

Commit

Permalink
fix: fix persistence issue on some distributions
Browse files Browse the repository at this point in the history
This fixes an issue, #12, on some
distributions where the Bash shell is not located in /usr/bin/ by
falling back to the one in /bin/ or returning an error otherwise.
  • Loading branch information
Tshaka Eric Lekholoane committed Jul 30, 2021
1 parent 62d47b3 commit d75dd0f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ There have also been some [problems setting the charging threshold inside of a v

## Installation

Precompiled binaries (Linux x86-64) are available from the [GitHub releases page](https://github.com/leveson/bat/releases), the latest of which can be downloaded from [here](https://github.com/leveson/bat/releases/download/0.8/bat).
Precompiled binaries (Linux x86-64) are available from the [GitHub releases page](https://github.com/leveson/bat/releases), the latest of which can be downloaded from [here](https://github.com/leveson/bat/releases/download/0.8.1/bat).

After downloading the binary, give it permission to execute on your system by running the following command. For example, assuming the binary is located in the user's Downloads folder:

Expand Down
3 changes: 3 additions & 0 deletions cmd/bat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func main() {
err := persist.WriteServices()
if err != nil {
switch {
case err.Error() == "bash not found":
fmt.Println("Requires Bash to persist the charging threshold.")
os.Exit(1)
case err.Error() == "incompatible systemd version":
fmt.Println("Requires systemd version 244-rc1 or later.")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion internal/docs/version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bat 0.8
bat 0.8.1
Copyright (c) 2021 Tshaka Eric Lekholoane.
MIT Licence.
32 changes: 29 additions & 3 deletions internal/persist/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ import (
//go:embed unit.tmpl
var unit string

// bashLocation returns the location of the Bash shell as a string. A
// successful call returns err == nil. It will return the first instance
// found starting by searching in /usr/bin/ and then in /bin/ as a last
// resort.
func bashLocation() (string, error) {
_, err := os.Stat("/usr/bin/bash")
if err != nil {
if os.IsNotExist(err) {
_, err = os.Stat("/bin/bash")
if err != nil {
if os.IsNotExist(err) {
return "", errors.New("bash not found")
}
return "", err
}
return "/bin/bash", nil
}
return "", err
}
return "/usr/bin/bash", nil
}

// hasRequiredSystemd returns true if the systemd version of the system
// in question is later than 244 and returns false otherwise. (systemd
// v244-rc1 is the earliest version to allow restarts for oneshot
Expand Down Expand Up @@ -85,14 +107,18 @@ func WriteServices() error {
if !ok {
return errors.New("incompatible systemd version")
}
shell, err := bashLocation()
if err != nil {
return err
}
threshold, err := io.FileContents("charge_control_end_threshold")
if err != nil {
return err
}
units := [3]Service{
{"boot", "multi-user", threshold},
{"hibernation", "hibernate", threshold},
{"sleep", "suspend", threshold},
{"boot", shell, "multi-user", threshold},
{"hibernation", shell, "hibernate", threshold},
{"sleep", shell, "suspend", threshold},
}
tmpl, err := template.New("unit").Parse(unit)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/persist/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package persist
// Service holds information for a systemd service.
type Service struct {
Event string
Shell string
Target string
Threshold string
}
2 changes: 1 addition & 1 deletion internal/persist/unit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=/usr/bin/bash -c 'echo {{ .Threshold }} > /sys/class/power_supply/BAT?/charge_control_end_threshold'
ExecStart={{ .Shell }} -c 'echo {{ .Threshold }} > /sys/class/power_supply/BAT?/charge_control_end_threshold'

[Install]
WantedBy={{ .Target }}.target

0 comments on commit d75dd0f

Please sign in to comment.