From e702b36ce7404fb9389f77b4456b55ebefff0c00 Mon Sep 17 00:00:00 2001 From: Pavel Podkorytov Date: Mon, 2 Apr 2018 18:26:03 +0500 Subject: [PATCH] Add BlockedBy to output for status --- cmd/sd-dbus-hooks/main.go | 2 +- cmd/sd-dbus-hooks/status.go | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/sd-dbus-hooks/main.go b/cmd/sd-dbus-hooks/main.go index fee4675..3b71f55 100644 --- a/cmd/sd-dbus-hooks/main.go +++ b/cmd/sd-dbus-hooks/main.go @@ -41,7 +41,7 @@ func main() { http.Handle("/unit/start/", unitStartHandler{conn, cfg}) http.Handle("/unit/stop/", unitStopHandler{conn, cfg}) - http.Handle("/unit/status/", unitStatusHandler{conn}) + http.Handle("/unit/status/", unitStatusHandler{conn, cfg}) // http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) diff --git a/cmd/sd-dbus-hooks/status.go b/cmd/sd-dbus-hooks/status.go index 66d4af0..6884dcf 100644 --- a/cmd/sd-dbus-hooks/status.go +++ b/cmd/sd-dbus-hooks/status.go @@ -10,10 +10,20 @@ import ( type unitStatusHandler struct { conn *dbus.Conn + cfg *Config } func (h unitStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { name := r.URL.Path[len("/unit/status/"):] + + // check if unit in config + unitCfg, err := h.cfg.getUnit(name) + if err != nil { + log.Printf("[ERROR] %s", err) + w.WriteHeader(http.StatusForbidden) + return + } + log.Printf("[INFO] get unit status %v", name) units, err := h.conn.ListUnits() @@ -25,11 +35,12 @@ func (h unitStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, unit := range units { if unit.Name == name { - fmt.Fprintf(w, "Name: %+v\n", unit.Name) - fmt.Fprintf(w, "Description: %+v\n", unit.Description) - fmt.Fprintf(w, "LoadState: %+v\n", unit.LoadState) - fmt.Fprintf(w, "ActiveState: %+v\n", unit.ActiveState) - fmt.Fprintf(w, "SubState: %+v\n", unit.ActiveState) + fmt.Fprintf(w, "Name %v\n", unit.Name) + fmt.Fprintf(w, "Description %v\n", unit.Description) + fmt.Fprintf(w, "LoadState %v\n", unit.LoadState) + fmt.Fprintf(w, "ActiveState %v\n", unit.ActiveState) + fmt.Fprintf(w, "SubState %v\n", unit.ActiveState) + fmt.Fprintf(w, "BlockedBy %v\n", unitCfg.BlockedBy) return } }