-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): Add organization list command
Added the organization list command, requested in #29
- Loading branch information
Johann
authored
Jun 28, 2019
1 parent
5bbf49a
commit 5c95715
Showing
13 changed files
with
216 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Contentful CLI - Organization command | ||
|
||
## Available subcommands: | ||
|
||
* [list](./list) - List all Organizations you have access to |
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,8 @@ | ||
# Contentful CLI - `space list` command | ||
|
||
Lists all Organizations you have access to. | ||
|
||
## Example | ||
```sh | ||
contentful organization list | ||
``` |
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,10 @@ | ||
export const command = 'organization' | ||
|
||
export const desc = 'Manage and list your organizations' | ||
|
||
export const builder = function (yargs) { | ||
return yargs | ||
.usage('') | ||
.commandDir('organization_cmds') | ||
.demandCommand(4, 'Please specify a sub command.') | ||
} |
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,54 @@ | ||
import Table from 'cli-table3' | ||
|
||
import { handleAsyncError as handle } from '../../utils/async' | ||
import { createManagementClient } from '../../utils/contentful-clients' | ||
import { log } from '../../utils/log' | ||
import paginate from '../../utils/pagination' | ||
|
||
export const command = 'list' | ||
|
||
export const desc = 'List your organizations' | ||
|
||
export const builder = yargs => { | ||
return yargs | ||
.usage('Usage: contentful organization list') | ||
.option('management-token', { | ||
alias: 'mt', | ||
describe: 'Contentful management API token', | ||
type: 'string' | ||
}) | ||
.epilog( | ||
[ | ||
'See more at:', | ||
'https://github.com/contentful/contentful-cli/tree/master/docs/organization/list', | ||
'Copyright 2018 Contentful, this is a BETA release' | ||
].join('\n') | ||
) | ||
} | ||
|
||
export const aliases = ['ls'] | ||
|
||
export async function organizationList ({ context }) { | ||
const { managementToken } = context | ||
|
||
const client = await createManagementClient({ | ||
accessToken: managementToken, | ||
feature: 'organization-list' | ||
}) | ||
|
||
const result = await paginate({ client, method: 'getOrganizations' }) | ||
|
||
const organizations = result.items.sort((a, b) => a.name.localeCompare(b.name)) | ||
|
||
const table = new Table({ | ||
head: ['Organization name', 'Organization id'] | ||
}) | ||
|
||
organizations.forEach(({ sys, name }) => { | ||
table.push([name, sys.id]) | ||
}) | ||
|
||
log(table.toString()) | ||
} | ||
|
||
export const handler = handle(organizationList) |
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
13 changes: 13 additions & 0 deletions
13
test/integration/cmds/organization/__snapshots__/list.test.js.snap
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,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should print help message: help data is incorrect 1`] = ` | ||
"Usage: contentful organization list | ||
Options: | ||
-h, --help Show help [boolean] | ||
--management-token, --mt Contentful management API token [string] | ||
See more at: | ||
https://github.com/contentful/contentful-cli/tree/master/docs/organization/list | ||
Copyright 2018 Contentful, this is a BETA release" | ||
`; |
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,38 @@ | ||
import nixt from 'nixt' | ||
import { join } from 'path' | ||
import { initConfig } from '../../util' | ||
|
||
const bin = join(__dirname, './../../../../', 'bin') | ||
const org = process.env.CLI_E2E_ORG_ID | ||
|
||
const app = () => { | ||
return nixt({ newlines: true }) | ||
.cwd(bin) | ||
.base('./contentful.js ') | ||
.clone() | ||
} | ||
|
||
beforeAll(() => { | ||
return initConfig() | ||
}) | ||
|
||
test('should print help message', done => { | ||
app() | ||
.run('organization list --help') | ||
.code(0) | ||
.expect(result => { | ||
const resultText = result.stdout.trim() | ||
expect(resultText).toMatchSnapshot('help data is incorrect') | ||
}) | ||
.end(done) | ||
}) | ||
|
||
test('should list organizations', done => { | ||
app() | ||
.run('organization list') | ||
.code(0) | ||
.stdout(/(Organization name)|(Organization id)/) | ||
.stdout(RegExp(org)) | ||
.stdout(/Contentful CLI Test Org/) | ||
.end(done) | ||
}) |
19 changes: 19 additions & 0 deletions
19
test/integration/cmds/organization/snapshots/list.test.js.md
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,19 @@ | ||
# Snapshot report for `test/integration/cmds/organization/list.test.js` | ||
|
||
The actual snapshot is saved in `list.test.js.snap`. | ||
|
||
Generated by [AVA](https://ava.li). | ||
|
||
## should print help message | ||
|
||
> help data is incorrect | ||
`Usage: contentful organization list␊ | ||
␊ | ||
Options:␊ | ||
-h, --help Show help [boolean]␊ | ||
--management-token Contentful management API token [string]␊ | ||
␊ | ||
See more at:␊ | ||
https://github.com/contentful/contentful-cli/tree/master/docs/organization/list␊ | ||
Copyright 2018 Contentful, this is a BETA release` |
Binary file not shown.
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,58 @@ | ||
import { organizationList } from '../../../../lib/cmds/organization_cmds/list' | ||
import { createManagementClient } from '../../../../lib/utils/contentful-clients' | ||
import { log } from '../../../../lib/utils/log' | ||
|
||
jest.mock('../../../../lib/context') | ||
jest.mock('../../../../lib/utils/contentful-clients') | ||
jest.mock('../../../../lib/utils/log') | ||
|
||
const organizationData = { | ||
items: [ | ||
{ | ||
sys: { | ||
type: 'Organization', | ||
id: '0D9ZC8rLWiw6x5qizZGiRs', | ||
version: '1', | ||
createdAt: '2015-05-18T11:29:46.809Z', | ||
updatedAt: '2015-05-18T11:29:46.809Z' | ||
}, | ||
name: 'My organization' | ||
}, | ||
{ | ||
sys: { | ||
type: 'Organization 2', | ||
id: 'ewr1k12341lk4123332131', | ||
version: '1', | ||
createdAt: '2015-05-18T11:29:46.809Z', | ||
updatedAt: '2015-05-18T11:29:46.809Z' | ||
}, | ||
name: 'My second organization' | ||
} | ||
] | ||
} | ||
|
||
const fakeClient = { | ||
getOrganizations: async () => organizationData | ||
} | ||
createManagementClient.mockResolvedValue(fakeClient) | ||
|
||
afterEach(() => { | ||
createManagementClient.mockClear() | ||
log.mockClear() | ||
}) | ||
|
||
test('list organizations', async () => { | ||
await organizationList({ | ||
context: { | ||
managementToken: 'managementToken' | ||
} | ||
}) | ||
|
||
expect(createManagementClient).toHaveBeenCalledTimes(1) | ||
const [result] = log.mock.calls[0] | ||
const [org1, org2] = organizationData.items | ||
expect(result).toContain(org1.name) | ||
expect(result).toContain(org1.sys.id) | ||
expect(result).toContain(org2.name) | ||
expect(result).toContain(org2.sys.id) | ||
}) |