From cdfa98273796df16f2f6a318d50691b995957379 Mon Sep 17 00:00:00 2001 From: Moshe Malka Date: Wed, 6 Nov 2024 18:23:07 +0200 Subject: [PATCH] add stdin stream support --- tools/az-prom-rules-converter/README.md | 5 ++++ tools/az-prom-rules-converter/dist/cli.js | 25 +++++++++++++--- .../dist/steps/to-arm-template.js | 1 - tools/az-prom-rules-converter/package.json | 2 +- tools/az-prom-rules-converter/src/cli.ts | 30 +++++++++++++++---- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/tools/az-prom-rules-converter/README.md b/tools/az-prom-rules-converter/README.md index 1740a90fd..1da800ad5 100644 --- a/tools/az-prom-rules-converter/README.md +++ b/tools/az-prom-rules-converter/README.md @@ -36,3 +36,8 @@ Options: -l, --location Rule group location. -h, --help display help for command ``` + +## usage with pipeline +```bash +Get-Content .\examples\yaml-example1.yml | node az-prom-rules-converter +``` \ No newline at end of file diff --git a/tools/az-prom-rules-converter/dist/cli.js b/tools/az-prom-rules-converter/dist/cli.js index 7c33f5f98..274014929 100644 --- a/tools/az-prom-rules-converter/dist/cli.js +++ b/tools/az-prom-rules-converter/dist/cli.js @@ -24,9 +24,10 @@ program.description('Azure Prometheus rule groups tool'); function yaml2arm(inputPath, options, command) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { - const inputAbsolutePath = path_1.default.resolve(inputPath); - const str = (_a = (yield promises_1.default.readFile(inputAbsolutePath))) === null || _a === void 0 ? void 0 : _a.toString(); - const flowResult = (0, _1.default)(str, options); + const inputStr = inputPath ? + (_a = (yield promises_1.default.readFile(path_1.default.resolve(inputPath)))) === null || _a === void 0 ? void 0 : _a.toString() : + yield readStdin(); + const flowResult = (0, _1.default)(inputStr, options); if (flowResult.success == false) { console.error((_b = flowResult.error) === null || _b === void 0 ? void 0 : _b.title); console.error(JSON.stringify((_c = flowResult.error) === null || _c === void 0 ? void 0 : _c.details, null, 2)); @@ -44,7 +45,7 @@ function yaml2arm(inputPath, options, command) { } program //.command('yaml2arm') .description('Convert Prometheus rules Yaml file to ARM template') - .argument('', 'Input Prometheus rule groups Yaml file path.') + .argument('[input]', 'Input Prometheus rule groups Yaml (or Json) file path.') .option('-amw, --azure-monitor-workspace ', 'Azure monitor workspace id\'s that this rule group is scoped to.') .option('-c, --cluster-name ', 'The cluster name of the rule group evaluation.') .option('-a, --action-group-id ', 'The resource id of the action group to use for alerting rules.') @@ -53,3 +54,19 @@ program //.command('yaml2arm') .option('-l, --location ', 'Rule group location.') .action(yaml2arm); program.parse(); +function readStdin() { + return __awaiter(this, void 0, void 0, function* () { + return new Promise((resolve, reject) => { + let input = ''; + process.stdin.on('data', chunk => { + input += chunk; + }); + process.stdin.on('end', () => { + resolve(input); + }); + process.stdin.on('error', err => { + reject(err); + }); + }); + }); +} diff --git a/tools/az-prom-rules-converter/dist/steps/to-arm-template.js b/tools/az-prom-rules-converter/dist/steps/to-arm-template.js index 7aefe654e..768d1f9ca 100644 --- a/tools/az-prom-rules-converter/dist/steps/to-arm-template.js +++ b/tools/az-prom-rules-converter/dist/steps/to-arm-template.js @@ -68,7 +68,6 @@ const getArmTemplateFormat = (params) => { resources: [] }; ['clusterName', 'actionGroupId', 'azureMonitorWorkspace', 'location'].forEach((paramName) => { - console.log(paramName, params[paramName]); if (params[paramName]) { result.parameters[paramName].defaultValue = params[paramName]; } diff --git a/tools/az-prom-rules-converter/package.json b/tools/az-prom-rules-converter/package.json index de767dab8..8615086a7 100644 --- a/tools/az-prom-rules-converter/package.json +++ b/tools/az-prom-rules-converter/package.json @@ -1,6 +1,6 @@ { "name": "az-prom-rules-converter", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "dist/index.js", "bin": "dist/cli.js", diff --git a/tools/az-prom-rules-converter/src/cli.ts b/tools/az-prom-rules-converter/src/cli.ts index 0920d8b28..922e3922d 100644 --- a/tools/az-prom-rules-converter/src/cli.ts +++ b/tools/az-prom-rules-converter/src/cli.ts @@ -12,9 +12,10 @@ program.description('Azure Prometheus rule groups tool'); // program.version(pack.version); async function yaml2arm(inputPath: string, options: any, command: Command) { - const inputAbsolutePath = path.resolve(inputPath); - const str = (await fs.readFile(inputAbsolutePath))?.toString(); - const flowResult: StepResult = toArmTemplateFlow(str, options); + const inputStr = inputPath ? + (await fs.readFile(path.resolve(inputPath)))?.toString() : + await readStdin(); + const flowResult: StepResult = toArmTemplateFlow(inputStr, options); if (flowResult.success == false){ console.error(flowResult.error?.title); @@ -33,7 +34,7 @@ async function yaml2arm(inputPath: string, options: any, command: Command) { program//.command('yaml2arm') .description('Convert Prometheus rules Yaml file to ARM template') - .argument('', 'Input Prometheus rule groups Yaml (or Json) file path.') + .argument('[input]', 'Input Prometheus rule groups Yaml (or Json) file path.') .option('-amw, --azure-monitor-workspace ', 'Azure monitor workspace id\'s that this rule group is scoped to.') .option('-c, --cluster-name ', 'The cluster name of the rule group evaluation.') .option('-a, --action-group-id ', 'The resource id of the action group to use for alerting rules.') @@ -42,4 +43,23 @@ program//.command('yaml2arm') .option('-l, --location ', 'Rule group location.') .action(yaml2arm); -program.parse(); \ No newline at end of file +program.parse(); + + +async function readStdin(): Promise { + return new Promise((resolve, reject) => { + let input = ''; + + process.stdin.on('data', chunk => { + input += chunk; + }); + + process.stdin.on('end', () => { + resolve(input); + }); + + process.stdin.on('error', err => { + reject(err); + }); + }); +} \ No newline at end of file