Skip to content

Commit

Permalink
Merge pull request #490 from mackerelio/gojq
Browse files Browse the repository at this point in the history
added filter by gojq
  • Loading branch information
yseto committed Aug 23, 2022
2 parents 12be88b + f799bda commit 1861b72
Show file tree
Hide file tree
Showing 22 changed files with 290 additions and 392 deletions.
8 changes: 5 additions & 3 deletions alerts/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/fatih/color"
"github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/jq"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
Expand All @@ -20,7 +21,7 @@ import (
var Command = cli.Command{
Name: "alerts",
Usage: "Retrieve/Close alerts",
ArgsUsage: "[--with-closed | -w] [--limit | -l]",
ArgsUsage: "[--with-closed | -w] [--limit | -l] [--jq <formula>]",
Description: `
Retrieve/Close alerts. With no subcommand specified, this will show all alerts.
Requests APIs under "/api/v0/alerts". See https://mackerel.io/api-docs/entry/alerts .
Expand All @@ -29,6 +30,7 @@ var Command = cli.Command{
Flags: []cli.Flag{
cli.BoolFlag{Name: "with-closed, w", Usage: "Display open alert including close alert. default: false"},
cli.IntFlag{Name: "limit, l", Value: defaultAlertsLimit, Usage: fmt.Sprintf("Set the number of alerts to display. Default is set to %d when -with-closed is set, otherwise all the open alerts are displayed.", defaultAlertsLimit)},
jq.CommandLineFlag,
},
Subcommands: []cli.Command{
{
Expand Down Expand Up @@ -251,7 +253,7 @@ func doAlertsRetrieve(c *cli.Context) error {
withClosed := c.Bool("with-closed")
alerts, err := fetchAlerts(client, withClosed, getAlertsLimit(c, withClosed))
logger.DieIf(err)
err = format.PrettyPrintJSON(os.Stdout, alerts)
err = format.PrettyPrintJSON(os.Stdout, alerts, c.String("jq"))
logger.DieIf(err)
return nil
}
Expand Down Expand Up @@ -385,7 +387,7 @@ func doAlertsClose(c *cli.Context) error {

logger.Log("Alert closed", alertID)
if isVerbose {
err := format.PrettyPrintJSON(os.Stdout, alert)
err := format.PrettyPrintJSON(os.Stdout, alert, "")
logger.DieIf(err)
}
}
Expand Down
12 changes: 7 additions & 5 deletions annotations/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/jq"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
Expand Down Expand Up @@ -42,7 +43,7 @@ var Command = cli.Command{
{
Name: "list",
Usage: "list annotations",
ArgsUsage: "--from <from> --to <to> --service|-s <service>",
ArgsUsage: "--from <from> --to <to> --service|-s <service> [--jq <formula>]",
Description: `
Shows annotations by service name and duration (from and to)
`,
Expand All @@ -51,6 +52,7 @@ var Command = cli.Command{
cli.StringFlag{Name: "service, s", Usage: "Service name for annotation"},
cli.IntFlag{Name: "from", Usage: "Starting time (epoch seconds)"},
cli.IntFlag{Name: "to", Usage: "Ending time (epoch seconds)"},
jq.CommandLineFlag,
},
},
{
Expand Down Expand Up @@ -128,7 +130,7 @@ func doAnnotationsCreate(c *cli.Context) error {
Roles: roles,
})
logger.DieIf(err)
err = format.PrettyPrintJSON(os.Stdout, annotation)
err = format.PrettyPrintJSON(os.Stdout, annotation, "")
logger.DieIf(err)
return nil
}
Expand Down Expand Up @@ -156,7 +158,7 @@ func doAnnotationsList(c *cli.Context) error {
client := mackerelclient.NewFromContext(c)
annotations, err := client.FindGraphAnnotations(service, from, to)
logger.DieIf(err)
err = format.PrettyPrintJSON(os.Stdout, annotations)
err = format.PrettyPrintJSON(os.Stdout, annotations, c.String("jq"))
logger.DieIf(err)
return nil
}
Expand Down Expand Up @@ -200,7 +202,7 @@ func doAnnotationsUpdate(c *cli.Context) error {
Roles: roles,
})
logger.DieIf(err)
err = format.PrettyPrintJSON(os.Stdout, annotation)
err = format.PrettyPrintJSON(os.Stdout, annotation, "")
logger.DieIf(err)
return nil
}
Expand All @@ -216,7 +218,7 @@ func doAnnotationsDelete(c *cli.Context) error {
client := mackerelclient.NewFromContext(c)
annotation, err := client.DeleteGraphAnnotation(annotationID)
logger.DieIf(err)
err = format.PrettyPrintJSON(os.Stdout, annotation)
err = format.PrettyPrintJSON(os.Stdout, annotation, "")
logger.DieIf(err)
return nil
}
3 changes: 2 additions & 1 deletion aws_integrations/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type awsIntegrationsApp struct {
client mackerelclient.Client
outStream io.Writer
jqFilter string
}

func (app *awsIntegrationsApp) run() error {
Expand All @@ -19,7 +20,7 @@ func (app *awsIntegrationsApp) run() error {
return err
}

err = format.PrettyPrintJSON(app.outStream, awsIntegrations)
err = format.PrettyPrintJSON(app.outStream, awsIntegrations, app.jqFilter)
logger.DieIf(err)
return nil
}
7 changes: 6 additions & 1 deletion aws_integrations/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ package aws_integrations
import (
"os"

"github.com/mackerelio/mkr/jq"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
)

var Command = cli.Command{
Name: "aws-integrations",
Usage: "List aws integration settings",
ArgsUsage: "",
ArgsUsage: "[--jq <formula>]",
Description: `
List the information of the aws integration settings.
Requests "GET /api/v0/aws-integrations". See https://mackerel.io/api-docs/entry/aws-integration#list.
`,
Action: doAWSIntegrations,
Flags: []cli.Flag{
jq.CommandLineFlag,
},
}

func doAWSIntegrations(c *cli.Context) error {
Expand All @@ -26,5 +30,6 @@ func doAWSIntegrations(c *cli.Context) error {
return (&awsIntegrationsApp{
client: client,
outStream: os.Stdout,
jqFilter: c.String("jq"),
}).run()
}
5 changes: 3 additions & 2 deletions channels/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type channelsApp struct {
client mackerelclient.Client
outStream io.Writer
jqFilter string
}

func (app *channelsApp) run() error {
Expand All @@ -23,7 +24,7 @@ func (app *channelsApp) run() error {
return err
}

err = format.PrettyPrintJSON(app.outStream, channels)
err = format.PrettyPrintJSON(app.outStream, channels, app.jqFilter)
logger.DieIf(err)
return nil
}
Expand All @@ -41,7 +42,7 @@ func (app *channelsApp) pullChannels(isVerbose bool, optFilePath string) error {
logger.DieIf(err)

if isVerbose {
err := format.PrettyPrintJSON(os.Stdout, channels)
err := format.PrettyPrintJSON(os.Stdout, channels, "")
logger.DieIf(err)
}

Expand Down
5 changes: 5 additions & 0 deletions channels/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package channels
import (
"os"

"github.com/mackerelio/mkr/jq"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
)
Expand All @@ -16,6 +17,9 @@ var Command = cli.Command{
Requests APIs under "/api/v0/channels". See https://mackerel.io/api-docs/entry/channels .
`,
Action: doChannels,
Flags: []cli.Flag{
jq.CommandLineFlag,
},
Subcommands: []cli.Command{
{
Name: "pull",
Expand All @@ -42,6 +46,7 @@ func doChannels(c *cli.Context) error {
return (&channelsApp{
client: client,
outStream: os.Stdout,
jqFilter: c.String("jq"),
}).run()
}

Expand Down
13 changes: 9 additions & 4 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/mackerelio/mkr/jq"
"github.com/mackerelio/mkr/logger"
)

Expand All @@ -23,10 +24,14 @@ type Host struct {
IPAddresses map[string]string `json:"ipAddresses,omitempty"`
}

// PrettyPrintJSON output indented json via stdout.
func PrettyPrintJSON(outStream io.Writer, src interface{}) error {
_, err := fmt.Fprintln(outStream, JSONMarshalIndent(src, "", " "))
return err
// PrettyPrintJSON outputs JSON or filtered result by jq query via stdout.
func PrettyPrintJSON(outStream io.Writer, src interface{}, query string) error {
if query == "" {
_, err := fmt.Fprintln(outStream, JSONMarshalIndent(src, "", " "))
return err
}

return jq.FilterJSON(outStream, src, query)
}

// JSONMarshalIndent call json.MarshalIndent and replace encoded angle brackets
Expand Down
82 changes: 82 additions & 0 deletions format/format_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package format

import (
"bytes"
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestISO8601Extended(t *testing.T) {
Expand All @@ -13,3 +17,81 @@ func TestISO8601Extended(t *testing.T) {
t.Errorf("should be %q got %q", expect, got)
}
}

func TestPrettyPrintJSON(t *testing.T) {
testCases := []struct {
id string
srcJson string
query string
expected string
}{
{
id: "simple",
srcJson: `{"id": 1, "string": "foo"}`,
expected: `{
"id": 1,
"string": "foo"
}
`,
},
{
id: "simple with query",
srcJson: `{"id": 1, "string": "foo"}`,
expected: "1\n",
query: ".id",
},
{
id: "array",
srcJson: `[{"id": 1, "string": "foo"},{"id": 2, "string": "bar"}]`,
expected: `[
{
"id": 1,
"string": "foo"
},
{
"id": 2,
"string": "bar"
}
]
`,
},
{
id: "array with query",
srcJson: `[{"id": 1, "string": "foo"},{"id": 2, "string": "bar"}]`,
expected: "1\n2\n",
query: ".[] | .id",
},
{
id: "array with select object",
srcJson: `[{"id": 1, "string": "foo"},{"id": 2, "string": "bar"}]`,
expected: `{"id":2,"string":"bar"}
`,
query: ".[] | select(.id == 2)",
},
{
id: "array with select value",
srcJson: `[{"id": 1, "string": "foo"},{"id": 2, "string": "bar"}]`,
expected: "bar\n",
query: ".[] | select(.id == 2) | .string",
},
{
id: "simple with brackets",
srcJson: `{"id": 1, "string": "\u003cfoo\u003e"}`,
expected: `{
"id": 1,
"string": "<foo>"
}
`,
},
}

for _, tc := range testCases {
t.Run(tc.id, func(t *testing.T) {
out := new(bytes.Buffer)
var src interface{}
assert.NoError(t, json.Unmarshal([]byte(tc.srcJson), &src))
assert.NoError(t, PrettyPrintJSON(out, src, tc.query))
assert.Equal(t, tc.expected, out.String())
})
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/fatih/color v1.13.0
github.com/golangci/golangci-lint v1.47.1
github.com/google/go-github v17.0.0+incompatible
github.com/itchyny/gojq v0.12.8
github.com/jpillora/backoff v1.0.0
github.com/mackerelio/checkers v0.0.0-20190411030116-60cbd7b55456
github.com/mackerelio/mackerel-agent v0.72.14
Expand Down Expand Up @@ -98,6 +99,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/itchyny/timefmt-go v0.1.3 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect
Expand All @@ -119,7 +121,7 @@ require (
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mbilski/exhaustivestruct v1.2.0 // indirect
github.com/mgechev/revive v1.2.1 // indirect
Expand Down Expand Up @@ -148,6 +150,7 @@ require (
github.com/quasilyte/gogrep v0.0.0-20220120141003-628d8b3623b5 // indirect
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 // indirect
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryancurrah/gomodguard v1.2.3 // indirect
github.com/ryanrolds/sqlclosecheck v0.3.0 // indirect
Expand Down

0 comments on commit 1861b72

Please sign in to comment.