From ce7e715c2f5fd6f52a4b796cd2f10f780eb81281 Mon Sep 17 00:00:00 2001 From: Blallo Date: Mon, 24 Dec 2018 16:48:11 +0100 Subject: [PATCH] use time.Duration for server autotoggle wait time --- cmd/circologctl/main.go | 57 ++++++----------------------------------- 1 file changed, 8 insertions(+), 49 deletions(-) diff --git a/cmd/circologctl/main.go b/cmd/circologctl/main.go index 4f2ff51..d95ffe0 100644 --- a/cmd/circologctl/main.go +++ b/cmd/circologctl/main.go @@ -9,8 +9,8 @@ import ( "net" "net/http" "os" - "strconv" "strings" + "time" ) var globalOpts struct { @@ -40,62 +40,21 @@ func init() { //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 { var postBody string - var err error + var dontChangeAgain time.Duration 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:]) - if *postponeTime != "" { - postBody, err = parsePauseOpts(*postponeTime) - if err != nil { - return err - } + if *waitTime != dontChangeAgain { + postBody = fmt.Sprintf("waittime=%s", *waitTime) } resp, err := ctl.Post("http://unix/pause/toggle", "application/octet-stream", strings.NewReader(postBody)) if globalOpts.verbose { defer resp.Body.Close() - bodyBytes, err2 := ioutil.ReadAll(resp.Body) - if err2 != nil { - return err2 + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err } fmt.Println(string(bodyBytes)) }