-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tshaka Eric Lekholoane
committed
Sep 9, 2021
1 parent
0e22a62
commit 7f1c61c
Showing
12 changed files
with
418 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,137 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/leveson/bat/internal/docs" | ||
"github.com/leveson/bat/internal/io" | ||
"github.com/leveson/bat/internal/persist" | ||
"github.com/leveson/bat/internal/threshold" | ||
"github.com/tshakalekholoane/bat/internal/docs" | ||
"github.com/tshakalekholoane/bat/internal/io" | ||
"github.com/tshakalekholoane/bat/internal/persist" | ||
"github.com/tshakalekholoane/bat/internal/threshold" | ||
) | ||
|
||
// printFile is a wrapper around `io.FileContents` to simplify printing | ||
// the values of some (battery) virtual files. | ||
func printFile(vf string) { | ||
s, err := io.FileContents(vf) | ||
if err != nil { | ||
if err.Error() == "virtual file not found" { | ||
fmt.Println( | ||
"This program is most likely not compatible with your " + | ||
"system. See\nhttps://github.com/leveson/bat#disclaimer.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
fmt.Println(s) | ||
s, err := io.FileContents(vf) | ||
if err != nil { | ||
if err.Error() == "virtual file not found" { | ||
fmt.Println( | ||
"This program is most likely not compatible with your system." + | ||
" See\nhttps://github.com/tshakalekholoane/bat#disclaimer.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
fmt.Println(s) | ||
} | ||
|
||
func main() { | ||
if len(os.Args) == 1 { | ||
docs.Help() | ||
os.Exit(0) | ||
} | ||
switch os.Args[1] { | ||
case "-c", "--capacity": | ||
printFile("capacity") | ||
case "-h", "--help": | ||
err := docs.Help() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
case "-p", "--persist": | ||
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) | ||
case err.Error() == "virtual file not found": | ||
fmt.Println( | ||
"This program is most likely not compatible with your " + | ||
"system. See\nhttps://github.com/leveson/bat#disclaimer.") | ||
os.Exit(1) | ||
case strings.HasSuffix(err.Error(), "permission denied"): | ||
fmt.Println("This command requires sudo permissions.") | ||
os.Exit(1) | ||
default: | ||
log.Fatal(err) | ||
} | ||
} | ||
fmt.Println("Persistence of the current charging threshold enabled.") | ||
case "-r", "--reset": | ||
err := persist.RemoveServices() | ||
if err != nil { | ||
if strings.HasSuffix(err.Error(), "permission denied") { | ||
fmt.Println("This command requires sudo permissions.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Charging threshold persistence reset.") | ||
case "-s", "--status": | ||
printFile("status") | ||
case "-t", "--threshold": | ||
switch { | ||
case len(os.Args) > 3: | ||
fmt.Println("Expects a single argument.") | ||
os.Exit(1) | ||
case len(os.Args) == 3: | ||
t, err := strconv.Atoi(os.Args[2]) | ||
if err != nil { | ||
if errors.Is(err, strconv.ErrSyntax) { | ||
fmt.Println("Argument should be an integer.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
if t < 1 || t > 100 { | ||
fmt.Println("Number should be between 1 and 100.") | ||
os.Exit(1) | ||
} | ||
err = threshold.Write(t) | ||
if err != nil { | ||
switch { | ||
case err.Error() == "incompatible kernel version": | ||
fmt.Println("Requires Linux kernel version 5.4 or later.") | ||
os.Exit(1) | ||
case err.Error() == "virtual file not found": | ||
fmt.Println( | ||
"This program is most likely not compatible with " + | ||
"your system. See\n" + | ||
"https://github.com/leveson/bat#disclaimer.") | ||
os.Exit(1) | ||
case strings.HasSuffix(err.Error(), "permission denied"): | ||
fmt.Println("This command requires sudo permissions.") | ||
os.Exit(1) | ||
default: | ||
log.Fatal(err) | ||
} | ||
} | ||
fmt.Println( | ||
"Charging threshold set.\nUse `sudo bat --persist` to " + | ||
"persist the setting between restarts.") | ||
default: | ||
printFile("charge_control_end_threshold") | ||
} | ||
case "-v", "--version": | ||
err := docs.VersionInfo() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
default: | ||
fmt.Printf( | ||
"There is no %s option. Use `bat --help` to see a list of " + | ||
"available options.\n", | ||
os.Args[1]) | ||
os.Exit(1) | ||
} | ||
if len(os.Args) == 1 { | ||
docs.Help() | ||
os.Exit(0) | ||
} | ||
switch os.Args[1] { | ||
case "-c", "--capacity": | ||
printFile("capacity") | ||
case "-h", "--help": | ||
err := docs.Help() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
case "-p", "--persist": | ||
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) | ||
case err.Error() == "virtual file not found": | ||
fmt.Println( | ||
"This program is most likely not compatible with your " + | ||
"system. See\n" + | ||
"https://github.com/tshakalekholoane/bat#disclaimer.") | ||
os.Exit(1) | ||
case strings.HasSuffix(err.Error(), "permission denied"): | ||
fmt.Println("This command requires sudo permissions.") | ||
os.Exit(1) | ||
default: | ||
log.Fatal(err) | ||
} | ||
} | ||
fmt.Println("Persistence of the current charging threshold enabled.") | ||
case "-r", "--reset": | ||
err := persist.RemoveServices() | ||
if err != nil { | ||
if strings.HasSuffix(err.Error(), "permission denied") { | ||
fmt.Println("This command requires sudo permissions.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Charging threshold persistence reset.") | ||
case "-s", "--status": | ||
printFile("status") | ||
case "-t", "--threshold": | ||
switch { | ||
case len(os.Args) > 3: | ||
fmt.Println("Expects a single argument.") | ||
os.Exit(1) | ||
case len(os.Args) == 3: | ||
t, err := strconv.Atoi(os.Args[2]) | ||
if err != nil { | ||
if errors.Is(err, strconv.ErrSyntax) { | ||
fmt.Println("Argument should be an integer.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
if t < 1 || t > 100 { | ||
fmt.Println("Number should be between 1 and 100.") | ||
os.Exit(1) | ||
} | ||
err = threshold.Write(t) | ||
if err != nil { | ||
switch { | ||
case err.Error() == "incompatible kernel version": | ||
fmt.Println("Requires Linux kernel version 5.4 or later.") | ||
os.Exit(1) | ||
case err.Error() == "virtual file not found": | ||
fmt.Println( | ||
"This program is most likely not compatible with your" + | ||
" system. See\n" + | ||
"https://github.com/tshakalekholoane/bat#disclaimer.") | ||
os.Exit(1) | ||
case strings.HasSuffix(err.Error(), "permission denied"): | ||
fmt.Println("This command requires sudo permissions.") | ||
os.Exit(1) | ||
default: | ||
log.Fatal(err) | ||
} | ||
} | ||
fmt.Println( | ||
"Charging threshold set.\nUse `sudo bat --persist` to " + | ||
"persist the setting between restarts.") | ||
default: | ||
printFile("charge_control_end_threshold") | ||
} | ||
case "-v", "--version": | ||
err := docs.VersionInfo() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
default: | ||
fmt.Printf( | ||
"There is no %s option. Use `bat --help` to see a list of " + | ||
"available options.\n", | ||
os.Args[1]) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/leveson/bat | ||
module github.com/tshakalekholoane/bat | ||
|
||
go 1.16 | ||
go 1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
bat 0.8.2 | ||
bat 0.8.3 | ||
Copyright (c) 2021 Tshaka Eric Lekholoane. | ||
MIT Licence. |
Oops, something went wrong.