Skip to content

Commit

Permalink
feat: use as a cli
Browse files Browse the repository at this point in the history
  • Loading branch information
simonecorsi committed Jan 27, 2023
1 parent 1604704 commit 12d45bb
Show file tree
Hide file tree
Showing 7 changed files with 2,035 additions and 337 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: npm i

- name: Build
run: npm run build
run: npm run compile

- name: Semantic release
uses: codfish/semantic-release-action@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.out
# Logs
logs
*.log
Expand Down
17 changes: 16 additions & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@
"assets": ["CHANGELOG.md", "package.json", "package-lock.json"]
}
],
"@semantic-release/github",
[
"@semantic-release/github",
{
"assets": [
{
"path": ".out/dats-cli-linux",
"label": "Linux distribution"
},
{
"path": ".out/dats-cli-macos",
"label": "Macos distribution"
},
{ "path": ".out/dats-cli-win", "label": "Win distribution" }
]
}
],
[
"@saithodev/semantic-release-backmerge",
{
Expand Down
2,252 changes: 1,920 additions & 332 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@immobiliarelabs/dats",
"version": "3.0.1",
"description": "Minimalistic zero-dependencies UDP/TCP statsd client for Node.js",
"bin": "./dist/dats-cli.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"publishConfig": {
Expand All @@ -25,6 +26,8 @@
"sampling"
],
"scripts": {
"precompile": "npm run build",
"compile": "pkg dist/dats-cli.js --out-path .out",
"build": "tsc -p tsconfig.json",
"build:watch": "npm run build -- -w",
"lint": "eslint --fix --ignore-path .gitignore .",
Expand Down Expand Up @@ -71,14 +74,15 @@
"lint-staged": "^13.0.0",
"markdown-toc": "^1.2.0",
"nyc": "^15.0.1",
"pkg": "^5.8.0",
"prettier": "^2.0.5",
"sinon": "^15.0.0",
"strip-ansi-cli": "^3.0.0",
"ts-node-dev": "^2.0.0",
"typescript": "^4.2.3"
"typescript": "^4.9.4"
},
"volta": {
"node": "16.13.0",
"node": "18.13.0",
"npm": "8.1.0"
}
}
90 changes: 90 additions & 0 deletions src/dats-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { parseArgs } from 'util';
import Client, { Types } from './index';

const TYPES = Object.keys(Types);

type Options = {
[k: string]: {
type: string;
default?: string | boolean;
help?: string;
validate?: (v: any) => boolean;
};
};

const options: Options = {
host: {
type: 'string',
validate: (v) => Boolean(v),
},
port: {
type: 'string',
validate: (v) => Boolean(v) && isFinite(v),
},
// metric
type: {
type: 'string',
help: 'Metric type can be one of: ' + TYPES.join(', '),
validate: (v) => Boolean(v) && TYPES.includes(v),
},
namespace: {
type: 'string',
help: 'Metric full namespace, use dots `.` to separate metrics',
validate: (v) => Boolean(v),
},
value: {
type: 'string',
help: 'Metric value',
validate: (v) => Boolean(v),
},
help: {
type: 'boolean',
},
};

function validateRequiredValues(values, opts: Options) {
let valid = true;
for (const [k, opt] of Object.entries(opts)) {
if (
typeof opt?.validate === 'function' &&
!opt?.validate(values?.[k])
) {
valid = false;
console.log('🚨 Wrong input flag: `%s`', k);
}
}
if (!valid) console.log('ℹ️ Run with `--help` for more info');
return valid;
}

function printHelp({ help, ...opts }: Options) {
console.log(
'ℹ️ The following are required input flags: \n\n%s\n\nIf unsure of output run the command prepended with `DRY_RUN=1`',
Object.entries(opts)
.map(([k, v]) => `\t--${k} {${v.type}} [${v?.help || ''}]`)
.join('\n')
);
return process.exit(0);
}

const { values } = parseArgs({ options } as any);

if (values.help) {
printHelp(options);
}

if (!validateRequiredValues(values, options)) {
process.exit(1);
}

const { host, port, type, value, namespace } = values;

const client = new Client({
host: new URL(`udp://${host}:${port}`),
});

if (!process.env.DRY_RUN) {
client[type as string](namespace, value);
} else {
console.log(`[dry-run]: ${namespace}:${value}|${Types[type as string]}`);
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface DebugLogger extends DebugLoggerFunction {
* Enum of metrics types
* @see https://github.com/statsd/statsd/blob/master/docs/metric_types.md
*/
enum Types {
export enum Types {
counter = 'c',
timing = 'ms',
gauge = 'g',
Expand Down

0 comments on commit 12d45bb

Please sign in to comment.