-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CLI for simple template transformation
- Loading branch information
Showing
7 changed files
with
160 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile, writeFile } from 'node:fs/promises' | ||
import { parse, stringify } from 'yaml' | ||
import { addAlarms, addDashboard } from 'slic-watch-core/index' | ||
import { type SlicWatchConfig, resolveSlicWatchConfig } from 'slic-watch-core/inputs/general-config' | ||
import { setLogger } from 'slic-watch-core/logging' | ||
import { cliLogger } from '../lib/cli-logger' | ||
|
||
setLogger(cliLogger) | ||
|
||
export default async function transform (inputFile: string, outputFile: string) { | ||
const outputToJson = outputFile.endsWith('.json') | ||
try { | ||
const input = await readFile(inputFile) | ||
const template = parse(input.toString()) | ||
const slicWatchConfig: SlicWatchConfig = template.Metadata?.slicWatch ?? {} | ||
|
||
const config = resolveSlicWatchConfig(slicWatchConfig) | ||
|
||
setLogger(cliLogger) | ||
addAlarms(config.alarms, config.alarmActionsConfig, template) | ||
addDashboard(config.dashboard, template) | ||
|
||
const output = outputToJson ? JSON.stringify(template, null, ' ') : stringify(template) | ||
await writeFile(outputFile, output) | ||
} catch (err) { | ||
cliLogger.error(err) | ||
process.exit(1) | ||
} | ||
} |
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,21 @@ | ||
#!/usr/bin/env node | ||
|
||
import { program } from 'commander' | ||
import transform from './commands/transform' | ||
|
||
program | ||
.name('slic-watch') | ||
.description('SLIC Watch CLI for transforming CloudFormation templates, adding alarms and dashboards') | ||
.addHelpText('after', ` | ||
$ slic-watch <input_template> <output_template> | ||
`) | ||
|
||
program | ||
.command('transform', { isDefault: true }) | ||
.argument('<input_template>', 'Path to the JSON or YAML template to transform') | ||
.argument('<output_template>', 'Path for output template') | ||
.action(transform) | ||
|
||
program | ||
.parse() |
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,18 @@ | ||
{ | ||
"name": "slic-watch-cli", | ||
"version": "3.2.1", | ||
"description": "CLI for SLIC Watch transformation", | ||
"main": "dist/index.js", | ||
"bin": "dist/index.js", | ||
"scripts": { | ||
"test": "tap --include='**/tests/**/*.ts' --coverage-report=lcovonly --allow-incomplete-coverage", | ||
"build": "esbuild index.ts --outdir=dist --bundle --format=cjs --platform=node --sourcemap", | ||
"watch": "npm run build -- --watch" | ||
}, | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"cli-color": "^2.0.4", | ||
"commander": "^12.1.0" | ||
} | ||
} |
Oops, something went wrong.