Skip to content

Commit

Permalink
Restore behavior of (*Client).Run() to accept a map of data
Browse files Browse the repository at this point in the history
Also add a (*Logger).Prettyify to pretty print that map
  • Loading branch information
Zachary Scott committed Jun 12, 2018
1 parent a150d3e commit 04eb7a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
13 changes: 2 additions & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"context"
"encoding/json"

"github.com/circleci/circleci-cli/logger"
"github.com/machinebox/graphql"
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"encoding/json"
"log"
"os"
)
Expand Down Expand Up @@ -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))
}

0 comments on commit 04eb7a9

Please sign in to comment.