Skip to content

Commit 0d70b25

Browse files
authored
feat: Add CLI argument parsing (#10)
1 parent b5bae80 commit 0d70b25

File tree

8 files changed

+154
-36
lines changed

8 files changed

+154
-36
lines changed

package-lock.json

Lines changed: 27 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"files": [
55
"dist",
66
"!dist/**/*.test.*",
7-
"!dist/tsconfig.tsbuildinfo"
7+
"!dist/tsconfig.tsbuildinfo",
8+
"!dist/**/*.map"
89
],
910
"scripts": {
1011
"dev": "ts-node src/index.ts",
@@ -38,9 +39,9 @@
3839
"author": "cloudquery (https://github.com/cloudquery)",
3940
"devDependencies": {
4041
"@ava/typescript": "^4.1.0",
41-
"@cloudquery/plugin-pb-js": "^0.0.4",
4242
"@grpc/grpc-js": "^1.9.0",
4343
"@tsconfig/node16": "^16.1.0",
44+
"@types/yargs": "^17.0.24",
4445
"@typescript-eslint/eslint-plugin": "^6.2.1",
4546
"@typescript-eslint/parser": "^6.2.1",
4647
"ava": "^5.3.1",
@@ -62,5 +63,9 @@
6263
},
6364
"compile": "tsc"
6465
}
66+
},
67+
"dependencies": {
68+
"@cloudquery/plugin-pb-javascript": "^0.0.5",
69+
"yargs": "^17.7.2"
6570
}
6671
}

src/index.test.ts renamed to src/grpc/server.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import { getServer } from '.';
2+
import { getServer } from './server';
33

44
test('getServer', (t) => {
55
const serve = getServer();

src/index.ts renamed to src/grpc/server.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { discovery1 } from '@cloudquery/plugin-pb-js';
2-
import { pluginV3 } from '@cloudquery/plugin-pb-js';
1+
import { discovery1 } from '@cloudquery/plugin-pb-javascript';
2+
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';
33
import grpc = require('@grpc/grpc-js');
44

55
class DiscoveryServer extends discovery1.cloudquery.discovery.v1.UnimplementedDiscoveryService {
@@ -92,11 +92,10 @@ export const getServer = () => {
9292
return server;
9393
};
9494

95-
const main = () => {
95+
export const startServer = (address: string) => {
9696
const server = getServer();
97-
server.bindAsync(`0.0.0.0:9999`, grpc.ServerCredentials.createInsecure(), (err, port) => {
97+
server.bindAsync(address, grpc.ServerCredentials.createInsecure(), (err, port) => {
9898
server.start();
99+
console.log('server running on port', port);
99100
});
100101
};
101-
102-
main();

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
import { serve } from './serve';
3+
4+
serve.parse();

src/serve.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava';
2+
import { serve as serveWithExit, ServeArgs } from './serve';
3+
4+
const serve = serveWithExit.exitProcess(false);
5+
6+
test('should return error without command', (t) => {
7+
t.throws(() => serve.parse([]), { message: 'Specify a command to run' });
8+
});
9+
10+
test('should pass with serve command and return default flags', (t) => {
11+
delete process.env.CQ_TELEMETRY_LEVEL;
12+
const results = serve.parse(['serve']) as ServeArgs;
13+
const { address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel } = results;
14+
t.deepEqual(
15+
{ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel },
16+
{
17+
address: 'localhost:7777',
18+
network: 'tcp',
19+
logLevel: 'info',
20+
logFormat: 'text',
21+
sentry: true,
22+
otelEndpoint: '',
23+
telemetryLevel: 'all',
24+
},
25+
);
26+
});

src/serve.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import yargs from 'yargs';
2+
import { hideBin } from 'yargs/helpers';
3+
4+
const NETWORK_CHOICES = ['tcp', 'tcp4', 'tcp6', 'unix', 'unixpacket'] as const;
5+
const LOG_LEVEL_CHOICES = ['trace', 'debug', 'info', 'warn', 'error'] as const;
6+
const LOG_FORMAT_CHOICES = ['json', 'text'] as const;
7+
const TELEMETRY_LEVEL_CHOICES = ['none', 'errors', 'stats', 'all'] as const;
8+
9+
export type ServeArgs = {
10+
address: string;
11+
network: (typeof NETWORK_CHOICES)[number];
12+
logLevel: (typeof LOG_LEVEL_CHOICES)[number];
13+
logFormat: (typeof LOG_FORMAT_CHOICES)[number];
14+
sentry: boolean;
15+
otelEndpoint: string;
16+
otelEndpointInsecure: boolean;
17+
telemetryLevel: (typeof TELEMETRY_LEVEL_CHOICES)[number];
18+
};
19+
20+
export const serve = yargs(hideBin(process.argv))
21+
.command<ServeArgs>(
22+
'serve',
23+
'start plugin gRPC server',
24+
() => {},
25+
({ address, network, logLevel, logFormat, sentry: sentry, otelEndpoint, telemetryLevel }: ServeArgs) => {
26+
console.log({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel });
27+
},
28+
)
29+
.options({
30+
address: {
31+
alias: 'a',
32+
type: 'string',
33+
description: 'address to bind to',
34+
default: 'localhost:7777',
35+
},
36+
network: {
37+
alias: 'n',
38+
type: 'string',
39+
choices: NETWORK_CHOICES,
40+
description: 'network to bind to',
41+
default: 'tcp',
42+
},
43+
'log-level': {
44+
alias: 'l',
45+
type: 'string',
46+
choices: LOG_LEVEL_CHOICES,
47+
description: 'log level',
48+
default: 'info',
49+
},
50+
'log-format': {
51+
alias: 'f',
52+
type: 'string',
53+
choices: LOG_FORMAT_CHOICES,
54+
description: 'log format',
55+
default: 'text',
56+
},
57+
sentry: {
58+
type: 'boolean',
59+
description: 'enable sentry reporting. Pass `--no-sentry` to disable.',
60+
default: true,
61+
},
62+
'otel-endpoint': {
63+
type: 'string',
64+
description: 'OpenTelemetry collector endpoint',
65+
default: '',
66+
},
67+
'otel-endpoint-insecure': {
68+
type: 'boolean',
69+
description: 'use Open Telemetry HTTP endpoint (for development only)',
70+
default: false,
71+
},
72+
'telemetry-level': {
73+
type: 'string',
74+
description: 'CQ Telemetry level',
75+
hidden: true,
76+
choices: TELEMETRY_LEVEL_CHOICES,
77+
default: 'all',
78+
},
79+
})
80+
.env('CQ_')
81+
.strict()
82+
.demandCommand(1, 1, 'Specify a command to run');

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"compilerOptions": {
44
"allowJs": true,
55
"declaration": true,
6-
"outDir": "dist"
6+
"outDir": "dist",
7+
"sourceMap": true
78
}
89
}

0 commit comments

Comments
 (0)