Browse Source

use time.Duration for server autotoggle wait time

Blallo 5 years ago
parent
commit
ce7e715c2f
1 changed files with 8 additions and 49 deletions
  1. 8 49
      cmd/circologctl/main.go

+ 8 - 49
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))
 	}