From 9427fe91b1c758605725009b2630a4a289e2fd32 Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 3 Jan 2019 11:15:30 +0100 Subject: [PATCH 1/4] Adding ctl subcommand to print status --- cmd/circologctl/main.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/circologd/http_ctl.go | 1 + hub.go | 24 ++++++++++++++++++++---- 3 files changed, 59 insertions(+), 4 deletions(-) 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} } } } From d1223fc17049f46ce17a491080dd70bc0ed8f77b Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 3 Jan 2019 11:51:14 +0100 Subject: [PATCH 2/4] Changing StatusResponse.Len to .Size --- cmd/circologctl/main.go | 2 +- hub.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/circologctl/main.go b/cmd/circologctl/main.go index 4e21768..c924ce1 100644 --- a/cmd/circologctl/main.go +++ b/cmd/circologctl/main.go @@ -72,7 +72,7 @@ func statusCmd(args []string) error { } fmt.Printf("%s\n", prettyJSON) case "plain": - fmt.Printf("Buffer Length: %d\n", respJSON["status"].Len) + fmt.Printf("Buffer Length: %d\n", respJSON["status"].Size) fmt.Printf("Server Status: %s\n", respJSON["status"].Status()) } return nil diff --git a/hub.go b/hub.go index dfb760d..c3b07e0 100644 --- a/hub.go +++ b/hub.go @@ -49,7 +49,7 @@ type CommandResponse struct { // StatusResponse is an implementation of a CommandResponse type StatusResponse struct { - Len int `json:"length"` + Size int `json:"size"` IsRunning bool `json:"running"` } @@ -154,7 +154,7 @@ func (h *Hub) Run() { cmd.Response <- CommandResponse{Value: active} case CommandStatus: var resp = StatusResponse{ - Len: h.circbuf.Len(), + Size: h.circbuf.Len(), IsRunning: active, } cmd.Response <- CommandResponse{Value: resp} From a8cefc993b5a3c1e477137eab7f85fdcc5703af0 Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 3 Jan 2019 12:11:03 +0100 Subject: [PATCH 3/4] Fix the text shown --- cmd/circologctl/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/circologctl/main.go b/cmd/circologctl/main.go index c924ce1..7d97e92 100644 --- a/cmd/circologctl/main.go +++ b/cmd/circologctl/main.go @@ -72,7 +72,7 @@ func statusCmd(args []string) error { } fmt.Printf("%s\n", prettyJSON) case "plain": - fmt.Printf("Buffer Length: %d\n", respJSON["status"].Size) + fmt.Printf("Buffer Size: %d\n", respJSON["status"].Size) fmt.Printf("Server Status: %s\n", respJSON["status"].Status()) } return nil From 133852518373795145d53d89a77b7e0ecc201436 Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 3 Jan 2019 13:42:16 +0100 Subject: [PATCH 4/4] Fixing plain format in ctl --- cmd/circologctl/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/circologctl/main.go b/cmd/circologctl/main.go index 076eaac..e94cbf1 100644 --- a/cmd/circologctl/main.go +++ b/cmd/circologctl/main.go @@ -76,6 +76,7 @@ func statusCmd(args []string) error { 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 nil }