Skip to content

Commit

Permalink
feat(commands): Add organization list command
Browse files Browse the repository at this point in the history
Added the organization list command, requested in #29
  • Loading branch information
Johann authored Jun 28, 2019
1 parent 5bbf49a commit 5c95715
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
* [guide](./guide) - Getting started with Contentful
* [config](./config) - Config related commands
* [space](./space) - Space related commands
* [organization](./organization) - Organization related commands
* [content-type](./content-type) - Content Type related commands
* [extension](./extension) - UI Extension related commands
5 changes: 5 additions & 0 deletions docs/organization/README.md
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
8 changes: 8 additions & 0 deletions docs/organization/list/README.md
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
```
10 changes: 10 additions & 0 deletions lib/cmds/organization.js
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.')
}
54 changes: 54 additions & 0 deletions lib/cmds/organization_cmds/list.js
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)
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default {
'config list',
'config remove',
'space create',
'space list'
'space list',
'organization list'
]
}
4 changes: 4 additions & 0 deletions test/e2e/__snapshots__/basics.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand All @@ -33,6 +34,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand All @@ -54,6 +56,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand All @@ -77,6 +80,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand Down
4 changes: 4 additions & 0 deletions test/integration/cmds/__snapshots__/main.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand All @@ -31,6 +32,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand All @@ -52,6 +54,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand All @@ -72,6 +75,7 @@ Commands:
guide A guide introducing basic concepts of working with Contentful
login Login to Contentful
logout Logout from Contentful
organization Manage and list your organizations
space Manage and list your spaces
Options:
Expand Down
13 changes: 13 additions & 0 deletions test/integration/cmds/organization/__snapshots__/list.test.js.snap
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"
`;
38 changes: 38 additions & 0 deletions test/integration/cmds/organization/list.test.js
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 test/integration/cmds/organization/snapshots/list.test.js.md
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.
58 changes: 58 additions & 0 deletions test/unit/cmds/organization_cmds/list.test.js
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)
})

0 comments on commit 5c95715

Please sign in to comment.