Adding ctl subcommand to print status

This commit is contained in:
Blallo 2019-01-03 11:15:30 +01:00
parent 20269bf94e
commit 9427fe91b1
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F
3 changed files with 59 additions and 4 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
@ -10,6 +11,8 @@ import (
"net/http"
"os"
"time"
"git.lattuga.net/boyska/circolog"
)
var globalOpts struct {
@ -29,6 +32,7 @@ func init() {
// TODO: implement set and get of config at runtime
//"set": setCmd,
//"get": getCmd,
"status": statusCmd,
"pause": pauseCmd,
"reload": reloadCmd,
"restart": restartCmd,
@ -40,6 +44,40 @@ func init() {
//func getCmd(ctlSock string, args []string) error {}
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":
fmt.Printf("Buffer Length: %d\n", respJSON["status"].Len)
fmt.Printf("Server Status: %s\n", respJSON["status"].Status())
}
return nil
}
func pauseCmd(args []string) error {
var dontChangeAgain time.Duration
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)

View file

@ -96,6 +96,7 @@ func printHelp(verbose bool) http.HandlerFunc {
var pathsWithDocs = map[string]string{
"/pause/toggle": "Toggle the server from pause state (not listening)",
"/logs/clear": "Wipe the buffer from all the messages",
"/status": "Get info on the status of the server",
"/help": "This help",
"/echo": "Answers to heartbeat",
}

24
hub.go
View file

@ -42,10 +42,25 @@ type HubFullCommand struct {
Parameters map[string]interface{}
Response chan CommandResponse
}
type CommandResponse struct {
Value interface{}
}
// StatusResponse is an implementation of a CommandResponse
type StatusResponse struct {
Len int `json:"length"`
IsRunning bool `json:"running"`
}
// Status return "paused/unpaused" based on isRunning value
func (r StatusResponse) Status() string {
if r.IsRunning {
return "unpaused"
}
return "paused"
}
type Hub struct {
Register chan Client
Unregister chan Client
@ -138,10 +153,11 @@ func (h *Hub) Run() {
fmt.Println("paused")
cmd.Response <- CommandResponse{Value: active}
case CommandStatus:
cmd.Response <- CommandResponse{Value: map[string]interface{}{
"size": h.circbuf.Len(),
"paused": !active,
}}
var resp = StatusResponse{
Len: h.circbuf.Len(),
IsRunning: active,
}
cmd.Response <- CommandResponse{Value: resp}
}
}
}