Skip to content

Add --context-file param to override cdk.context.json #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function makeConfig(): Promise<CliConfig> {
'app': { type: 'string', alias: 'a', desc: 'REQUIRED WHEN RUNNING APP: command-line for executing your app or a cloud assembly directory (e.g. "node bin/my-app.js"). Can also be specified in cdk.json or ~/.cdk.json', requiresArg: true },
'build': { type: 'string', desc: 'Command-line for a pre-synth build' },
'context': { type: 'array', alias: 'c', desc: 'Add contextual string parameter (KEY=VALUE)' },
'context-file': { type: 'string', desc: 'Add contextual file path parameter' },
'plugin': { type: 'array', alias: 'p', desc: 'Name or path of a node package that extend the CDK features. Can be specified multiple times' },
'trace': { type: 'boolean', desc: 'Print trace for stack warnings' },
'strict': { type: 'boolean', desc: 'Do not construct stacks with warnings' },
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk/lib/cli/convert-to-user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function convertYargsToUserInput(args: any): UserInput {
app: args.app,
build: args.build,
context: args.context,
contextFile: args.contextFile,
plugin: args.plugin,
trace: args.trace,
strict: args.strict,
Expand Down Expand Up @@ -291,6 +292,7 @@ export function convertConfigToUserInput(config: any): UserInput {
app: config.app,
build: config.build,
context: config.context,
contextFile: config.contextFile,
plugin: config.plugin,
trace: config.trace,
strict: config.strict,
Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk/lib/cli/parse-command-line-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export function parseCommandLineArguments(args: Array<string>): any {
nargs: 1,
requiresArg: true,
})
.option('context-file', {
default: undefined,
type: 'string',
desc: 'Add contextual file path parameter',
})
.option('plugin', {
type: 'array',
alias: 'p',
Expand Down
23 changes: 20 additions & 3 deletions packages/aws-cdk/lib/cli/user-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import * as os from 'os';
import * as fs_path from 'path';
import { ToolkitError } from '@aws-cdk/toolkit-lib';
import * as fs from 'fs-extra';
import { Context, PROJECT_CONTEXT } from '../api/context';
import { Context, PROJECT_CONTEXT as DEFAULT_PROJECT_CONTEXT } from '../api/context';
import { Settings } from '../api/settings';
import type { Tag } from '../api/tags';
import { debug, warning } from '../logging';
import { info, debug, warning } from '../logging';

export const PROJECT_CONFIG = 'cdk.json';
export { PROJECT_CONTEXT } from '../api/context';
export const USER_DEFAULTS = '~/.cdk.json';
const CONTEXT_KEY = 'context';

let PROJECT_CONTEXT = DEFAULT_PROJECT_CONTEXT;
export { PROJECT_CONTEXT };
Comment on lines +14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do this. It's going to be confusing if this value changes bases on whether load() has been called or not.

You can just update the name of the variable to DEFAULT_PROJECT_CONTEXT everywhere, or maybe for now just update the docblock.


export enum Command {
LS = 'ls',
LIST = 'list',
Expand Down Expand Up @@ -122,6 +124,20 @@ export class Configuration {
public async load(): Promise<this> {
const userConfig = await loadAndLog(USER_DEFAULTS);
this._projectConfig = await loadAndLog(PROJECT_CONFIG);
var userProjectContextFile = this.commandLineArguments.subSettings(['contextFile']).get([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll need to store the value as a class property

// Check if file exists
if (
userProjectContextFile &&
typeof userProjectContextFile === 'string' &&
fs.existsSync(expandHomeDir(userProjectContextFile))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think t the filepath should be considered relative to the user's home directory. It's also not what is done later on.

) {
info(`Using specified project context ${userProjectContextFile}`);
PROJECT_CONTEXT = userProjectContextFile;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

} else {
warning(
`File ${userProjectContextFile} does not exist or is not a valid path. Using default: ${PROJECT_CONTEXT}`,
);
}
this._projectContext = await loadAndLog(PROJECT_CONTEXT);

// @todo cannot currently be disabled by cli users
Expand Down Expand Up @@ -278,6 +294,7 @@ export function commandLineArgumentsToSettings(argv: Arguments): Settings {
build: argv.build,
caBundlePath: argv.caBundlePath,
context,
contextFile: argv.contextFile,
debug: argv.debug,
tags,
language: argv.language,
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk/lib/cli/user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ export interface GlobalOptions {
*/
readonly context?: Array<string>;

/**
* Add contextual file path parameter
*
* @default - undefined
*/
readonly contextFile?: string;

/**
* Name or path of a node package that extend the CDK features. Can be specified multiple times
*
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/test/cli/cli-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as logging from '../../lib/logging';
jest.mock('../../lib/logging', () => ({
info: jest.fn(),
debug: jest.fn(),
warning: jest.fn(),
error: jest.fn(),
print: jest.fn(),
result: jest.fn(),
Expand Down
Loading