2018-12-20 09:45:05 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-21 18:09:32 +01:00
|
|
|
"context"
|
2019-01-03 11:15:30 +01:00
|
|
|
"encoding/json"
|
2018-12-20 09:45:05 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-12-21 18:09:32 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-12-20 09:45:05 +01:00
|
|
|
"os"
|
2018-12-24 16:48:11 +01:00
|
|
|
"time"
|
2019-01-03 11:15:30 +01:00
|
|
|
|
|
|
|
"git.lattuga.net/boyska/circolog"
|
2018-12-20 09:45:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var globalOpts struct {
|
|
|
|
ctlSock string
|
|
|
|
verbose bool
|
2018-12-24 18:41:06 +01:00
|
|
|
debug bool
|
2018-12-20 09:45:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 18:09:32 +01:00
|
|
|
var ctl http.Client
|
|
|
|
|
2018-12-20 09:45:05 +01:00
|
|
|
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,
|
2019-01-03 11:15:30 +01:00
|
|
|
"status": statusCmd,
|
2018-12-20 09:45:05 +01:00
|
|
|
"pause": pauseCmd,
|
|
|
|
"reload": reloadCmd,
|
|
|
|
"restart": restartCmd,
|
|
|
|
"help": helpCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//func setCmd(ctlSock string, args []string) error {}
|
|
|
|
|
|
|
|
//func getCmd(ctlSock string, args []string) error {}
|
|
|
|
|
2019-01-03 11:15:30 +01:00
|
|
|
func statusCmd(args []string) error {
|
|
|
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
|
|
|
outFormat := flagset.String("format", "plain", "Which format to use as output for this command (json, pretty, plain)")
|
|
|
|
flagset.Parse(args[1:])
|
|
|
|
resp, err := ctl.Get("http://unix/status")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
respJSON := make(map[string]circolog.StatusResponse)
|
|
|
|
err = json.Unmarshal(respBytes, &respJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch *outFormat {
|
|
|
|
case "json":
|
|
|
|
fmt.Printf("%s", string(respBytes))
|
|
|
|
case "pretty":
|
|
|
|
prettyJSON, err := json.MarshalIndent(respJSON, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", prettyJSON)
|
|
|
|
case "plain":
|
2019-01-03 11:51:14 +01:00
|
|
|
fmt.Printf("Buffer Length: %d\n", respJSON["status"].Size)
|
2019-01-03 11:15:30 +01:00
|
|
|
fmt.Printf("Server Status: %s\n", respJSON["status"].Status())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 09:45:05 +01:00
|
|
|
func pauseCmd(args []string) error {
|
2018-12-24 16:48:11 +01:00
|
|
|
var dontChangeAgain time.Duration
|
2018-12-21 18:09:32 +01:00
|
|
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
2018-12-24 16:48:11 +01:00
|
|
|
waitTime := flagset.Duration("wait-time", dontChangeAgain, "How long to wait before untoggling the state, defaults to never")
|
2018-12-21 18:09:32 +01:00
|
|
|
flagset.Parse(args[1:])
|
2018-12-24 18:41:06 +01:00
|
|
|
postBody := make(map[string][]string)
|
2018-12-24 16:48:11 +01:00
|
|
|
if *waitTime != dontChangeAgain {
|
2018-12-24 18:41:06 +01:00
|
|
|
postBody["waitTime"] = []string{fmt.Sprintf("%s", *waitTime)}
|
2018-12-21 18:09:32 +01:00
|
|
|
}
|
2018-12-24 18:41:06 +01:00
|
|
|
if globalOpts.debug {
|
|
|
|
fmt.Println("[DEBUG] postBody:", postBody)
|
|
|
|
}
|
|
|
|
resp, err := ctl.PostForm("http://unix/pause/toggle", postBody)
|
2018-12-21 18:09:32 +01:00
|
|
|
if globalOpts.verbose {
|
|
|
|
defer resp.Body.Close()
|
2018-12-24 16:48:11 +01:00
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-12-21 18:09:32 +01:00
|
|
|
}
|
|
|
|
fmt.Println(string(bodyBytes))
|
|
|
|
}
|
|
|
|
return err
|
2018-12-20 09:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2018-12-21 18:09:32 +01:00
|
|
|
// 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)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-12-20 09:45:05 +01:00
|
|
|
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")
|
2018-12-24 18:41:06 +01:00
|
|
|
flag.BoolVar(&globalOpts.debug, "debug", false, "Print debugging info")
|
2018-12-20 09:45:05 +01:00
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
usage(os.Stderr)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
parseAndRun(args)
|
|
|
|
}
|