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
```
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
```bash

71
main.go
View File

@ -213,45 +213,54 @@ func takeBreak(minutes int) {
// Prints the current list of distractors to be blocked
// 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() {
// Open /etc/hf_distractors and store it as *os.File type
userDistractorsFileObj, err := os.Open(localFilePaths["uDistractors"])
if err != nil {
fmt.Println(err)
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)
}
defer func() {
if err := userDistractorsFileObj.Close(); err != nil {
fmt.Printf("Error closing file: %s\n", err)
if fileExists(localFilePaths["uDistractors"]) {
// Open /etc/hf_distractors and store it as *os.File type
userDistractorsFileObj, err := os.Open(localFilePaths["uDistractors"])
if err != nil {
fmt.Println(err)
}
}()
defer func() {
if err := userDistractorsFileObj.Close(); err != nil {
fmt.Printf("Error closing file: %s\n", err)
}
}()
// Initialize a new scanner, scan /etc/hf_distractors, and print to
// stdout line by line.
scanner := bufio.NewScanner(userDistractorsFileObj)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
// Open /etc/hf_predef_distractors and store it as *os.File type
predefDistractorsFileObj, err := os.Open(localFilePaths["pDistractors"])
if err != nil {
fmt.Println(err)
}
defer func() {
if err := predefDistractorsFileObj.Close(); err != nil {
fmt.Printf("Error closing file: %s\n", err)
// Initialize a new scanner, scan /etc/hf_distractors, and print to
// stdout line by line.
scanner := bufio.NewScanner(userDistractorsFileObj)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
// Initialize a new scanner, scan /etc/hf_predef_distractors, and print to
// stdout line by line.
scanner = bufio.NewScanner(predefDistractorsFileObj)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if fileExists(localFilePaths["pDistractors"]) {
// Open /etc/hf_predef_distractors and store it as *os.File type
predefDistractorsFileObj, err := os.Open(localFilePaths["pDistractors"])
if err != nil {
fmt.Println(err)
}
defer func() {
if err := predefDistractorsFileObj.Close(); err != nil {
fmt.Printf("Error closing file: %s\n", err)
}
}()
// Initialize a new scanner, scan /etc/hf_predef_distractors, and print to
// stdout line by line.
scanner := bufio.NewScanner(predefDistractorsFileObj)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
}
// Adds predefined distractors to /etc/hf_predef_distractors file