-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from mackerelio/add-org-package
Create org package and add tests for org subcommand
- Loading branch information
Showing
7 changed files
with
119 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/mackerelio/mkr/format" | ||
"github.com/mackerelio/mkr/mackerelclient" | ||
) | ||
|
||
type orgApp struct { | ||
client mackerelclient.Client | ||
outStream io.Writer | ||
} | ||
|
||
func (app *orgApp) run() error { | ||
org, err := app.client.GetOrg() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format.PrettyPrintJSON(app.outStream, org) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
mackerel "github.com/mackerelio/mackerel-client-go" | ||
|
||
"github.com/mackerelio/mkr/mackerelclient" | ||
) | ||
|
||
func TestOrgApp_Run(t *testing.T) { | ||
testCases := []struct { | ||
id string | ||
org *mackerel.Org | ||
expected string | ||
}{ | ||
{ | ||
id: "default", | ||
org: &mackerel.Org{Name: "sample-org"}, | ||
expected: `{ | ||
"name": "sample-org" | ||
} | ||
`, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
client := mackerelclient.NewMockClient( | ||
mackerelclient.MockGetOrg(func() (*mackerel.Org, error) { | ||
return tc.org, nil | ||
}), | ||
) | ||
t.Run(tc.id, func(t *testing.T) { | ||
out := new(bytes.Buffer) | ||
app := &orgApp{ | ||
client: client, | ||
outStream: out, | ||
} | ||
assert.NoError(t, app.run()) | ||
assert.Equal(t, tc.expected, out.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org | ||
|
||
import ( | ||
"os" | ||
|
||
cli "gopkg.in/urfave/cli.v1" | ||
|
||
"github.com/mackerelio/mkr/mackerelclient" | ||
) | ||
|
||
// Command is the definition of org subcommand | ||
var Command = cli.Command{ | ||
Name: "org", | ||
Usage: "Fetch organization", | ||
Description: ` | ||
Fetch organization. | ||
Requests APIs under "/api/v0/org". See https://mackerel.io/api-docs/entry/organizations . | ||
`, | ||
Action: doOrg, | ||
} | ||
|
||
func doOrg(c *cli.Context) error { | ||
client, err := mackerelclient.New(c.GlobalString("conf"), c.GlobalString("apibase")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return (&orgApp{ | ||
client: client, | ||
outStream: os.Stdout, | ||
}).run() | ||
} |