Skip to content

Commit

Permalink
Merge pull request #646 from mackerelio/add-jq-to-org
Browse files Browse the repository at this point in the history
Add `--jq` option to `org` sub command
  • Loading branch information
kmuto authored May 28, 2024
2 parents 8fb7fcc + 4972c39 commit 5c92a0d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion org/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type orgApp struct {
client mackerelclient.Client
outStream io.Writer
jqFilter string
}

func (app *orgApp) run() error {
Expand All @@ -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
}
28 changes: 26 additions & 2 deletions org/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
},
}
Expand All @@ -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())
Expand Down
10 changes: 8 additions & 2 deletions org/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <formula>]",
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 {
Expand All @@ -27,5 +32,6 @@ func doOrg(c *cli.Context) error {
return (&orgApp{
client: client,
outStream: os.Stdout,
jqFilter: c.String("jq"),
}).run()
}

0 comments on commit 5c92a0d

Please sign in to comment.