Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --jq option to org sub command #646

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>]",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

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()
}