Progress++

This commit is contained in:
Jeffrey Serio 2021-12-23 22:12:05 -06:00
parent cc32666028
commit 5f39595e84
2 changed files with 152 additions and 167 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
# ---> Go # ---> Go
go-hyperfocus
# Binaries for programs and plugins # Binaries for programs and plugins
*.exe *.exe
*.exe~ *.exe~

308
main.go
View File

@ -2,11 +2,11 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"os/user" "os/user"
"runtime" "runtime"
@ -19,8 +19,23 @@ import (
var ( var (
marker string = "## MANAGED BY HYPERFOCUS ##\n" marker string = "## MANAGED BY HYPERFOCUS ##\n"
distractorsFile string = "/etc/hyperfocus_distractors" distractorsFile string = "/etc/hyperfocus_distractors"
predefDistractorsFile string = "/etc/hyperfocus_distractors_predefined"
) )
const resetDNSMsg string = `
Note that you may need to flush your system's DNS cache for changes to take effect.
In most cases this could be done by restarting the resolver daemon.
Windows systems: open PowerShell as administrator and run "ipconfig /flushdns"
macOS: open terminal and run "sudo dscacheutil -flushcache"
Linux with Systemd: open terminal and run "sudo systemctl restart systemd-resolved"
Other init systems or *BSD: You probably already know how to do this.`
func fileExists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
func getHostFile() string { func getHostFile() string {
var hostFile string var hostFile string
@ -35,70 +50,6 @@ func getHostFile() string {
return hostFile return hostFile
} }
func resetNetwork(message string) {
switch runtime.GOOS {
case "windows":
cmd := exec.Command("ipconfig /flushdns")
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
fmt.Println(message)
case "darwin":
cmd := exec.Command("dscacheutil", "-flushcache")
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
fmt.Println(message)
case "linux":
var (
usingDnsmasq bool = false
usingResolved bool = false
)
// Detect if using dnsmasq
out, err := exec.Command("systemctl", "is-active", "dnsmasq").Output()
if err != nil {
log.Fatalln(err)
}
if string(out) == "active" {
usingDnsmasq = true
}
// Detect if using systemd-resolved
out, err = exec.Command("systemctl", "is-active", "systemd-resolved").Output()
if err != nil {
log.Fatalln(err)
}
if string(out) == "active" {
usingResolved = true
}
// Restart networking service
if usingDnsmasq {
cmd := exec.Command("systemctl", "restart", "dnsmasq")
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
}
if usingResolved {
cmd := exec.Command("resolvectl", "flush-caches")
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
}
fmt.Println(message)
}
}
func isRoot() bool { func isRoot() bool {
currUser, err := user.Current() currUser, err := user.Current()
@ -118,29 +69,63 @@ func improveFocus() {
} }
defer hostFile.Close() defer hostFile.Close()
userDistractorsFile, err := os.Open(distractorsFile)
if err != nil {
log.Fatalln(err)
}
defer userDistractorsFile.Close()
if _, err := hostFile.WriteString(marker); err != nil { if _, err := hostFile.WriteString(marker); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
scanner := bufio.NewScanner(userDistractorsFile) var (
distractorsFileWarn bool
predefDistractorsFileWarn bool
)
if fileExists(distractorsFile) {
distractorsFileObj, err := os.Open(distractorsFile)
if err != nil {
log.Fatalln(err)
}
defer distractorsFileObj.Close()
scanner := bufio.NewScanner(distractorsFileObj)
for scanner.Scan() { for scanner.Scan() {
var hostLine string = fmt.Sprintf("127.0.0.1\t%s\n", scanner.Text()) var hostLine string = fmt.Sprintf("127.0.0.1\t%s\n", scanner.Text())
if _, err := hostFile.WriteString(hostLine); err != nil { if _, err := hostFile.WriteString(hostLine); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} }
} else {
distractorsFileWarn = true
}
if fileExists(predefDistractorsFile) {
predefDistractorsFileObj, err := os.Open(predefDistractorsFile)
if err != nil {
log.Fatalln(err)
}
defer predefDistractorsFileObj.Close()
scanner := bufio.NewScanner(predefDistractorsFileObj)
for scanner.Scan() {
var hostLine string = fmt.Sprintf("127.0.0.1\t%s\n", scanner.Text())
if _, err := hostFile.WriteString(hostLine); err != nil {
log.Fatalln(err)
}
}
} else {
predefDistractorsFileWarn = true
}
if distractorsFileWarn && predefDistractorsFileWarn {
log.Fatalln("Error: Please define a set of distractors in your distractors file, one per line. Alternatively, you can use a predefined set by running `hf predefined`.")
}
if _, err := hostFile.WriteString(marker); err != nil { if _, err := hostFile.WriteString(marker); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
resetNetwork("Focus is now improved 😊") fmt.Println("Focus is now improved 😊")
fmt.Println(resetDNSMsg)
} }
// Enables access to websites that are defined as 'distractors' // Enables access to websites that are defined as 'distractors'
@ -159,7 +144,7 @@ func loseFocus() {
log.Fatalln(err) log.Fatalln(err)
} }
resetNetwork("Focus is now lost 🤪") fmt.Println("Focus is now lost 🤪")
} }
// Enables temporarily breaking concentration // Enables temporarily breaking concentration
@ -207,111 +192,110 @@ func listDistractors() {
func addPredefinedDistractors() { func addPredefinedDistractors() {
distractors := []string{ distractors := []string{
"ycombinator.com", "anandtech.com",
"slashdot.com", "answerbag.com",
"facebook.com", "arstechnica.com",
"reddit.com", "awkwardfamilyphotos.com",
"gawker.com", "bbc.com",
"theverge.com", "befunky.com",
"techcrunch.com", "break.com",
"thenextweb.com",
"wired.com",
"gizmodo.com",
"slickdeals.net",
"mashable.com",
"digitaltrends.com",
"techradar.com",
"twitter.com",
"tumblr.com",
"technorati.com",
"digg.com",
"buzzfeed.com", "buzzfeed.com",
"twitter.com", "cheezburger.com",
"youtube.com", "cnet.com",
"netflix.com", "cnn.com",
"iwastesomuchtime.com", "cracked.com",
"pinterest.com", "digg.com",
"digitaltrends.com",
"distrowatch.com",
"ebay.com", "ebay.com",
"thepointsguy.com", "engadget.com",
"imgur.com", "everypoet.com",
"woot.com", "facebook.com",
"failblog.com",
"fark.com",
"firstpost.com",
"flyertalk.com", "flyertalk.com",
"fmylife.com",
"fox.com",
"freerice.com",
"gawker.com",
"geek.com",
"gizmag.com",
"gizmodo.com",
"gsmarena.com ",
"homestarrunner.com",
"hulu.com",
"imgur.com",
"imore.com",
"instagram.com", "instagram.com",
"iwastesomuchtime.com",
"lifehacker.com",
"liveplasma.com",
"lunchtimers.com",
"mashable.com",
"mcsweeneys.net",
"medium.com", "medium.com",
"meetup.com", "meetup.com",
"distrowatch.com",
"arstechnica.com",
"phoronix.com",
"arstechnica.com",
"failblog.com",
"redfin.com",
"realtor.com",
"zillow.com",
"trulia.com",
"cnn.com",
"fox.com",
"realclearpolitics.com",
"yelp.com",
"opentable.com",
"slashdot.org",
"xkcd.com",
"cnet.com",
"tomshardware.com",
"engadget.com",
"zdnet.com",
"techrepublic.com",
"gizmag.com",
"anandtech.com",
"imore.com",
"gsmarena.com ",
"geek.com",
"firstpost.com",
"wearables.com",
"stripgenerator.com",
"fmylife.com",
"liveplasma.com",
"cracked.com",
"befunky.com",
"pcworld.com",
"typepad.com",
"pogo.com",
"omegle.com",
"lifehacker.com",
"answerbag.com",
"cheezburger.com",
"fark.com",
"popurls.com",
"sho.com",
"hulu.com",
"myparentsjoinedfacebook.com",
"homestarrunner.com",
"petsinclothes.com",
"freerice.com",
"everypoet.com",
"mono-1.com", "mono-1.com",
"mcsweeneys.net", "myparentsjoinedfacebook.com",
"postsecret.com",
"textsfromlastnight.com",
"awkwardfamilyphotos.com",
"myspace.com", "myspace.com",
"lunchtimers.com", "netflix.com",
"twitterfall.com",
"break.com",
"passiveaggressivenotes.com",
"sciencemag.org",
"bbc.com",
"notalwaysright.com", "notalwaysright.com",
"omegle.com",
"opentable.com",
"passiveaggressivenotes.com",
"pcworld.com",
"petsinclothes.com",
"phoronix.com",
"pinterest.com",
"pogo.com",
"popurls.com",
"postsecret.com",
"realclearpolitics.com",
"realtor.com",
"reddit.com",
"redfin.com",
"sciencemag.org",
"sho.com",
"slashdot.com",
"slashdot.org",
"slickdeals.net",
"stripgenerator.com",
"techcrunch.com",
"technorati.com",
"techradar.com",
"techrepublic.com",
"textsfromlastnight.com",
"thenextweb.com",
"thepointsguy.com",
"theverge.com",
"tomshardware.com",
"trulia.com",
"tumblr.com",
"twitter.com",
"twitterfall.com",
"typepad.com",
"wearables.com",
"wired.com",
"woot.com",
"xkcd.com",
"ycombinator.com",
"yelp.com",
"youtube.com",
"zdnet.com",
"zillow.com",
} }
userDistractorsFile, err := os.OpenFile(distractorsFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) predefDistractorsFileObj, err := os.OpenFile(predefDistractorsFile, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer userDistractorsFile.Close() defer predefDistractorsFileObj.Close()
dataWriter := bufio.NewWriter(userDistractorsFile) dataWriter := bufio.NewWriter(predefDistractorsFileObj)
for _, v := range distractors { for _, v := range distractors {
_, _ = dataWriter.WriteString(v + "\n") _, _ = dataWriter.WriteString(v + "\n")
} }
dataWriter.Flush() dataWriter.Flush()