Skip to content

Commit

Permalink
fix: communicate missing charge threshold setting
Browse files Browse the repository at this point in the history
Return a user-friendly message when the charging threshold setting does
not exist instead of panicking.
  • Loading branch information
tshakalekholoane committed Mar 5, 2024
1 parent ab475ca commit bd7a4d5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ var (
version string
)

func exists(battery, variable string) bool {
_, err := os.Stat(filepath.Join(battery, variable))
if err != nil && errors.Is(err, fs.ErrNotExist) {
return false
}
return true
}

func must[T any](v T, err error) T {
if err != nil {
panic(err)
Expand Down Expand Up @@ -146,6 +154,10 @@ func main() {
w := must(strconv.Atoi(must(read(bat, y))))
fmt.Println(v * 100 / w)
case "persist":
if !exists(bat, threshold) {
fmt.Fprintf(os.Stderr, "Charging threshold setting not found.\n")
os.Exit(1)
}
// systemd 244-rc1 is the earliest version to allow restarts for
// oneshot services.
out := must(exec.Command("systemctl", "--version").CombinedOutput())
Expand Down Expand Up @@ -191,7 +203,11 @@ func main() {
}
fmt.Println("Persistence of the current charging threshold enabled.")
case "threshold":
if len(os.Args) < 3 {
if !exists(bat, threshold) {
fmt.Fprintf(os.Stderr, "Charging threshold setting not found.\n")
os.Exit(1)
}
if len(os.Args) == 2 || len(os.Args) == 3 && opts.debug {
// Get.
fmt.Println(must(read(bat, threshold)))
} else {
Expand Down

0 comments on commit bd7a4d5

Please sign in to comment.