-
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 #198 from mackerelio/add-services-package
Create services package and add tests for services subcommand
- Loading branch information
Showing
6 changed files
with
164 additions
and
22 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 services | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/mackerelio/mkr/format" | ||
"github.com/mackerelio/mkr/mackerelclient" | ||
) | ||
|
||
type servicesApp struct { | ||
client mackerelclient.Client | ||
outStream io.Writer | ||
} | ||
|
||
func (app *servicesApp) run() error { | ||
services, err := app.client.FindServices() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format.PrettyPrintJSON(app.outStream, services) | ||
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,87 @@ | ||
package services | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
mackerel "github.com/mackerelio/mackerel-client-go" | ||
|
||
"github.com/mackerelio/mkr/mackerelclient" | ||
) | ||
|
||
func TestServicesApp_Run(t *testing.T) { | ||
testCases := []struct { | ||
id string | ||
services []*mackerel.Service | ||
expected string | ||
}{ | ||
{ | ||
id: "default", | ||
services: []*mackerel.Service{ | ||
&mackerel.Service{ | ||
Name: "sample-service-1", | ||
Memo: "sample memo 1", | ||
Roles: []string{"role1", "role2", "role3"}, | ||
}, | ||
&mackerel.Service{ | ||
Name: "sample-service-2", | ||
Memo: "sample memo 2", | ||
Roles: []string{"role"}, | ||
}, | ||
&mackerel.Service{ | ||
Name: "sample-service-3", | ||
Memo: "", | ||
Roles: []string{}, | ||
}, | ||
}, | ||
expected: `[ | ||
{ | ||
"name": "sample-service-1", | ||
"memo": "sample memo 1", | ||
"roles": [ | ||
"role1", | ||
"role2", | ||
"role3" | ||
] | ||
}, | ||
{ | ||
"name": "sample-service-2", | ||
"memo": "sample memo 2", | ||
"roles": [ | ||
"role" | ||
] | ||
}, | ||
{ | ||
"name": "sample-service-3", | ||
"memo": "", | ||
"roles": [] | ||
} | ||
] | ||
`, | ||
}, | ||
{ | ||
id: "no services", | ||
services: []*mackerel.Service{}, | ||
expected: `[] | ||
`, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
client := mackerelclient.NewMockClient( | ||
mackerelclient.MockFindServices(func() ([]*mackerel.Service, error) { | ||
return tc.services, nil | ||
}), | ||
) | ||
t.Run(tc.id, func(t *testing.T) { | ||
out := new(bytes.Buffer) | ||
app := &servicesApp{ | ||
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,33 @@ | ||
package services | ||
|
||
import ( | ||
"os" | ||
|
||
cli "gopkg.in/urfave/cli.v1" | ||
|
||
"github.com/mackerelio/mkr/mackerelclient" | ||
) | ||
|
||
// Command is the definition of services subcommand | ||
var Command = cli.Command{ | ||
Name: "services", | ||
Usage: "List services", | ||
ArgsUsage: "", | ||
Description: ` | ||
List the information of the services. | ||
Requests "GET /api/v0/services". See https://mackerel.io/api-docs/entry/services#list. | ||
`, | ||
Action: doServices, | ||
} | ||
|
||
func doServices(c *cli.Context) error { | ||
client, err := mackerelclient.New(c.GlobalString("conf"), c.GlobalString("apibase")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return (&servicesApp{ | ||
client: client, | ||
outStream: os.Stdout, | ||
}).run() | ||
} |