Skip to content

Commit

Permalink
feat: add CLI for simple template transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
eoinsha committed Sep 19, 2024
1 parent c7a788e commit 046de70
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 45 deletions.
29 changes: 29 additions & 0 deletions cli/commands/transform.ts
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)
}
}
21 changes: 21 additions & 0 deletions cli/index.ts
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()
18 changes: 18 additions & 0 deletions cli/package.json
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"
}
}
Loading

0 comments on commit 046de70

Please sign in to comment.