-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolkit): add 'cdk context' command (#1169)
Add a command to view and manage cached context values. Fixes #311.
- Loading branch information
Showing
9 changed files
with
21,310 additions
and
7 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,30 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
scriptdir=$(cd $(dirname $0) && pwd) | ||
source ${scriptdir}/common.bash | ||
# ---------------------------------------------------------- | ||
|
||
rm -rf /tmp/cdk-integ-test | ||
mkdir -p /tmp/cdk-integ-test | ||
cd /tmp/cdk-integ-test | ||
|
||
cat > cdk.json <<HERE | ||
{ | ||
"context": { | ||
"contextkey": "this is the context value" | ||
} | ||
} | ||
HERE | ||
|
||
|
||
echo "Testing for the context value" | ||
cdk context 2>&1 | grep "this is the context value" > /dev/null | ||
|
||
# Test that deleting the contextkey works | ||
cdk context --reset contextkey | ||
cdk context 2>&1 | grep "this is the context value" > /dev/null && { echo "Should not contain key"; exit 1; } || true | ||
|
||
# Test that forced delete of the context key does not error | ||
cdk context -f --reset contextkey | ||
|
||
echo "✅ success" |
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,112 @@ | ||
import colors = require('colors/safe'); | ||
import table = require('table'); | ||
import yargs = require('yargs'); | ||
import { print } from '../../lib/logging'; | ||
import { DEFAULTS, loadProjectConfig, saveProjectConfig } from '../settings'; | ||
|
||
export const command = 'context'; | ||
export const describe = 'Manage cached context values'; | ||
export const builder = { | ||
reset: { | ||
alias: 'e', | ||
desc: 'The context key (or its index) to reset', | ||
type: 'string', | ||
requiresArg: 'KEY' | ||
}, | ||
clear: { | ||
desc: 'Clear all context', | ||
type: 'boolean', | ||
}, | ||
}; | ||
|
||
export async function handler(args: yargs.Arguments): Promise<number> { | ||
const settings = await loadProjectConfig(); | ||
const context = settings.get(['context']) || {}; | ||
|
||
if (args.clear) { | ||
settings.set(['context'], {}); | ||
await saveProjectConfig(settings); | ||
print('All context values cleared.'); | ||
} else if (args.reset) { | ||
invalidateContext(context, args.reset); | ||
settings.set(['context'], context); | ||
await saveProjectConfig(settings); | ||
} else { | ||
// List -- support '--json' flag | ||
if (args.json) { | ||
process.stdout.write(JSON.stringify(context, undefined, 2)); | ||
} else { | ||
listContext(context); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
function listContext(context: any) { | ||
const keys = contextKeys(context); | ||
|
||
// Print config by default | ||
const data: any[] = [[colors.green('#'), colors.green('Key'), colors.green('Value')]]; | ||
for (const [i, key] of keys) { | ||
const jsonWithoutNewlines = JSON.stringify(context[key], undefined, 2).replace(/\s+/g, ' '); | ||
data.push([i, key, jsonWithoutNewlines]); | ||
} | ||
|
||
print(`Context found in ${colors.blue(DEFAULTS)}:\n`); | ||
|
||
print(table.table(data, { | ||
border: table.getBorderCharacters('norc'), | ||
columns: { | ||
1: { width: 50, wrapWord: true } as any, | ||
2: { width: 50, wrapWord: true } as any | ||
} | ||
})); | ||
|
||
// tslint:disable-next-line:max-line-length | ||
print(`Run ${colors.blue('cdk context --reset KEY_OR_NUMBER')} to remove a context key. It will be refreshed on the next CDK synthesis run.`); | ||
} | ||
|
||
function invalidateContext(context: any, key: string) { | ||
const i = parseInt(key, 10); | ||
if (`${i}` === key) { | ||
// Twas a number and we fully parsed it. | ||
key = keyByNumber(context, i); | ||
} | ||
|
||
// Unset! | ||
if (key in context) { | ||
delete context[key]; | ||
print(`Context value ${colors.blue(key)} reset. It will be refreshed on the next SDK synthesis run.`); | ||
} else { | ||
print(`No context value with key ${colors.blue(key)}`); | ||
} | ||
} | ||
|
||
function keyByNumber(context: any, n: number) { | ||
for (const [i, key] of contextKeys(context)) { | ||
if (n === i) { | ||
return key; | ||
} | ||
} | ||
throw new Error(`No context key with number: ${n}`); | ||
} | ||
|
||
/** | ||
* Return enumerated keys in a definitive order | ||
*/ | ||
function contextKeys(context: any) { | ||
const keys = Object.keys(context); | ||
keys.sort(); | ||
return enumerate1(keys); | ||
} | ||
|
||
function enumerate1<T>(xs: T[]): Array<[number, T]> { | ||
const ret = new Array<[number, T]>(); | ||
let i = 1; | ||
for (const x of xs) { | ||
ret.push([i, x]); | ||
i += 1; | ||
} | ||
return ret; | ||
} |
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
Oops, something went wrong.