rc: add support for Go 1.6

This commit is contained in:
remusb 2018-03-14 21:48:01 +02:00 committed by Remus Bunduc
parent 86e5a35491
commit 97b48cf988
4 changed files with 40 additions and 16 deletions

View file

@ -106,9 +106,7 @@ func run(args []string) (err error) {
// Write the JSON blob to stdout if required // Write the JSON blob to stdout if required
if out != nil && !noOutput { if out != nil && !noOutput {
enc := json.NewEncoder(os.Stdout) err := rc.WriteJSON(os.Stdout, out)
enc.SetIndent("", "\t")
err = enc.Encode(out)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to output JSON") return errors.Wrap(err, "failed to output JSON")
} }

View file

@ -62,17 +62,6 @@ func (s *server) serve() {
s.srv.Serve() s.srv.Serve()
} }
// writes JSON in out to w
func writeJSON(w http.ResponseWriter, out Params) {
enc := json.NewEncoder(w)
enc.SetIndent("", "\t")
err := enc.Encode(out)
if err != nil {
// can't return the error at this point
fs.Errorf(nil, "rc: failed to write JSON output: %v", err)
}
}
// handler reads incoming requests and dispatches them // handler reads incoming requests and dispatches them
func (s *server) handler(w http.ResponseWriter, r *http.Request) { func (s *server) handler(w http.ResponseWriter, r *http.Request) {
path := strings.Trim(r.URL.Path, "/") path := strings.Trim(r.URL.Path, "/")
@ -81,10 +70,14 @@ func (s *server) handler(w http.ResponseWriter, r *http.Request) {
writeError := func(err error, status int) { writeError := func(err error, status int) {
fs.Errorf(nil, "rc: %q: error: %v", path, err) fs.Errorf(nil, "rc: %q: error: %v", path, err)
w.WriteHeader(status) w.WriteHeader(status)
writeJSON(w, Params{ err = WriteJSON(w, Params{
"error": err.Error(), "error": err.Error(),
"input": in, "input": in,
}) })
if err != nil {
// can't return the error at this point
fs.Errorf(nil, "rc: failed to write JSON output: %v", err)
}
} }
if r.Method != "POST" { if r.Method != "POST" {
@ -131,5 +124,9 @@ func (s *server) handler(w http.ResponseWriter, r *http.Request) {
} }
fs.Debugf(nil, "rc: %q: reply %+v: %v", path, out, err) fs.Debugf(nil, "rc: %q: reply %+v: %v", path, out, err)
writeJSON(w, out) err = WriteJSON(w, out)
if err != nil {
// can't return the error at this point
fs.Errorf(nil, "rc: failed to write JSON output: %v", err)
}
} }

15
fs/rc/rc_new.go Normal file
View file

@ -0,0 +1,15 @@
//+build go1.7
package rc
import (
"encoding/json"
"io"
)
// WriteJSON writes JSON in out to w
func WriteJSON(w io.Writer, out Params) error {
enc := json.NewEncoder(w)
enc.SetIndent("", "\t")
return enc.Encode(out)
}

14
fs/rc/rc_old.go Normal file
View file

@ -0,0 +1,14 @@
//+build !go1.7
package rc
import (
"encoding/json"
"io"
)
// WriteJSON writes JSON in out to w
func WriteJSON(w io.Writer, out Params) error {
enc := json.NewEncoder(w)
return enc.Encode(out)
}