Fix file exists bug in listDistractors

This commit is contained in:
Jeffrey Serio 2023-04-26 18:29:52 -05:00
parent 985e3d080b
commit d7d69b907a
2 changed files with 47 additions and 31 deletions

View File

@ -12,6 +12,13 @@ This is a Go implementation of the Python-written [concentration](https://github
go install codeberg.org/hyperreal/hyperfocus@latest go install codeberg.org/hyperreal/hyperfocus@latest
``` ```
Move the binary to a path where sudo can find it. E.g.:
``` bash
cd ~/go/bin
sudo cp -v hyperfocus /usr/local/bin
```
## Usage ## Usage
```bash ```bash

15
main.go
View File

@ -213,9 +213,16 @@ func takeBreak(minutes int) {
// Prints the current list of distractors to be blocked // Prints the current list of distractors to be blocked
// If neither /etc/hf_distractors nor /etc/hf_predef_distractors exist, // If neither /etc/hf_distractors nor /etc/hf_predef_distractors exist,
// print error message to console but do not exit. // print error message to console and exit.
func listDistractors() { func listDistractors() {
if !fileExists(localFilePaths["uDistractors"]) && !fileExists(localFilePaths["pDistractors"]) {
fmt.Printf("%s not found\n", localFilePaths["uDistractors"])
fmt.Printf("%s not found\n", localFilePaths["pDistractors"])
os.Exit(1)
}
if fileExists(localFilePaths["uDistractors"]) {
// Open /etc/hf_distractors and store it as *os.File type // Open /etc/hf_distractors and store it as *os.File type
userDistractorsFileObj, err := os.Open(localFilePaths["uDistractors"]) userDistractorsFileObj, err := os.Open(localFilePaths["uDistractors"])
if err != nil { if err != nil {
@ -233,7 +240,9 @@ func listDistractors() {
for scanner.Scan() { for scanner.Scan() {
fmt.Println(scanner.Text()) fmt.Println(scanner.Text())
} }
}
if fileExists(localFilePaths["pDistractors"]) {
// Open /etc/hf_predef_distractors and store it as *os.File type // Open /etc/hf_predef_distractors and store it as *os.File type
predefDistractorsFileObj, err := os.Open(localFilePaths["pDistractors"]) predefDistractorsFileObj, err := os.Open(localFilePaths["pDistractors"])
if err != nil { if err != nil {
@ -247,11 +256,11 @@ func listDistractors() {
// Initialize a new scanner, scan /etc/hf_predef_distractors, and print to // Initialize a new scanner, scan /etc/hf_predef_distractors, and print to
// stdout line by line. // stdout line by line.
scanner = bufio.NewScanner(predefDistractorsFileObj) scanner := bufio.NewScanner(predefDistractorsFileObj)
for scanner.Scan() { for scanner.Scan() {
fmt.Println(scanner.Text()) fmt.Println(scanner.Text())
} }
}
} }
// Adds predefined distractors to /etc/hf_predef_distractors file // Adds predefined distractors to /etc/hf_predef_distractors file