forked from boyska/circolog
161 lines
3.4 KiB
Go
161 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var globalOpts struct {
|
|
ctlSock string
|
|
verbose bool
|
|
}
|
|
|
|
var ctl http.Client
|
|
|
|
type commandFunc func([]string) error
|
|
|
|
var cmdMap map[string]commandFunc
|
|
|
|
func init() {
|
|
cmdMap = map[string]commandFunc{
|
|
// TODO: implement set and get of config at runtime
|
|
//"set": setCmd,
|
|
//"get": getCmd,
|
|
"pause": pauseCmd,
|
|
"reload": reloadCmd,
|
|
"restart": restartCmd,
|
|
"help": helpCmd,
|
|
}
|
|
}
|
|
|
|
//func setCmd(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 {
|
|
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 {
|
|
return nil
|
|
}
|
|
|
|
func restartCmd(args []string) error {
|
|
return nil
|
|
}
|
|
|
|
func helpCmd(args []string) error {
|
|
usage(os.Stdout)
|
|
os.Exit(0)
|
|
return nil
|
|
}
|
|
|
|
func usage(w io.Writer) {
|
|
fmt.Fprintf(w, "USAGE: %s [globalOpts] [SUBCOMMAND] [opts]\n", os.Args[0])
|
|
fmt.Fprintf(w, "\nSUBCOMMANDS:\n\n")
|
|
for command := range cmdMap {
|
|
fmt.Fprintf(w, "\t%s\n", command)
|
|
}
|
|
}
|
|
|
|
func parseAndRun(args []string) {
|
|
cmdName := args[0]
|
|
cmdToRun, ok := cmdMap[cmdName]
|
|
if !ok {
|
|
fmt.Fprintf(os.Stderr, "Unknown subcommand: %s\n", cmdName)
|
|
usage(os.Stderr)
|
|
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)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error:\n%s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
flag.StringVar(&globalOpts.ctlSock, "ctl-socket", "/tmp/circologd-ctl.sock",
|
|
"Path to a unix domain socket for the control server; leave empty to disable")
|
|
flag.BoolVar(&globalOpts.verbose, "verbose", false, "Print more output")
|
|
flag.Parse()
|
|
args := flag.Args()
|
|
if len(args) == 0 {
|
|
usage(os.Stderr)
|
|
os.Exit(-1)
|
|
}
|
|
parseAndRun(args)
|
|
}
|