Adding ctl subcommand to print status
This commit is contained in:
parent
20269bf94e
commit
9427fe91b1
3 changed files with 59 additions and 4 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -10,6 +11,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.lattuga.net/boyska/circolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalOpts struct {
|
var globalOpts struct {
|
||||||
|
@ -29,6 +32,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,
|
||||||
"reload": reloadCmd,
|
"reload": reloadCmd,
|
||||||
"restart": restartCmd,
|
"restart": restartCmd,
|
||||||
|
@ -40,6 +44,40 @@ func init() {
|
||||||
|
|
||||||
//func getCmd(ctlSock string, args []string) error {}
|
//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 {
|
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)
|
||||||
|
|
|
@ -96,6 +96,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",
|
||||||
}
|
}
|
||||||
|
|
24
hub.go
24
hub.go
|
@ -42,10 +42,25 @@ 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 {
|
||||||
|
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 {
|
type Hub struct {
|
||||||
Register chan Client
|
Register chan Client
|
||||||
Unregister chan Client
|
Unregister chan Client
|
||||||
|
@ -138,10 +153,11 @@ 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(),
|
Len: h.circbuf.Len(),
|
||||||
"paused": !active,
|
IsRunning: active,
|
||||||
}}
|
}
|
||||||
|
cmd.Response <- CommandResponse{Value: resp}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue