Merge pull request #255 from sosedoff/pretty-json-default

Pretty-print JSON results by default
This commit is contained in:
Dan Sosedoff
2017-09-13 23:42:19 -05:00
committed by GitHub
2 changed files with 30 additions and 21 deletions

View File

@@ -8,6 +8,8 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"time" "time"
"github.com/sosedoff/pgweb/pkg/command"
) )
type Row []interface{} type Row []interface{}
@@ -108,7 +110,13 @@ func (res *Result) CSV() []byte {
} }
func (res *Result) JSON() []byte { func (res *Result) JSON() []byte {
data, _ := json.Marshal(res.Format()) var data []byte
if command.Opts.EnablePrettyJson {
data, _ = json.MarshalIndent(res.Format(), "", " ")
} else {
data, _ = json.Marshal(res.Format())
}
return data return data
} }

View File

@@ -28,6 +28,7 @@ type Options struct {
LockSession bool `long:"lock-session" description:"Lock session to a single database connection" default:"false"` LockSession bool `long:"lock-session" description:"Lock session to a single database connection" default:"false"`
Bookmark string `short:"b" long:"bookmark" description:"Bookmark to use for connection. Bookmark files are stored under $HOME/.pgweb/bookmarks/*.toml" default:""` Bookmark string `short:"b" long:"bookmark" description:"Bookmark to use for connection. Bookmark files are stored under $HOME/.pgweb/bookmarks/*.toml" default:""`
BookmarksDir string `long:"bookmarks-dir" description:"Overrides default directory for bookmark files to search" default:""` BookmarksDir string `long:"bookmarks-dir" description:"Overrides default directory for bookmark files to search" default:""`
EnablePrettyJson bool `long:"feature-pretty-json" description:"Format JSON output" default:"true"`
} }
var Opts Options var Opts Options