Skip to content

Commit

Permalink
chore: setup new cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Jan 24, 2021
1 parent 3fcff9e commit 34425fb
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 36 deletions.
32 changes: 32 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @ogma/cli

Rehydration of Ogma JSON logs without a problem.

Ogma comes with a command line function to rehydrate your json formatted logs back into the human readable format that can be installed with `@ogma/cli`. The command takes one to two arguments, a log file relative to where the command is run from, and an optional flag to force the cli to print out with color. Find the table below to learn more about the arguments.

| argument | required | default | description |
| --- | --- | --- | --- |
| file | yes | none | The log file to be "hydrated". This file should contain newline separated Ogma formatted JSON logs. |
| --color | no | terminal's TTY argument | you can pass `--color` or `--color=true` to force colors to be used. `--color=false` will force the command to not print the logs back out in color. Depending on the terminal you are using, colors may not be used by default. |

The arguments can be passed in any order for ease of use.

## Example

An example of the command's usage could be like so:

```sh
ogma production.log --color
```

or if you have a TTY enabled command prompt

```sh
ogma production.log
```

As this prints out to `process.stdout` it is possible to pipe this output to another file using the `>` operator. Like so:

```sh
ogma production.log > production.hydrated.log
```
1 change: 1 addition & 0 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { preset: '../../jest.config.js' };
53 changes: 53 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@ogma/cli",
"version": "0.4.3",
"description": "A CLI to re-hydrate JSON logs back to a dev readable format, colors included.",
"keywords": [
"cli",
"ogma",
"logs",
"logger",
"hydrate",
"nodejs"
],
"author": "Jay McDoniel <[email protected]>",
"homepage": "https://github.com/jmcdo29/ogma",
"license": "MIT",
"main": "lib/main.js",
"types": "lib/index.d.ts",
"bin": {
"ogma": "./lib/main.js"
},
"files": [
"lib"
],
"directories": {
"lib": "lib"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jmcdo29/ogma.git"
},
"scripts": {
"prebuild": "rimraf lib",
"build": "tsc -p tsconfig.build.json",
"postbuild": "mv ./lib/src/* ./lib && rmdir lib/src",
"lint": "eslint --ext .ts ./src/",
"test": "jest",
"test:cov": "jest --coverage"
},
"engines": {
"node": ">=10"
},
"bugs": {
"url": "https://github.com/jmcdo29/ogma/issues"
},
"dependencies": {
"@nestjs/common": "^7.6.5",
"@nestjs/core": "^7.6.5",
"@ogma/logger": "^0.4.2"
}
}
7 changes: 7 additions & 0 deletions packages/cli/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { CommandService } from './command.service';

@Module({
providers: [CommandService],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions packages/cli/src/command-args.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface OgmaArgs {
file: string;
color?: boolean;
}

export const ArgKeyMap = {
'--color': 'color',
};
48 changes: 48 additions & 0 deletions packages/cli/src/command.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Injectable } from '@nestjs/common';
import { promises } from 'fs';
import { join } from 'path';
import { ArgKeyMap, OgmaArgs } from './command-args.interface';

const { readFile } = promises;

@Injectable()
export class CommandService {
private validFlags = Object.keys(ArgKeyMap);

private ogmaKeys = ['time', 'pid', 'level'];

async act(args: string[]) {
const options = this.validateArgs(args);
const fileContents = await this.readFile(options.file);
}

validateArgs(args: string[]): OgmaArgs {
const retArgs: OgmaArgs = {
file: '',
color: process.stdout.isTTY,
};
retArgs.file = args.filter((arg) => !arg.includes('--'))[0];
if (!args.length || args.length > 2) {
throw new Error('Invalid usage');
}
const flags = args.filter((arg) => arg.includes('--'));
const flagKeys = flags.map((flag) => flag.split('=')[0]);
if (args.length > 1 && !flagKeys.every((flag) => this.validFlags.includes(flag))) {
throw new Error('Invalid options');
}
flags.forEach((flag) => {
const [key, value] = flag.split('=');
retArgs[ArgKeyMap[key]] = value ? JSON.parse(value) : true;
});
return retArgs;
}

async readFile(fileName: string): Promise<string> {
const file = await readFile(join(process.cwd(), fileName));
return file.toString();
}

isOgmaFormat(log: Record<string, any>): boolean {
return this.ogmaKeys.every((key) => Object.prototype.hasOwnProperty.call(log, key));
}
}
19 changes: 19 additions & 0 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CommandService } from './command.service';

async function bootstrap(...args: string[]) {
const app = await NestFactory.createApplicationContext(AppModule);
const service = app.get(CommandService);
await service.act(args);
await app.close();
}

const [, , ...args] = process.argv;

bootstrap(...args).catch((err) => {
process.stderr.write(err.message);
process.stderr.write(err.stack);
process.exit(1);
});
4 changes: 4 additions & 0 deletions packages/cli/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["test"]
}
8 changes: 8 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./",
"outDir": "./lib"
},
"include": ["./src", "./test"]
}
31 changes: 0 additions & 31 deletions packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,37 +149,6 @@ I said the logs were beautiful, and to me they absolutely are. Each log is match
}
```

## Command Line Function

Ogma comes with a built in command line function to rehydrate your json formatted logs back into the human readable format. The command takes one to two arguments, a log file relative to where the command is run from, and an optional flag to force the cli to print out with color. Find the table below to learn more about the arguments.

| argument | required | default | description |
| --- | --- | --- | --- |
| file | yes | none | The log file to be "hydrated". This file should contain newline separated Ogma formatted JSON logs. |
| --color | no | terminal's TTY argument | you can pass `--color` or `--color=true` to force colors to be used. `--color=false` will force the command to not print the logs back out in color. Depending on the terminal you are using, colors may not be used by default. |

The arguments can be passed in any order for ease of use.

### Example

An example of the command's usage could be like so:

```sh
ogma production.log --color
```

or if you have a TTY enabled command prompt

```sh
ogma production.log
```

As this prints out to `process.stdout` it is possible to pipe this output to another file using the `>` operator. Like so:

```sh
ogma production.log > production.hydrated.log
```

## Benchmarks

Benchmarks will be brought back soon. After moving to the monorepo format, some things got messed up. Rest assured, they'll be back.
3 changes: 0 additions & 3 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
"license": "MIT",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"bin": {
"ogma": "./lib/command/index.js"
},
"scripts": {
"prebuild": "rimraf lib",
"build": "tsc -p tsconfig.build.json",
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@
webpack "5.9.0"
webpack-node-externals "2.5.2"

"@nestjs/common@^7.0.0", "@nestjs/common@^7.0.6", "@nestjs/common@^7.3.2":
"@nestjs/common@^7.0.0", "@nestjs/common@^7.0.6", "@nestjs/common@^7.3.2", "@nestjs/common@^7.6.5":
version "7.6.5"
resolved "https://registry.yarnpkg.com/@nestjs/common/-/common-7.6.5.tgz#d6e9435453eef2d1b492384ca27fa23358744949"
integrity sha512-WvBJd71ktaCRm9KTURVqn1YMyUzsOIkvezjP7WEpP9DVqQUOFVvn6/osJGZky/qL+zE4P7NBNyoXM94bpYvMwQ==
Expand All @@ -2133,7 +2133,7 @@
tslib "2.0.3"
uuid "8.3.2"

"@nestjs/core@^7.0.0", "@nestjs/core@^7.0.6", "@nestjs/core@^7.3.2":
"@nestjs/core@^7.0.0", "@nestjs/core@^7.0.6", "@nestjs/core@^7.3.2", "@nestjs/core@^7.6.5":
version "7.6.5"
resolved "https://registry.yarnpkg.com/@nestjs/core/-/core-7.6.5.tgz#93c642d1abf8d3f09f8f78c262814eaf83bc2ad6"
integrity sha512-syRpXT09RDMySs1BLQSXJfq1NXGfG4VmF9hZYGef+/QWqTRfSMEDEH5MsCCLt2lK3AZnOXE9BQwWKeNBhKLplA==
Expand Down

0 comments on commit 34425fb

Please sign in to comment.