diff --git a/cmd/circologctl/main.go b/cmd/circologctl/main.go index 0eb7ed9..4e21768 100644 --- a/cmd/circologctl/main.go +++ b/cmd/circologctl/main.go @@ -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) diff --git a/cmd/circologd/http_ctl.go b/cmd/circologd/http_ctl.go index 26f0c8b..c584b64 100644 --- a/cmd/circologd/http_ctl.go +++ b/cmd/circologd/http_ctl.go @@ -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", } diff --git a/hub.go b/hub.go index 209f0ab..dfb760d 100644 --- a/hub.go +++ b/hub.go @@ -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} } } }