|
@@ -2,6 +2,7 @@ package main
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"flag"
|
|
|
"fmt"
|
|
|
"io"
|
|
@@ -11,6 +12,8 @@ import (
|
|
|
"os"
|
|
|
"strings"
|
|
|
"time"
|
|
|
+
|
|
|
+ "git.lattuga.net/boyska/circolog"
|
|
|
)
|
|
|
|
|
|
var globalOpts struct {
|
|
@@ -30,6 +33,7 @@ func init() {
|
|
|
// TODO: implement set and get of config at runtime
|
|
|
//"set": setCmd,
|
|
|
//"get": getCmd,
|
|
|
+ "status": statusCmd,
|
|
|
"pause": pauseCmd,
|
|
|
"filter": filterCmd,
|
|
|
"reload": reloadCmd,
|
|
@@ -42,24 +46,41 @@ func init() {
|
|
|
|
|
|
//func getCmd(ctlSock string, args []string) error {}
|
|
|
|
|
|
-func filterCmd(args []string) error {
|
|
|
- filter := strings.Join(args[1:], " ")
|
|
|
- postBody := make(map[string][]string)
|
|
|
- postBody["where"] = []string{filter}
|
|
|
- if globalOpts.debug {
|
|
|
- fmt.Println("[DEBUG] postBody:", postBody)
|
|
|
+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
|
|
|
}
|
|
|
- resp, err := ctl.PostForm("http://unix/filter", postBody)
|
|
|
- if resp.StatusCode != 200 || globalOpts.verbose {
|
|
|
- defer resp.Body.Close()
|
|
|
- bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
+ 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.Println(string(bodyBytes))
|
|
|
+ fmt.Printf("%s\n", prettyJSON)
|
|
|
+ case "plain":
|
|
|
+ fmt.Printf("Buffer Size: %d\n", respJSON["status"].Size)
|
|
|
+ fmt.Printf("Server Status: %s\n", respJSON["status"].Status())
|
|
|
+ fmt.Printf("Filter String: %s\n", respJSON["status"].Filter)
|
|
|
}
|
|
|
- return err
|
|
|
+ return nil
|
|
|
}
|
|
|
+
|
|
|
func pauseCmd(args []string) error {
|
|
|
var dontChangeAgain time.Duration
|
|
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
|
@@ -84,6 +105,25 @@ func pauseCmd(args []string) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+func filterCmd(args []string) error {
|
|
|
+ filter := strings.Join(args[1:], " ")
|
|
|
+ postBody := make(map[string][]string)
|
|
|
+ postBody["where"] = []string{filter}
|
|
|
+ if globalOpts.debug {
|
|
|
+ fmt.Println("[DEBUG] postBody:", postBody)
|
|
|
+ }
|
|
|
+ resp, err := ctl.PostForm("http://unix/filter", postBody)
|
|
|
+ if resp.StatusCode != 200 || globalOpts.verbose {
|
|
|
+ defer resp.Body.Close()
|
|
|
+ bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ fmt.Println(string(bodyBytes))
|
|
|
+ }
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
func reloadCmd(args []string) error {
|
|
|
return nil
|
|
|
}
|