1
0
Fork 0
forked from boyska/circolog

[ctl] pause subcommand partially implemented

This commit is contained in:
Blallo 2018-12-21 18:09:32 +01:00
parent 19045d9b25
commit 9ef425d827
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -1,10 +1,16 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net"
"net/http"
"os" "os"
"strconv"
"strings"
) )
var globalOpts struct { var globalOpts struct {
@ -12,6 +18,8 @@ var globalOpts struct {
verbose bool verbose bool
} }
var ctl http.Client
type commandFunc func([]string) error type commandFunc func([]string) error
var cmdMap map[string]commandFunc var cmdMap map[string]commandFunc
@ -32,8 +40,66 @@ 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 {
return nil var postBody string
var err error
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
postponeTime := flagset.String("postpone", "", "How long to wait before untoggling the state")
flagset.Parse(args[1:])
if *postponeTime != "" {
postBody, err = parsePauseOpts(*postponeTime)
if err != nil {
return err
}
}
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
}
fmt.Println(string(bodyBytes))
}
return err
} }
func reloadCmd(args []string) error { func reloadCmd(args []string) error {
@ -66,6 +132,14 @@ func parseAndRun(args []string) {
usage(os.Stderr) usage(os.Stderr)
os.Exit(2) os.Exit(2)
} }
// from here: https://gist.github.com/teknoraver/5ffacb8757330715bcbcc90e6d46ac74
ctl = http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", globalOpts.ctlSock)
},
},
}
err := cmdToRun(args) err := cmdToRun(args)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error:\n%s\n", err) fmt.Fprintf(os.Stderr, "Error:\n%s\n", err)
@ -79,9 +153,6 @@ func main() {
flag.BoolVar(&globalOpts.verbose, "verbose", false, "Print more output") flag.BoolVar(&globalOpts.verbose, "verbose", false, "Print more output")
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
if globalOpts.verbose {
fmt.Fprintf(os.Stdout, "Args: %v", args)
}
if len(args) == 0 { if len(args) == 0 {
usage(os.Stderr) usage(os.Stderr)
os.Exit(-1) os.Exit(-1)