Skip to content

Commit

Permalink
Merge f63702d into 48ed634
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Mar 8, 2019
2 parents 48ed634 + f63702d commit 55c2a10
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 41 deletions.
3 changes: 2 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/mackerelio/mkr/hosts"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/mackerelio/mkr/org"
"github.com/mackerelio/mkr/plugin"
"github.com/mackerelio/mkr/wrap"
cli "gopkg.in/urfave/cli.v1"
Expand All @@ -33,7 +34,7 @@ var Commands = []cli.Command{
commandAlerts,
commandDashboards,
commandAnnotations,
commandOrg,
org.Command,
plugin.CommandPlugin,
checks.Command,
wrap.Command,
Expand Down
8 changes: 5 additions & 3 deletions hosts/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"os"
"text/template"

mkr "github.com/mackerelio/mackerel-client-go"
mackerel "github.com/mackerelio/mackerel-client-go"

"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/mackerelclient"
)

type hostApp struct {
cli *mkr.Client
cli mackerelclient.Client

verbose bool

Expand All @@ -22,7 +24,7 @@ type hostApp struct {
}

func (ha *hostApp) run() error {
hosts, err := ha.cli.FindHosts(&mkr.FindHostsParam{
hosts, err := ha.cli.FindHosts(&mackerel.FindHostsParam{
Name: ha.name,
Service: ha.service,
Roles: ha.roles,
Expand Down
3 changes: 2 additions & 1 deletion hosts/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package hosts

import (
"github.com/mackerelio/mkr/mackerelclient"
cli "gopkg.in/urfave/cli.v1"

"github.com/mackerelio/mkr/mackerelclient"
)

// Command is definition of mkr hosts subcommand
Expand Down
9 changes: 9 additions & 0 deletions mackerelclient/client.go
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)
}
62 changes: 62 additions & 0 deletions mackerelclient/mock_client.go
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
}
}
16 changes: 9 additions & 7 deletions mackerelclient/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"fmt"
"os"

cli "gopkg.in/urfave/cli.v1"

"github.com/mackerelio/mackerel-agent/config"
mkr "github.com/mackerelio/mackerel-client-go"
mackerel "github.com/mackerelio/mackerel-client-go"

"github.com/mackerelio/mkr/logger"
cli "gopkg.in/urfave/cli.v1"
)

// New returns new mackerel client
func New(conffile, apibase string) (*mkr.Client, error) {
func New(conffile, apibase string) (Client, error) {
apikey := os.Getenv("MACKEREL_APIKEY")
var conf *config.Config
if apikey == "" {
Expand All @@ -34,11 +36,11 @@ func New(conffile, apibase string) (*mkr.Client, error) {
}
apibase = conf.Apibase
}
return mkr.NewClientWithOptions(apikey, apibase, os.Getenv("DEBUG") != "")
return mackerel.NewClientWithOptions(apikey, apibase, os.Getenv("DEBUG") != "")
}

// NewFromContext returns mackerel client from cli.Context
func NewFromContext(c *cli.Context) *mkr.Client {
func NewFromContext(c *cli.Context) *mackerel.Client {
confFile := c.GlobalString("conf")
apiBase := c.GlobalString("apibase")
apiKey := LoadApikeyFromEnvOrConfig(confFile)
Expand All @@ -53,8 +55,8 @@ func NewFromContext(c *cli.Context) *mkr.Client {
apiBase = LoadApibaseFromConfigWithFallback(confFile)
}

mackerel, err := mkr.NewClientWithOptions(apiKey, apiBase, os.Getenv("DEBUG") != "")
client, err := mackerel.NewClientWithOptions(apiKey, apiBase, os.Getenv("DEBUG") != "")
logger.DieIf(err)

return mackerel
return client
}
29 changes: 0 additions & 29 deletions org.go

This file was deleted.

23 changes: 23 additions & 0 deletions org/app.go
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
}
45 changes: 45 additions & 0 deletions org/app_test.go
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())
})
}
}
32 changes: 32 additions & 0 deletions org/command.go
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()
}

0 comments on commit 55c2a10

Please sign in to comment.