-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add print-change-set function to org-formation cli (#547)
* Add print-change-set function to org-formation cli The print-change-set function will output the changes a changeset will perform, in either yaml or json. * chore: made some minor tweaks * Remove unused import from print-changesets --------- Co-authored-by: Olaf Conijn <[email protected]>
- Loading branch information
1 parent
85b66b1
commit 54f5c19
Showing
6 changed files
with
110 additions
and
2 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
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 { Command } from 'commander'; | ||
import { ConsoleUtil } from '../util/console-util'; | ||
import { BaseCliCommand, ICommandArgs } from './base-command'; | ||
import { ChangeSetProvider } from '~change-set/change-set-provider'; | ||
import { yamlDump } from '~yaml-cfn/index'; | ||
|
||
const commandName = 'print-change-set <change-set-name>'; | ||
const commandDescription = 'outputs change set details'; | ||
|
||
export class PrintChangeSetCommand extends BaseCliCommand<IPrintChangeSetCommandArgs> { | ||
|
||
public static async Perform(command: IPrintChangeSetCommandArgs): Promise<void> { | ||
const x = new PrintChangeSetCommand(); | ||
await x.performCommand(command); | ||
} | ||
|
||
constructor(command?: Command) { | ||
super(command, commandName, commandDescription, 'changeSetName'); | ||
} | ||
|
||
public addOptions(command: Command): void { | ||
super.addOptions(command); | ||
command.option('--change-set-name [change-set-name]', 'change set name'); | ||
command.option('--output <output>', 'serialization format used when printing change set. Either json or yaml.', 'json'); | ||
} | ||
|
||
public async performCommand(command: IPrintChangeSetCommandArgs): Promise<void> { | ||
if (!['json', 'yaml'].includes(command.output)) { | ||
ConsoleUtil.LogError(`Invalid output format '${command.output}'. Must be either 'json' or 'yaml'.`); | ||
return; | ||
} | ||
const changeSetName = command.changeSetName; | ||
const stateBucketName = await BaseCliCommand.GetStateBucketName(command.stateBucketName); | ||
const provider = new ChangeSetProvider(stateBucketName); | ||
const changeSetObj = await provider.getChangeSet(changeSetName); | ||
if (!changeSetObj) { | ||
ConsoleUtil.LogError(`change set '${changeSetName}' not found.`); | ||
return; | ||
} | ||
const changeSet = changeSetObj.changeSet; | ||
|
||
if (command.output === 'json') { | ||
ConsoleUtil.Out(JSON.stringify(changeSet, null, 2)); | ||
} else if (command.output === 'yaml') { | ||
ConsoleUtil.Out(yamlDump(changeSet)); | ||
} | ||
|
||
} | ||
} | ||
|
||
export interface IPrintChangeSetCommandArgs extends ICommandArgs { | ||
changeSetName?: string; | ||
output?: 'json' | 'yaml'; | ||
} |
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,33 @@ | ||
import { Command, Option } from "commander"; | ||
import { PrintChangeSetCommand } from "~commands/print-changeset"; | ||
|
||
describe('when creating print change set command', () => { | ||
let command: PrintChangeSetCommand; | ||
let commanderCommand: Command; | ||
let subCommanderCommand: Command; | ||
|
||
beforeEach(() => { | ||
commanderCommand = new Command('root'); | ||
command = new PrintChangeSetCommand(commanderCommand); | ||
subCommanderCommand = commanderCommand.commands[0]; | ||
}); | ||
|
||
test('print change set command is created', () => { | ||
expect(command).toBeDefined(); | ||
expect(subCommanderCommand).toBeDefined(); | ||
expect(subCommanderCommand.name()).toBe('print-change-set'); | ||
}); | ||
|
||
test('print change set command has description', () => { | ||
expect(subCommanderCommand).toBeDefined(); | ||
expect(subCommanderCommand.description()).toBeDefined(); | ||
}); | ||
|
||
test('command has required output parameter with default', () => { | ||
const opts: Option[] = subCommanderCommand.options; | ||
const stackNameOpt = opts.find((x) => x.long === '--output'); | ||
expect(stackNameOpt).toBeDefined(); | ||
expect(stackNameOpt.required).toBeTruthy(); | ||
expect(subCommanderCommand.output).toBe('yaml'); | ||
}); | ||
}); |