-
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.
refactor: update structure and improve performance
- Loading branch information
Tshaka Eric Lekholoane
committed
Dec 14, 2021
1 parent
ed20c9e
commit f940da4
Showing
12 changed files
with
346 additions
and
269 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,137 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"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/tshakalekholoane/bat#disclaimer.") | ||
os.Exit(1) | ||
} | ||
log.Fatal(err) | ||
} | ||
fmt.Println(s) | ||
} | ||
import "tshaka.co/bat/internal/cli" | ||
|
||
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\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) | ||
} | ||
cli.Run() | ||
} |
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/tshakalekholoane/bat | ||
module tshaka.co/bat | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Package cli handles the command line user interface for bat. | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"tshaka.co/bat/internal/docs" | ||
"tshaka.co/bat/internal/file" | ||
"tshaka.co/bat/internal/persist" | ||
"tshaka.co/bat/internal/threshold" | ||
) | ||
|
||
// common error messages | ||
const ( | ||
incompat = "This program is most likely not compatible with your system. " + | ||
"See\nhttps://github.com/tshakalekholoane/bat#disclaimer for details." | ||
permissionDenied = "Permission denied. Try running this command using sudo." | ||
) | ||
|
||
// errPermissionDenied indicates that the user has insufficient | ||
// permissions to perform an action. | ||
var errPermissionDenied = syscall.EACCES | ||
|
||
// context is a helper function that returns an io.Writer and a status | ||
// code the program should exit with after performing an action. | ||
func context(fatal bool) (io.Writer, int) { | ||
var out io.Writer = os.Stdout | ||
var code int | ||
if fatal { | ||
out = os.Stderr | ||
code = 1 | ||
} | ||
return out, code | ||
} | ||
|
||
// reportf formats according to a format specifier and writes to either | ||
// standard error or standard output depending on the context. | ||
func reportf(fatal bool, format string, a ...interface{}) { | ||
out, code := context(fatal) | ||
fmt.Fprintf(out, format, a...) | ||
os.Exit(code) | ||
} | ||
|
||
// reportln formats using the default formats for its operands, appends | ||
// a new line and, writes to standard output. | ||
func reportln(fatal bool, a ...interface{}) { | ||
reportf(fatal, "%v\n", a...) | ||
} | ||
|
||
// show prints the value of a /sys/class/power_supply/BAT?/ variable. | ||
func show(v string) { | ||
c, err := file.Contents(v) | ||
if err != nil { | ||
if errors.Is(err, file.ErrNotFound) { | ||
reportln(true, incompat) | ||
} | ||
log.Fatalln(err) | ||
} | ||
reportln(false, strings.TrimSpace(string(c))) | ||
} | ||
|
||
// Run executes the application. | ||
func Run() { | ||
if len(os.Args) == 1 { | ||
docs.Usage() | ||
os.Exit(0) | ||
} | ||
switch os.Args[1] { | ||
case "-c", "--capacity": | ||
show("capacity") | ||
case "-h", "--help": | ||
err := docs.Usage() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
os.Exit(0) | ||
case "-p", "--persist": | ||
err := persist.WriteServices() | ||
if err != nil { | ||
switch { | ||
case errors.Is(err, persist.ErrBashNotFound): | ||
reportln(true, "Cannot find Bash on your system.") | ||
case errors.Is(err, persist.ErrIncompatSystemd): | ||
reportln(true, "Requires systemd version 244-rc1 or later.") | ||
case errors.Is(err, file.ErrNotFound): | ||
reportln(true, incompat) | ||
case errors.Is(err, errPermissionDenied): | ||
reportln(true, permissionDenied) | ||
default: | ||
log.Fatalln(err) | ||
} | ||
} | ||
reportln(false, "Persistence of the current charging threshold enabled.") | ||
case "-r", "--reset": | ||
err := persist.DeleteServices() | ||
if err != nil { | ||
if errors.Is(err, errPermissionDenied) { | ||
reportln(true, permissionDenied) | ||
} | ||
log.Fatal(err) | ||
} | ||
reportln(false, "Charging threshold persistence reset.") | ||
case "-s", "--status": | ||
show("status") | ||
case "-t", "--threshold": | ||
switch { | ||
case len(os.Args) > 3: | ||
reportln(true, "Expects a single argument.") | ||
case len(os.Args) == 3: | ||
t, err := strconv.Atoi(os.Args[2]) | ||
if err != nil { | ||
if errors.Is(err, strconv.ErrSyntax) { | ||
reportln(true, "Argument should be an integer.") | ||
} | ||
log.Fatal(err) | ||
} | ||
if t < 1 || t > 100 { | ||
reportln(true, "Number should be between 1 and 100.") | ||
} | ||
err = threshold.Set(t) | ||
if err != nil { | ||
switch { | ||
case errors.Is(err, threshold.ErrIncompatKernel): | ||
reportln(true, "Requires Linux kernel version 5.4 or later.") | ||
case errors.Is(err, file.ErrNotFound): | ||
reportln(true, incompat) | ||
case errors.Is(err, errPermissionDenied): | ||
reportln(true, permissionDenied) | ||
default: | ||
log.Fatal(err) | ||
} | ||
} | ||
reportln( | ||
false, | ||
"Charging threshold set.\nUse `sudo bat --persist` to persist the "+ | ||
"setting between restarts.") | ||
default: | ||
show("charge_control_end_threshold") | ||
} | ||
case "-v", "--version": | ||
err := docs.Version() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
os.Exit(0) | ||
default: | ||
reportf( | ||
true, | ||
"There is no %s option. Run `bat --help` to see a list of available "+ | ||
"options.\n", | ||
os.Args[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
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.3 | ||
bat 0.8.4 | ||
Copyright (c) 2021 Tshaka Eric Lekholoane. | ||
MIT Licence. |
Oops, something went wrong.