Skip to content

Commit

Permalink
fix: add more sleep and hibernate targets
Browse files Browse the repository at this point in the history
Fixes #15.
  • Loading branch information
Tshaka Eric Lekholoane committed Aug 12, 2021
1 parent d75dd0f commit 70f305a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bat
!bat/
*.zip

# Binaries for programs and plugins
*.exe
Expand Down
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.1/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.2/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
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.1
bat 0.8.2
Copyright (c) 2021 Tshaka Eric Lekholoane.
MIT Licence.
28 changes: 18 additions & 10 deletions internal/persist/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import (
//go:embed unit.tmpl
var unit string

// units array contains prepopulated Service structs that are used by
// systemd to support threshold persistence between various suspend or
// hibernate states.
var units = [...]Service{
{Event: "boot", Target: "multi-user"},
{Event: "hibernation", Target: "hibernate"},
{Event: "hybridsleep", Target: "hybrid-sleep"},
{Event: "sleep", Target: "suspend"},
{Event: "suspendthenhibernate", Target: "suspend-then-hibernate"},
}

// 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
Expand All @@ -29,7 +40,7 @@ func bashLocation() (string, error) {
_, err = os.Stat("/bin/bash")
if err != nil {
if os.IsNotExist(err) {
return "", errors.New("bash not found")
return "", errors.New("bash not found")
}
return "", err
}
Expand Down Expand Up @@ -66,9 +77,9 @@ func hasRequiredSystemd() (bool, error) {
// level between restarts. If the call is successful, the return value
// is nil.
func RemoveServices() error {
for _, service := range [3]string{"boot", "hibernation", "sleep"} {
for _, service := range units {
err := os.Remove(
fmt.Sprintf("/etc/systemd/system/bat-%s.service", service))
fmt.Sprintf("/etc/systemd/system/bat-%s.service", service.Event))
if err != nil {
switch {
case strings.HasSuffix(err.Error(), "no such file or directory"):
Expand All @@ -80,14 +91,14 @@ func RemoveServices() error {
cmd := exec.Command(
"systemctl",
"disable",
fmt.Sprintf("bat-%s.service", service))
fmt.Sprintf("bat-%s.service", service.Event))
var stdErr bytes.Buffer
cmd.Stderr = &stdErr
err = cmd.Run()
if err != nil {
if !strings.HasSuffix(
strings.TrimSpace(stdErr.String()),
fmt.Sprintf("file bat-%s.service does not exist.", service)) {
fmt.Sprintf("bat-%s.service does not exist.", service.Event)) {
return err
}
}
Expand Down Expand Up @@ -115,16 +126,13 @@ func WriteServices() error {
if err != nil {
return err
}
units := [3]Service{
{"boot", shell, "multi-user", threshold},
{"hibernation", shell, "hibernate", threshold},
{"sleep", shell, "suspend", threshold},
}
tmpl, err := template.New("unit").Parse(unit)
if err != nil {
return err
}
for _, service := range units {
service.Shell = shell
service.Threshold = threshold
f, err := os.Create(
fmt.Sprintf("/etc/systemd/system/bat-%s.service", service.Event))
if err != nil {
Expand Down

0 comments on commit 70f305a

Please sign in to comment.