pretty print in the client

this makes sure that source documents are pretty-printed, as ES won't
pretty print source documents if you just supply the ?pretty query param
master
Jordan Orelli 9 years ago
parent 9aaeccccee
commit 76b6f1796d

@ -3,10 +3,12 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/jordanorelli/moon" "github.com/jordanorelli/moon"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -74,10 +76,6 @@ func (r *repl) run() {
req.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"} req.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"}
q := req.URL.Query()
q.Set("pretty", "")
req.URL.RawQuery = q.Encode()
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
r.errorf("error sending http request: %v", err) r.errorf("error sending http request: %v", err)
@ -114,7 +112,19 @@ func (r *repl) dumpResponse(res *http.Response) {
w = r.out1 w = r.out1
} }
io.Copy(w, res.Body) b, err := ioutil.ReadAll(res.Body)
if err != nil {
r.errorf("error reading response body: %v", err)
return
}
var buf bytes.Buffer
if err := json.Indent(&buf, b, "", " "); err != nil {
r.errorf("error pretty-printing json: %v", err)
return
}
io.Copy(w, &buf)
w.Write([]byte("\n")) w.Write([]byte("\n"))
} }

Loading…
Cancel
Save