-
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.
- Loading branch information
Showing
10 changed files
with
189 additions
and
41 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,9 @@ | ||
package mackerelclient | ||
|
||
import mackerel "github.com/mackerelio/mackerel-client-go" | ||
|
||
// Client represents a client of Mackerel API | ||
type Client interface { | ||
FindHosts(param *mackerel.FindHostsParam) ([]*mackerel.Host, error) | ||
GetOrg() (*mackerel.Org, error) | ||
} |
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,62 @@ | ||
package mackerelclient | ||
|
||
import mackerel "github.com/mackerelio/mackerel-client-go" | ||
|
||
// MockClient represents a mock client of Mackerel API | ||
type MockClient struct { | ||
findHostsCallback func(param *mackerel.FindHostsParam) ([]*mackerel.Host, error) | ||
getOrgCallback func() (*mackerel.Org, error) | ||
} | ||
|
||
// MockClientOption represents an option of mock client of Mackerel API | ||
type MockClientOption func(*MockClient) | ||
|
||
// NewMockClient creates a new mock client of Mackerel API | ||
func NewMockClient(opts ...MockClientOption) *MockClient { | ||
client := &MockClient{} | ||
for _, opt := range opts { | ||
client.ApplyOption(opt) | ||
} | ||
return client | ||
} | ||
|
||
// ApplyOption applies a mock client option | ||
func (c *MockClient) ApplyOption(opt MockClientOption) { | ||
opt(c) | ||
} | ||
|
||
type errCallbackNotFound string | ||
|
||
func (err errCallbackNotFound) Error() string { | ||
return string(err) + " callback not found" | ||
} | ||
|
||
// FindHosts ... | ||
func (c *MockClient) FindHosts(param *mackerel.FindHostsParam) ([]*mackerel.Host, error) { | ||
if c.findHostsCallback != nil { | ||
return c.findHostsCallback(param) | ||
} | ||
return nil, errCallbackNotFound("FindHosts") | ||
} | ||
|
||
// MockFindHosts returns an option to set the callback of FindHosts | ||
func MockFindHosts(callback func(param *mackerel.FindHostsParam) ([]*mackerel.Host, error)) MockClientOption { | ||
return func(c *MockClient) { | ||
c.findHostsCallback = callback | ||
} | ||
} | ||
|
||
// GetOrg ... | ||
func (c *MockClient) GetOrg() (*mackerel.Org, error) { | ||
if c.getOrgCallback != nil { | ||
return c.getOrgCallback() | ||
} | ||
return nil, errCallbackNotFound("GetOrg") | ||
} | ||
|
||
// MockGetOrg returns an option to set the callback of GetOrg | ||
func MockGetOrg(callback func() (*mackerel.Org, error)) MockClientOption { | ||
return func(c *MockClient) { | ||
c.getOrgCallback = callback | ||
} | ||
} |
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() | ||
} |