Skip to content

Commit

Permalink
🌱 Format API JSON output (#265)
Browse files Browse the repository at this point in the history
* Add --indent flag to pretty print json output

Signed-off-by: Spencer Schrock <[email protected]>

* move flag so generated code doesn't delete

Signed-off-by: Spencer Schrock <[email protected]>

* move indent flag from init()

Signed-off-by: Spencer Schrock <[email protected]>

Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock authored Nov 28, 2022
1 parent 6335f0d commit 90a8d87
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app/generated/restapi/configure_scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ package restapi
import (
"crypto/tls"
"embed"
"encoding/json"
"io"
"io/fs"
"net/http"

"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
"github.com/rs/cors"
flag "github.com/spf13/pflag"

"github.com/ossf/scorecard-webapp/app/generated/restapi/operations"
"github.com/ossf/scorecard-webapp/app/generated/restapi/operations/badge"
Expand All @@ -35,8 +38,11 @@ import (
//nolint:lll // generated code
//go:generate swagger generate server --target ../../generated --name Scorecard --spec ../../../openapi.yaml --principal interface{}

//go:embed static
var staticDir embed.FS
var (
//go:embed static
staticDir embed.FS
jsonIndent = flag.String("indent", "", "the indent used in json output (default no ident \"\")")
)

func configureFlags(api *operations.ScorecardAPI) {
// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
Expand All @@ -57,7 +63,12 @@ func configureAPI(api *operations.ScorecardAPI) http.Handler {
// api.UseRedoc()

api.JSONConsumer = runtime.JSONConsumer()
api.JSONProducer = runtime.JSONProducer()
api.JSONProducer = runtime.ProducerFunc(func(writer io.Writer, data interface{}) error {
enc := json.NewEncoder(writer)
enc.SetIndent("", *jsonIndent)
enc.SetEscapeHTML(false)
return enc.Encode(data)
})

api.ResultsGetResultHandler = results.GetResultHandlerFunc(server.GetResultHandler)
api.ResultsPostResultHandler = results.PostResultHandlerFunc(server.PostResultsHandler)
Expand Down

0 comments on commit 90a8d87

Please sign in to comment.