From 77bdf5131e0696a7f601487238b3ca8455f35c31 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 19 May 2024 22:27:30 +0900 Subject: [PATCH 1/2] add --jq option to org subcommand --- org/app.go | 3 ++- org/app_test.go | 28 ++++++++++++++++++++++++++-- org/command.go | 10 ++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/org/app.go b/org/app.go index 450142ab..4af28db2 100644 --- a/org/app.go +++ b/org/app.go @@ -11,6 +11,7 @@ import ( type orgApp struct { client mackerelclient.Client outStream io.Writer + jqFilter string } func (app *orgApp) run() error { @@ -19,7 +20,7 @@ func (app *orgApp) run() error { return err } - err = format.PrettyPrintJSON(app.outStream, org, "") + err = format.PrettyPrintJSON(app.outStream, org, app.jqFilter) logger.DieIf(err) return nil } diff --git a/org/app_test.go b/org/app_test.go index 6e7a3b4b..7a3ba52d 100644 --- a/org/app_test.go +++ b/org/app_test.go @@ -15,14 +15,36 @@ func TestOrgApp_Run(t *testing.T) { testCases := []struct { id string org *mackerel.Org + jqFilter string expected string }{ { - id: "default", - org: &mackerel.Org{Name: "sample-org"}, + id: "default", + org: &mackerel.Org{Name: "sample-org"}, + jqFilter: "", expected: `{ "name": "sample-org" } +`, + }, + { + id: "jq_orgName", + org: &mackerel.Org{Name: "sample-org"}, + jqFilter: ".name", + expected: `sample-org +`, + }, + { + id: "jq_emptyDisplayName", + org: &mackerel.Org{Name: "sample-org"}, + jqFilter: ".displayName", + expected: "\n", + }, + { + id: "jq_displayName", + org: &mackerel.Org{Name: "sample-org", DisplayName: "Sample Org"}, + jqFilter: ".displayName", + expected: `Sample Org `, }, } @@ -34,9 +56,11 @@ func TestOrgApp_Run(t *testing.T) { ) t.Run(tc.id, func(t *testing.T) { out := new(bytes.Buffer) + jqFilter := tc.jqFilter app := &orgApp{ client: client, outStream: out, + jqFilter: jqFilter, } assert.NoError(t, app.run()) assert.Equal(t, tc.expected, out.String()) diff --git a/org/command.go b/org/command.go index 60bd2d1d..4ddad8bc 100644 --- a/org/command.go +++ b/org/command.go @@ -3,19 +3,24 @@ package org import ( "os" + "github.com/mackerelio/mkr/jq" "github.com/mackerelio/mkr/mackerelclient" "github.com/urfave/cli" ) // Command is the definition of org subcommand var Command = cli.Command{ - Name: "org", - Usage: "Fetch organization", + Name: "org", + Usage: "Fetch organization", + ArgsUsage: "--jq ", Description: ` Fetch organization. Requests APIs under "/api/v0/org". See https://mackerel.io/api-docs/entry/organizations . `, Action: doOrg, + Flags: []cli.Flag{ + jq.CommandLineFlag, + }, } func doOrg(c *cli.Context) error { @@ -27,5 +32,6 @@ func doOrg(c *cli.Context) error { return (&orgApp{ client: client, outStream: os.Stdout, + jqFilter: c.String("jq"), }).run() } From 4972c3977ebdb72223f0e248e241fef81937f997 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 20 May 2024 19:46:28 +0900 Subject: [PATCH 2/2] --jq should be option --- org/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org/command.go b/org/command.go index 4ddad8bc..e4365b06 100644 --- a/org/command.go +++ b/org/command.go @@ -12,7 +12,7 @@ import ( var Command = cli.Command{ Name: "org", Usage: "Fetch organization", - ArgsUsage: "--jq ", + ArgsUsage: "[--jq ]", Description: ` Fetch organization. Requests APIs under "/api/v0/org". See https://mackerel.io/api-docs/entry/organizations .