Skip to content

Commit

Permalink
add stdin stream support
Browse files Browse the repository at this point in the history
  • Loading branch information
Moshe Malka committed Nov 6, 2024
1 parent 7a8e490 commit cdfa982
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
5 changes: 5 additions & 0 deletions tools/az-prom-rules-converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ Options:
-l, --location <string> Rule group location.
-h, --help display help for command
```

## usage with pipeline
```bash
Get-Content .\examples\yaml-example1.yml | node az-prom-rules-converter
```
25 changes: 21 additions & 4 deletions tools/az-prom-rules-converter/dist/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -44,7 +45,7 @@ function yaml2arm(inputPath, options, command) {
}
program //.command('yaml2arm')
.description('Convert Prometheus rules Yaml file to ARM template')
.argument('<input>', 'Input Prometheus rule groups Yaml file path.')
.argument('[input]', 'Input Prometheus rule groups Yaml (or Json) file path.')
.option('-amw, --azure-monitor-workspace <string>', 'Azure monitor workspace id\'s that this rule group is scoped to.')
.option('-c, --cluster-name <string>', 'The cluster name of the rule group evaluation.')
.option('-a, --action-group-id <string>', 'The resource id of the action group to use for alerting rules.')
Expand All @@ -53,3 +54,19 @@ program //.command('yaml2arm')
.option('-l, --location <string>', '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);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
2 changes: 1 addition & 1 deletion tools/az-prom-rules-converter/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 25 additions & 5 deletions tools/az-prom-rules-converter/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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>', '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 <string>', 'Azure monitor workspace id\'s that this rule group is scoped to.')
.option('-c, --cluster-name <string>', 'The cluster name of the rule group evaluation.')
.option('-a, --action-group-id <string>', 'The resource id of the action group to use for alerting rules.')
Expand All @@ -42,4 +43,23 @@ program//.command('yaml2arm')
.option('-l, --location <string>', 'Rule group location.')
.action(yaml2arm);

program.parse();
program.parse();


async function readStdin(): Promise<string> {
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);
});
});
}

0 comments on commit cdfa982

Please sign in to comment.