Merge branch 'statusctl' of blallo/circolog into master
This commit is contained in:
commit
bf9667a8a8
3 changed files with 75 additions and 17 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -11,6 +12,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.lattuga.net/boyska/circolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalOpts struct {
|
var globalOpts struct {
|
||||||
|
@ -30,6 +33,7 @@ func init() {
|
||||||
// TODO: implement set and get of config at runtime
|
// TODO: implement set and get of config at runtime
|
||||||
//"set": setCmd,
|
//"set": setCmd,
|
||||||
//"get": getCmd,
|
//"get": getCmd,
|
||||||
|
"status": statusCmd,
|
||||||
"pause": pauseCmd,
|
"pause": pauseCmd,
|
||||||
"filter": filterCmd,
|
"filter": filterCmd,
|
||||||
"reload": reloadCmd,
|
"reload": reloadCmd,
|
||||||
|
@ -42,24 +46,41 @@ func init() {
|
||||||
|
|
||||||
//func getCmd(ctlSock string, args []string) error {}
|
//func getCmd(ctlSock string, args []string) error {}
|
||||||
|
|
||||||
func filterCmd(args []string) error {
|
func statusCmd(args []string) error {
|
||||||
filter := strings.Join(args[1:], " ")
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||||
postBody := make(map[string][]string)
|
outFormat := flagset.String("format", "plain", "Which format to use as output for this command (json, pretty, plain)")
|
||||||
postBody["where"] = []string{filter}
|
flagset.Parse(args[1:])
|
||||||
if globalOpts.debug {
|
resp, err := ctl.Get("http://unix/status")
|
||||||
fmt.Println("[DEBUG] postBody:", postBody)
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
resp, err := ctl.PostForm("http://unix/filter", postBody)
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != 200 || globalOpts.verbose {
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
||||||
defer resp.Body.Close()
|
if err != nil {
|
||||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
func pauseCmd(args []string) error {
|
||||||
var dontChangeAgain time.Duration
|
var dontChangeAgain time.Duration
|
||||||
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||||
|
@ -84,6 +105,25 @@ func pauseCmd(args []string) error {
|
||||||
return err
|
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 {
|
func reloadCmd(args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,6 +117,7 @@ func printHelp(verbose bool) http.HandlerFunc {
|
||||||
var pathsWithDocs = map[string]string{
|
var pathsWithDocs = map[string]string{
|
||||||
"/pause/toggle": "Toggle the server from pause state (not listening)",
|
"/pause/toggle": "Toggle the server from pause state (not listening)",
|
||||||
"/logs/clear": "Wipe the buffer from all the messages",
|
"/logs/clear": "Wipe the buffer from all the messages",
|
||||||
|
"/status": "Get info on the status of the server",
|
||||||
"/help": "This help",
|
"/help": "This help",
|
||||||
"/echo": "Answers to heartbeat",
|
"/echo": "Answers to heartbeat",
|
||||||
}
|
}
|
||||||
|
|
27
hub.go
27
hub.go
|
@ -44,10 +44,26 @@ type HubFullCommand struct {
|
||||||
Parameters map[string]interface{}
|
Parameters map[string]interface{}
|
||||||
Response chan CommandResponse
|
Response chan CommandResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandResponse struct {
|
type CommandResponse struct {
|
||||||
Value interface{}
|
Value interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StatusResponse is an implementation of a CommandResponse
|
||||||
|
type StatusResponse struct {
|
||||||
|
Size int `json:"size"`
|
||||||
|
IsRunning bool `json:"running"`
|
||||||
|
Filter string `json:"filter"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status return "paused/unpaused" based on isRunning value
|
||||||
|
func (r StatusResponse) Status() string {
|
||||||
|
if r.IsRunning {
|
||||||
|
return "unpaused"
|
||||||
|
}
|
||||||
|
return "paused"
|
||||||
|
}
|
||||||
|
|
||||||
type Hub struct {
|
type Hub struct {
|
||||||
Register chan Client
|
Register chan Client
|
||||||
Unregister chan Client
|
Unregister chan Client
|
||||||
|
@ -141,11 +157,12 @@ func (h *Hub) Run() {
|
||||||
fmt.Println("paused")
|
fmt.Println("paused")
|
||||||
cmd.Response <- CommandResponse{Value: active}
|
cmd.Response <- CommandResponse{Value: active}
|
||||||
case CommandStatus:
|
case CommandStatus:
|
||||||
cmd.Response <- CommandResponse{Value: map[string]interface{}{
|
var resp = StatusResponse{
|
||||||
"size": h.circbuf.Len(),
|
Size: h.circbuf.Len(),
|
||||||
"paused": !active,
|
IsRunning: active,
|
||||||
"filter": filter.String(),
|
Filter: filter.String(),
|
||||||
}}
|
}
|
||||||
|
cmd.Response <- CommandResponse{Value: resp}
|
||||||
case CommandNewFilter:
|
case CommandNewFilter:
|
||||||
if err := filter.Set(cmd.Parameters["where"].(string)); err != nil {
|
if err := filter.Set(cmd.Parameters["where"].(string)); err != nil {
|
||||||
cmd.Response <- CommandResponse{Value: map[string]interface{}{
|
cmd.Response <- CommandResponse{Value: map[string]interface{}{
|
||||||
|
|
Loading…
Reference in a new issue