Skip to content

Commit

Permalink
feat(context): Use middleware to handle assertions (auth and space-id)
Browse files Browse the repository at this point in the history
The assertions (assertLoggedIn & assertSpaceIdProvided) that were done on command level are now moved to a global middleware. Every command is checked for the two functions, except if it is added in the whitelists in lib/config.js.
  • Loading branch information
Johann authored Jun 28, 2019
1 parent 2932353 commit 5bbf49a
Show file tree
Hide file tree
Showing 42 changed files with 168 additions and 279 deletions.
6 changes: 4 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import yargs from 'yargs'
import {log} from './utils/log'
import {buildContext} from './utils/middlewares'
import {buildContext, getCommand, assertContext} from './utils/middlewares'
import {version} from '../package.json'

yargs.usage('\nUsage: contentful <cmd> [args]')
.commandDir('cmds')
.middleware([
buildContext
getCommand,
buildContext,
assertContext
])
.scriptName('')
.demandCommand(4, 'Please specify a command.')
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/boilerplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { successEmoji } from '../utils/emojis'
import { frame } from '../utils/text'
import { accessTokenCreate } from './space_cmds/accesstoken_cmds/create'

import { assertLoggedIn, assertSpaceIdProvided } from '../utils/assertions'

export const command = 'boilerplate'

export const desc = 'Download a boilerplate'
Expand Down Expand Up @@ -44,10 +42,7 @@ async function getBoilerplates () {
}
}

export async function downloadBoilerplate (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)
export async function downloadBoilerplate ({ context }) {
const { activeSpaceId } = context

const boilerplatesResult = await getBoilerplates()
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/content-type_cmds/get.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Table from 'cli-table3'

import { createManagementClient } from '../../utils/contentful-clients'
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { log } from '../../utils/log'
import { handleAsyncError as handle } from '../../utils/async'
import { getId } from '../../utils/helpers'
Expand All @@ -20,12 +19,8 @@ export const builder = (yargs) => {
}

async function ctShow (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

const contentTypeId = getId(argv)
const { managementToken, activeSpaceId, activeEnvironmentId } = context
const { managementToken, activeSpaceId, activeEnvironmentId } = argv.context

const client = await createManagementClient({
accessToken: managementToken,
Expand Down
6 changes: 1 addition & 5 deletions lib/cmds/content-type_cmds/list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Table from 'cli-table3'

import { createManagementClient } from '../../utils/contentful-clients'
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { log } from '../../utils/log'
import paginate from '../../utils/pagination'
Expand Down Expand Up @@ -30,10 +29,7 @@ export const builder = yargs => {
.epilog('Copyright 2018 Contentful, this is a BETA release')
}

async function ctList ({context}) {
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

async function ctList ({ context }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/extension_cmds/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { createManagementClient } from '../../utils/contentful-clients'
import { successEmoji } from '../../utils/emojis'
Expand Down Expand Up @@ -53,10 +52,6 @@ export async function createExtension (environment, data) {
}

export async function createExtensionHandler (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

const data = await prepareData(argv)

await assertExtensionValuesProvided(data, 'create')
Expand All @@ -65,7 +60,7 @@ export async function createExtensionHandler (argv) {
await readSrcDocFile(data.extension)
}

const { managementToken, activeSpaceId, activeEnvironmentId } = context
const { managementToken, activeSpaceId, activeEnvironmentId } = argv.context

const client = await createManagementClient({
accessToken: managementToken,
Expand Down
6 changes: 1 addition & 5 deletions lib/cmds/extension_cmds/delete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { createManagementClient } from '../../utils/contentful-clients'
import { successEmoji } from '../../utils/emojis'
Expand Down Expand Up @@ -28,10 +27,7 @@ export const builder = (yargs) => {
}

export async function deleteExtension (argv) {
const {context, id} = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

const { context, id } = argv
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/extension_cmds/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createManagementClient } from '../../utils/contentful-clients'
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { logExtension } from './utils/log-as-table'

Expand All @@ -16,11 +15,7 @@ export const builder = (yargs) => {
.epilog('Copyright 2018 Contentful, this is a BETA release')
}

async function getExtension (argv) {
const {context, id} = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

async function getExtension ({ context, id }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/extension_cmds/list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Table from 'cli-table3'

import { createManagementClient } from '../../utils/contentful-clients'
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { log } from '../../utils/log'
import paginate from '../../utils/pagination'
Expand All @@ -18,11 +17,7 @@ export const builder = (yargs) => {
.epilog('Copyright 2018 Contentful, this is a BETA release')
}

export async function listExtensions (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

export async function listExtensions ({ context }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/extension_cmds/update.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { createManagementClient } from '../../utils/contentful-clients'
import { successEmoji } from '../../utils/emojis'
Expand Down Expand Up @@ -60,10 +59,6 @@ export async function updateExtension (extension, data) {
}

export async function updateExtensionHandler (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

const data = await prepareData(argv)

await assertExtensionValuesProvided(data, 'update')
Expand All @@ -72,7 +67,7 @@ export async function updateExtensionHandler (argv) {
await readSrcDocFile(data.extension)
}

const { managementToken, activeSpaceId, activeEnvironmentId } = context
const { managementToken, activeSpaceId, activeEnvironmentId } = argv.context

const client = await createManagementClient({
accessToken: managementToken,
Expand Down
5 changes: 1 addition & 4 deletions lib/cmds/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { setContext, storeRuntimeConfig } from '../context'
import { confirmation } from '../utils/actions'
import { handleAsyncError as handle } from '../utils/async'
import { log, warning, success } from '../utils/log'
import { assertLoggedIn } from '../utils/assertions'

export const command = 'logout'

export const desc = 'Logout from Contentful'

export async function logout ({ context }) {
await assertLoggedIn(context)

export async function logout () {
warning('This will log you out by deleting the CMA token stored on your system.')
const confirmed = await confirmation('Do you want to log out now?')

Expand Down
4 changes: 0 additions & 4 deletions lib/cmds/space_cmds/accesstoken_cmds/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'
import { handleAsyncError as handle } from '../../../utils/async'
import { createManagementClient } from '../../../utils/contentful-clients'
import { successEmoji } from '../../../utils/emojis'
Expand Down Expand Up @@ -37,9 +36,6 @@ export const builder = (yargs) => {

export async function accessTokenCreate (argv) {
const { context, name, description, silent, feature = 'space-access_token-create' } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

const { managementToken, activeSpaceId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/space_cmds/accesstoken_cmds/list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Table from 'cli-table3'

import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'
import { handleAsyncError as handle } from '../../../utils/async'
import { createManagementClient } from '../../../utils/contentful-clients'
import { log } from '../../../utils/log'
Expand Down Expand Up @@ -28,11 +27,7 @@ export const builder = (yargs) => {

export const aliases = ['ls']

async function accessTokenList (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

async function accessTokenList ({ context }) {
const { managementToken, activeSpaceId } = context

const client = await createManagementClient({
Expand Down
2 changes: 0 additions & 2 deletions lib/cmds/space_cmds/create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as logging from '../../utils/log'
import { handleAsyncError as handle } from '../../utils/async'
import { createManagementClient } from '../../utils/contentful-clients'
import { assertLoggedIn } from '../../utils/assertions'
import { spaceUse } from './use'

import { EventSystem } from '../../core/events'
Expand Down Expand Up @@ -56,7 +55,6 @@ export const builder = yargs => {

export async function spaceCreate (argv) {
const { context, name, defaultLocale, yes, use, feature = 'space-create' } = argv
await assertLoggedIn(context)

const {managementToken} = context
let { organizationId } = argv
Expand Down
6 changes: 1 addition & 5 deletions lib/cmds/space_cmds/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createManagementClient } from '../../utils/contentful-clients'
import { successEmoji } from '../../utils/emojis'
import { log, success } from '../../utils/log'
import { highlightStyle, warningStyle } from '../../utils/styles'
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'

export const command = 'delete'
export const desc = 'Deletes a space'
Expand All @@ -28,10 +27,7 @@ export const builder = (yargs) => {
.epilog('Copyright 2018 Contentful, this is a BETA release')
}

export async function spaceDelete (argv) {
const { context, yes } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)
export async function spaceDelete ({ context, yes }) {
const { managementToken, activeSpaceId } = context
const client = await createManagementClient({
accessToken: managementToken
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/space_cmds/environment_cmds/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Promise from 'bluebird'
import * as logging from '../../../utils/log'
import { handleAsyncError as handle } from '../../../utils/async'
import { createManagementClient } from '../../../utils/contentful-clients'
import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'

export const command = 'create'

Expand Down Expand Up @@ -49,11 +48,7 @@ export const builder = (yargs) => {
].join('\n'))
}

export async function environmentCreate (argv) {
const { context, name, source, awaitProcessing, environmentId } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

export async function environmentCreate ({ context, name, source, awaitProcessing, environmentId }) {
const { managementToken, activeSpaceId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/space_cmds/environment_cmds/delete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as logging from '../../../utils/log'
import { handleAsyncError as handle } from '../../../utils/async'
import { createManagementClient } from '../../../utils/contentful-clients'
import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'

export const command = 'delete'

Expand Down Expand Up @@ -32,11 +31,7 @@ export const builder = (yargs) => {
].join('\n'))
}

export async function environmentDelete (argv) {
const { context, environmentId } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

export async function environmentDelete ({ context, environmentId }) {
const { managementToken, activeSpaceId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/space_cmds/environment_cmds/list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Table from 'cli-table3'

import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'
import { handleAsyncError as handle } from '../../../utils/async'
import { createManagementClient } from '../../../utils/contentful-clients'
import { log } from '../../../utils/log'
Expand Down Expand Up @@ -33,11 +32,7 @@ export const builder = (yargs) => {

export const aliases = ['ls']

export async function environmentList (argv) {
const { context } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

export async function environmentList ({ context }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/space_cmds/environment_cmds/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import inquirer from 'inquirer'
import { createManagementClient } from '../../../utils/contentful-clients'

import { setContext, storeRuntimeConfig } from '../../../context'
import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'
import { handleAsyncError as handle } from '../../../utils/async'
import { success } from '../../../utils/log'
import paginate from '../../../utils/pagination'
Expand Down Expand Up @@ -35,11 +34,7 @@ function showSuccess (space, environment) {
success(`Now using Environment ${highlightStyle(environment.name)} (${highlightStyle(environment.sys.id)}) in Space ${highlightStyle(space.name)} (${highlightStyle(space.sys.id)}) when the \`--environment-id\` option is missing.`)
}

export async function environmentUse (argv) {
const { context, environmentId } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

export async function environmentUse ({ context, environmentId }) {
const { managementToken, activeSpaceId } = context

const client = await createManagementClient({
Expand Down
4 changes: 0 additions & 4 deletions lib/cmds/space_cmds/export.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as log from '../../utils/log'
import runContentfulExport from 'contentful-export'
import {handleAsyncError as handle} from '../../utils/async'
import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { proxyObjectToString } from '../../utils/proxy'
import emojic from 'emojic'
import { version } from '../../../package.json'
Expand Down Expand Up @@ -109,9 +108,6 @@ export const builder = function (yargs) {

export const exportSpace = async (argv) => {
const { context, feature = 'space-export' } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

const { managementToken, activeSpaceId, activeEnvironmentId, host, proxy, rawProxy } = context
const managementApplication = `contentful.cli/${version}`
const managementFeature = feature
Expand Down
7 changes: 1 addition & 6 deletions lib/cmds/space_cmds/generate_cmds/migration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions'
import { handleAsyncError as handle } from '../../../utils/async'
import { log, success, warning } from '../../../utils/log'
import recast from 'recast'
Expand Down Expand Up @@ -268,11 +267,7 @@ const getEnvironment = async function (managementToken, spaceId, environmentId)
return space.getEnvironment(environmentId)
}

export async function generateMigration (argv) {
const { context, filename: filenameFlag, contentTypeId } = argv
await assertLoggedIn(context)
await assertSpaceIdProvided(context)

export async function generateMigration ({ context, filename: filenameFlag, contentTypeId }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const filename = filenameFlag || generateFileName(activeSpaceId, activeEnvironmentId, contentTypeId)
Expand Down
Loading

0 comments on commit 5bbf49a

Please sign in to comment.