From 6fd0f121a053c24c9da7041afb9a66804471b764 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 12 Dec 2015 14:44:54 +0000 Subject: [PATCH 1/2] Add a 'json' function for 'weave report -f' --- prog/weaver/http.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/prog/weaver/http.go b/prog/weaver/http.go index 1dc849256b..68e725008d 100644 --- a/prog/weaver/http.go +++ b/prog/weaver/http.go @@ -245,7 +245,13 @@ func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRout muxRouter.Methods("GET").Path("/report").Queries("format", "{format}").HandlerFunc( func(w http.ResponseWriter, r *http.Request) { - formatTemplate, err := template.New("format").Parse(mux.Vars(r)["format"]) + funcs := template.FuncMap{ + "json": func(v interface{}) string { + a, _ := json.Marshal(v) + return string(a) + }, + } + formatTemplate, err := template.New("format").Funcs(funcs).Parse(mux.Vars(r)["format"]) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return From c341399498d50461c595491b52903e865aca329f Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 15 Dec 2015 13:52:28 +0000 Subject: [PATCH 2/2] Document 'json' function --- site/troubleshooting.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/site/troubleshooting.md b/site/troubleshooting.md index bc5bbb2b1a..a3df5a2354 100644 --- a/site/troubleshooting.md +++ b/site/troubleshooting.md @@ -228,6 +228,12 @@ inspect`: $ weave report -f {% raw %}'{{.DNS.Domain}}'{% endraw %} weave.local. +Weave adds a template function, `json`, which can be applied to get +results in JSON format. + + $ weave report -f {% raw %}'{{json .DNS}}'{% endraw %} + {% raw %}{"Domain":"weave.local.","Upstream":["8.8.8.8","8.8.4.4"],"Address":"172.17.0.1:53","TTL":1,"Entries":null}{% endraw %} + ### List attached containers weave ps