Compare commits
4 Commits
21c99ce8f6
...
1829422562
| Author | SHA1 | Date | |
|---|---|---|---|
| 1829422562 | |||
| fb8849322d | |||
| 9592e4bb63 | |||
| 1b14fcbea4 |
58
main.go
58
main.go
@@ -6,9 +6,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -115,37 +117,49 @@ func getTime(timeString string) (int, int) {
|
|||||||
return int(hr), int(min)
|
return int(hr), int(min)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNextWait(config sunsetConfig) (int, int) {
|
func getNextWait(config sunsetConfig) time.Duration {
|
||||||
index := nextIndex(config)
|
index := nextIndex(config)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
nextHr, nextMin := getTime(config.Files[index].Time)
|
nextHr, nextMin := getTime(config.Files[index].Time)
|
||||||
|
|
||||||
var diffHr int
|
// Today at the time
|
||||||
var diffMin int
|
nextTime := time.Date(now.Year(), now.Month(), now.Day(), nextHr, nextMin, 0, 0, now.Location())
|
||||||
|
|
||||||
// FIXME still not working
|
until := nextTime.Sub(now)
|
||||||
if now.Hour() >= nextHr && now.Minute() >= nextMin {
|
|
||||||
diffHr = 24 - (now.Hour() - nextHr)
|
if math.Floor(until.Minutes()) > 0 {
|
||||||
diffMin = 60 - (now.Minute() - nextMin)
|
return until
|
||||||
} else {
|
|
||||||
diffHr = nextHr - now.Hour()
|
|
||||||
diffMin = nextMin - now.Minute()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return diffHr, diffMin
|
// Next day the the time
|
||||||
|
nextTime = nextTime.AddDate(0, 0, 1)
|
||||||
|
return nextTime.Sub(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
func block(config sunsetConfig, command string) {
|
func currentImage(configPath string, config sunsetConfig) string {
|
||||||
|
index := currentIndex(config)
|
||||||
|
absFile, err := filepath.Abs(path.Join(path.Dir(configPath), config.Files[index].File))
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
return absFile
|
||||||
|
}
|
||||||
|
|
||||||
|
func block(configPath string, config sunsetConfig, command string) {
|
||||||
|
image := currentImage(configPath, config)
|
||||||
|
go runCommand(command, image)
|
||||||
for {
|
for {
|
||||||
nextHr, nextMin := getNextWait(config)
|
until := getNextWait(config)
|
||||||
timer := time.NewTimer((time.Duration(nextHr) * time.Hour) + (time.Duration(nextMin) * time.Minute))
|
timer := time.NewTimer(until)
|
||||||
<-timer.C
|
<-timer.C
|
||||||
go runCommand(command)
|
log.Printf("Running command: %s\n", command)
|
||||||
|
image := currentImage(configPath, config)
|
||||||
|
go runCommand(command, image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCommand(command string) {
|
func runCommand(command, image string) {
|
||||||
cmd := exec.Command("/usr/bin/env sh", "-c", command)
|
cmd := exec.Command(command, image)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Command %s returned a non 0 code", command)
|
log.Printf("Command %s returned a non 0 code", command)
|
||||||
@@ -165,19 +179,17 @@ func main() {
|
|||||||
if *printNextTime {
|
if *printNextTime {
|
||||||
fmt.Printf("%s", getNextTime(conf))
|
fmt.Printf("%s", getNextTime(conf))
|
||||||
} else if *printNextWait {
|
} else if *printNextWait {
|
||||||
hr, min := getNextWait(conf)
|
unitl := getNextWait(conf)
|
||||||
fmt.Printf("%dh%dm", hr, min)
|
fmt.Printf("%s", unitl)
|
||||||
|
|
||||||
} else if *blockFlag {
|
} else if *blockFlag {
|
||||||
if *blockCommand == "" {
|
if *blockCommand == "" {
|
||||||
fmt.Println("Must provide a command.")
|
fmt.Println("Must provide a command.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
block(conf, *blockCommand)
|
block(*configFile, conf, *blockCommand)
|
||||||
} else {
|
} else {
|
||||||
index := currentIndex(conf)
|
fmt.Printf("%s", currentImage(*configFile, conf))
|
||||||
absoluteImagePath := path.Join(path.Dir(*configFile), conf.Files[index].File)
|
|
||||||
fmt.Printf("%s", absoluteImagePath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user