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-hyperfocus
# Binaries for programs and plugins
*.exe
*.exe~

318
main.go
View File

@ -2,11 +2,11 @@ package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"os/user"
"runtime"
@ -17,10 +17,25 @@ import (
)
var (
marker string = "## MANAGED BY HYPERFOCUS ##\n"
distractorsFile string = "/etc/hyperfocus_distractors"
marker string = "## MANAGED BY HYPERFOCUS ##\n"
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 {
var hostFile string
@ -35,70 +50,6 @@ func getHostFile() string {
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 {
currUser, err := user.Current()
@ -118,29 +69,63 @@ func improveFocus() {
}
defer hostFile.Close()
userDistractorsFile, err := os.Open(distractorsFile)
if err != nil {
log.Fatalln(err)
}
defer userDistractorsFile.Close()
if _, err := hostFile.WriteString(marker); err != nil {
log.Fatalln(err)
}
scanner := bufio.NewScanner(userDistractorsFile)
for scanner.Scan() {
var hostLine string = fmt.Sprintf("127.0.0.1\t%s\n", scanner.Text())
if _, err := hostFile.WriteString(hostLine); err != nil {
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() {
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 {
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 {
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'
@ -159,7 +144,7 @@ func loseFocus() {
log.Fatalln(err)
}
resetNetwork("Focus is now lost 🤪")
fmt.Println("Focus is now lost 🤪")
}
// Enables temporarily breaking concentration
@ -207,111 +192,110 @@ func listDistractors() {
func addPredefinedDistractors() {
distractors := []string{
"ycombinator.com",
"slashdot.com",
"facebook.com",
"reddit.com",
"gawker.com",
"theverge.com",
"techcrunch.com",
"thenextweb.com",
"wired.com",
"gizmodo.com",
"slickdeals.net",
"mashable.com",
"digitaltrends.com",
"techradar.com",
"twitter.com",
"tumblr.com",
"technorati.com",
"digg.com",
"anandtech.com",
"answerbag.com",
"arstechnica.com",
"awkwardfamilyphotos.com",
"bbc.com",
"befunky.com",
"break.com",
"buzzfeed.com",
"twitter.com",
"youtube.com",
"netflix.com",
"iwastesomuchtime.com",
"pinterest.com",
"cheezburger.com",
"cnet.com",
"cnn.com",
"cracked.com",
"digg.com",
"digitaltrends.com",
"distrowatch.com",
"ebay.com",
"thepointsguy.com",
"imgur.com",
"woot.com",
"engadget.com",
"everypoet.com",
"facebook.com",
"failblog.com",
"fark.com",
"firstpost.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",
"iwastesomuchtime.com",
"lifehacker.com",
"liveplasma.com",
"lunchtimers.com",
"mashable.com",
"mcsweeneys.net",
"medium.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",
"mcsweeneys.net",
"postsecret.com",
"textsfromlastnight.com",
"awkwardfamilyphotos.com",
"myparentsjoinedfacebook.com",
"myspace.com",
"lunchtimers.com",
"twitterfall.com",
"break.com",
"passiveaggressivenotes.com",
"sciencemag.org",
"bbc.com",
"netflix.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 {
log.Fatalln(err)
}
defer userDistractorsFile.Close()
defer predefDistractorsFileObj.Close()
dataWriter := bufio.NewWriter(userDistractorsFile)
dataWriter := bufio.NewWriter(predefDistractorsFileObj)
for _, v := range distractors {
_, _ = dataWriter.WriteString(v + "\n")
}
dataWriter.Flush()