Compare commits

..

7 Commits

Author SHA1 Message Date
ae3975dc47 drone prerelease 2020-12-29 14:43:25 +01:00
3006823255 updated drone file
All checks were successful
continuous-integration/drone/tag Build is passing
2020-12-29 14:36:47 +01:00
65f97127fa added drone file
Some checks failed
continuous-integration/drone/tag Build is failing
2020-12-09 23:24:14 +01:00
1829422562 fixed wait time function 2020-10-28 00:38:44 +01:00
fb8849322d command now gets the image as a parameter 2020-10-27 22:29:11 +01:00
9592e4bb63 now prints the absolute path 2020-10-27 22:26:09 +01:00
1b14fcbea4 fixed runCommand 2020-10-27 22:17:37 +01:00
2 changed files with 65 additions and 23 deletions

30
.drone.yml Normal file
View File

@@ -0,0 +1,30 @@
kind: pipeline
name: default
steps:
- name: build
image: golang
commands:
- go build
- name: gitea_release
image: plugins/gitea-release
settings:
api_key:
from_secret: GITEA_API_KEY
base_url: https://git.kapelle.org
files:
- sunset
checksum:
- md5
- sha1
- sha256
title: v0.1
prerelease: true
when:
event:
- tag
trigger:
event:
- tag

58
main.go
View File

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