-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-introduce the
tag
command to update the tags of a stack without …
…the need for synthesizing and re-deploying
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
CloudFormationClient, | ||
UpdateStackCommand, | ||
} from '@aws-sdk/client-cloudformation'; | ||
import {findStack} from './find-stack.js'; | ||
|
||
export interface UpdateTagsOptions { | ||
readonly stackName: string; | ||
readonly tagsToAdd: [string, string | undefined][]; | ||
readonly tagKeysToRemove: string[]; | ||
} | ||
|
||
export async function updateTags(options: UpdateTagsOptions): Promise<void> { | ||
const {stackName, tagsToAdd, tagKeysToRemove} = options; | ||
const stack = await findStack(stackName); | ||
const client = new CloudFormationClient({}); | ||
|
||
const tagObjects = [ | ||
...(stack.Tags ?? []).filter(({Key}) => | ||
tagsToAdd.every(([key]) => key !== Key), | ||
), | ||
...tagsToAdd.map(([key, value]) => ({Key: key, Value: value || `true`})), | ||
].filter(({Key}) => Key && !tagKeysToRemove.includes(Key)); | ||
|
||
await client.send( | ||
new UpdateStackCommand({ | ||
StackName: stackName, | ||
UsePreviousTemplate: true, | ||
Capabilities: stack.Capabilities, | ||
Parameters: stack.Parameters, | ||
Tags: tagObjects, | ||
}), | ||
); | ||
} |
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,89 @@ | ||
import type {CommandModule} from 'yargs'; | ||
import {readStackConfig} from './read-stack-config.js'; | ||
import {updateTags} from './sdk/update-tags.js'; | ||
import {getDomainName} from './utils/get-domain-name.js'; | ||
import {getStackName} from './utils/get-stack-name.js'; | ||
import {print} from './utils/print.js'; | ||
|
||
const commandName = `tag`; | ||
|
||
export const tagCommand: CommandModule< | ||
{}, | ||
{ | ||
readonly 'stack-name': string | undefined; | ||
readonly 'add': readonly (string | number)[]; | ||
readonly 'remove': readonly (string | number)[]; | ||
readonly 'yes': boolean; | ||
} | ||
> = { | ||
command: `${commandName} [options]`, | ||
describe: `Update the tags of the specified stack.`, | ||
|
||
builder: (argv) => | ||
argv | ||
.options(`stack-name`, { | ||
describe: `An optional stack name, if not specified it will be determined from the config file`, | ||
string: true, | ||
}) | ||
.options(`add`, { | ||
describe: `Tags to add to the specified stack`, | ||
array: true, | ||
default: [], | ||
}) | ||
.options(`remove`, { | ||
describe: `Tags to remove from the specified stack`, | ||
array: true, | ||
default: [], | ||
}) | ||
.options(`yes`, { | ||
describe: `Confirm to add or remove the specified tags automatically`, | ||
boolean: true, | ||
default: false, | ||
}) | ||
.example([ | ||
[ | ||
`npx $0 ${commandName} --stack-name ${getStackName( | ||
`example.com`, | ||
)} --add latest release --remove prerelease`, | ||
], | ||
[`npx $0 ${commandName} --add foo=something bar="something else"`], | ||
[`npx $0 ${commandName} --add prerelease --yes`], | ||
]), | ||
|
||
handler: async (args): Promise<void> => { | ||
const stackName = | ||
args.stackName || getStackName(getDomainName(await readStackConfig())); | ||
|
||
print.warning(`Stack: ${stackName}`); | ||
|
||
if (args.yes) { | ||
print.warning(`The tags of the specified stack will be updated.`); | ||
} else { | ||
const confirmed = await print.confirmation( | ||
`Confirm to update the tags of the specified stack.`, | ||
); | ||
|
||
if (!confirmed) { | ||
return; | ||
} | ||
} | ||
|
||
print.info(`Updating tags...`); | ||
|
||
await updateTags({ | ||
stackName, | ||
tagsToAdd: args.add.map((arg) => parseTag(arg.toString())), | ||
tagKeysToRemove: args.remove.map((arg) => arg.toString()), | ||
}); | ||
|
||
print.success( | ||
`The tags of the specified stack have been successfully updated.`, | ||
); | ||
}, | ||
}; | ||
|
||
function parseTag(arg: string): [string, string | undefined] { | ||
const [key, value] = arg.split(`=`) as [string, ...string[]]; | ||
|
||
return [key, value]; | ||
} |