use time.Duration for server autotoggle wait time

This commit is contained in:
Blallo 2018-12-24 16:48:11 +01:00
parent 8865335515
commit ce7e715c2f
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -9,8 +9,8 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strconv"
"strings" "strings"
"time"
) )
var globalOpts struct { var globalOpts struct {
@ -40,62 +40,21 @@ func init() {
//func getCmd(ctlSock string, args []string) error {} //func getCmd(ctlSock string, args []string) error {}
func parsePauseOpts(postponeTime string) (string, error) {
var waitTime int64
var err error
L := len(postponeTime)
switch unit := postponeTime[L-1]; string(unit) {
case "s":
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
if err != nil {
return "", err
}
case "m":
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
if err != nil {
return "", err
}
waitTime = waitTime * 60
case "h":
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
if err != nil {
return "", err
}
waitTime = waitTime * 60 * 60
case "d":
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
if err != nil {
return "", err
}
waitTime = waitTime * 60 * 60 * 24
case "w":
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
if err != nil {
return "", err
}
waitTime = waitTime * 60 * 60 * 24 * 7
}
return string(waitTime), nil
}
func pauseCmd(args []string) error { func pauseCmd(args []string) error {
var postBody string var postBody string
var err error var dontChangeAgain time.Duration
flagset := flag.NewFlagSet(args[0], flag.ExitOnError) flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
postponeTime := flagset.String("postpone", "", "How long to wait before untoggling the state") waitTime := flagset.Duration("wait-time", dontChangeAgain, "How long to wait before untoggling the state, defaults to never")
flagset.Parse(args[1:]) flagset.Parse(args[1:])
if *postponeTime != "" { if *waitTime != dontChangeAgain {
postBody, err = parsePauseOpts(*postponeTime) postBody = fmt.Sprintf("waittime=%s", *waitTime)
if err != nil {
return err
}
} }
resp, err := ctl.Post("http://unix/pause/toggle", "application/octet-stream", strings.NewReader(postBody)) resp, err := ctl.Post("http://unix/pause/toggle", "application/octet-stream", strings.NewReader(postBody))
if globalOpts.verbose { if globalOpts.verbose {
defer resp.Body.Close() defer resp.Body.Close()
bodyBytes, err2 := ioutil.ReadAll(resp.Body) bodyBytes, err := ioutil.ReadAll(resp.Body)
if err2 != nil { if err != nil {
return err2 return err
} }
fmt.Println(string(bodyBytes)) fmt.Println(string(bodyBytes))
} }