diff --git a/client/client.go b/client/client.go index b97c654ab..299140a82 100644 --- a/client/client.go +++ b/client/client.go @@ -2,7 +2,6 @@ package client import ( "context" - "encoding/json" "github.com/circleci/circleci-cli/logger" "github.com/machinebox/graphql" @@ -31,7 +30,7 @@ func NewClient(endpoint string, token string, logger *logger.Logger) *Client { // Run will construct a request using graphql.NewRequest. // Then it will execute the given query using graphql.Client.Run. // This function will return the unmarshalled response as JSON. -func (c *Client) Run(query string) (string, error) { +func (c *Client) Run(query string) (map[string]interface{}, error) { req := graphql.NewRequest(query) req.Header.Set("Authorization", c.token) @@ -40,13 +39,5 @@ func (c *Client) Run(query string) (string, error) { c.logger.Debug("Querying %s with:\n\n%s\n\n", c.endpoint, query) err := c.client.Run(ctx, req, &resp) - if err != nil { - return "", err - } - - b, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return "", err - } - return string(b), err + return resp, err } diff --git a/cmd/query.go b/cmd/query.go index d4d5634d0..f3a674e9b 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -28,5 +28,5 @@ func query(cmd *cobra.Command, args []string) { Logger.FatalOnError("Error occurred when running query", err) } - Logger.Infoln(resp) + Logger.Prettyify(resp) } diff --git a/logger/logger.go b/logger/logger.go index f46910999..c95c919a1 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,6 +1,7 @@ package logger import ( + "encoding/json" "log" "os" ) @@ -71,3 +72,13 @@ func (l *Logger) FatalOnError(msg string, err error) { l.error.Fatalln(msg, err.Error()) } } + +// Prettyify accepts a map fo data and pretty prints it. +// It's using json.MarshalIndent and printing with log.Logger.Infoln +func (l *Logger) Prettyify(data map[string]interface{}) { + bytes, err := json.MarshalIndent(data, "", " ") + if err != nil { + l.error.Fatalln(err) + } + l.Infoln(string(bytes)) +}