Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"files": [
"dist",
"!dist/**/*.test.*",
"!dist/tsconfig.tsbuildinfo"
"!dist/tsconfig.tsbuildinfo",
"!dist/**/*.map"
],
"scripts": {
"dev": "ts-node src/index.ts",
Expand Down Expand Up @@ -38,9 +39,9 @@
"author": "cloudquery (https://github.com/cloudquery)",
"devDependencies": {
"@ava/typescript": "^4.1.0",
"@cloudquery/plugin-pb-js": "^0.0.4",
"@grpc/grpc-js": "^1.9.0",
"@tsconfig/node16": "^16.1.0",
"@types/yargs": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"ava": "^5.3.1",
Expand All @@ -62,5 +63,9 @@
},
"compile": "tsc"
}
},
"dependencies": {
"@cloudquery/plugin-pb-javascript": "^0.0.5",
"yargs": "^17.7.2"
}
}
2 changes: 1 addition & 1 deletion src/index.test.ts → src/grpc/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import { getServer } from '.';
import { getServer } from './server';

test('getServer', (t) => {
const serve = getServer();
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts → src/grpc/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { discovery1 } from '@cloudquery/plugin-pb-js';
import { pluginV3 } from '@cloudquery/plugin-pb-js';
import { discovery1 } from '@cloudquery/plugin-pb-javascript';
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';
import grpc = require('@grpc/grpc-js');

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

const main = () => {
export const startServer = (address: string) => {
const server = getServer();
server.bindAsync(`0.0.0.0:9999`, grpc.ServerCredentials.createInsecure(), (err, port) => {
server.bindAsync(address, grpc.ServerCredentials.createInsecure(), (err, port) => {
server.start();
console.log('server running on port', port);
});
};

main();
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
import { serve } from './serve';

serve.parse();
26 changes: 26 additions & 0 deletions src/serve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava';
import { serve as serveWithExit, ServeArgs } from './serve';

const serve = serveWithExit.exitProcess(false);

test('should return error without command', (t) => {
t.throws(() => serve.parse([]), { message: 'Specify a command to run' });
});

test('should pass with serve command and return default flags', (t) => {
delete process.env.CQ_TELEMETRY_LEVEL;
const results = serve.parse(['serve']) as ServeArgs;
const { address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel } = results;
t.deepEqual(
{ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel },
{
address: 'localhost:7777',
network: 'tcp',
logLevel: 'info',
logFormat: 'text',
sentry: true,
otelEndpoint: '',
telemetryLevel: 'all',
},
);
});
82 changes: 82 additions & 0 deletions src/serve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

const NETWORK_CHOICES = ['tcp', 'tcp4', 'tcp6', 'unix', 'unixpacket'] as const;
const LOG_LEVEL_CHOICES = ['trace', 'debug', 'info', 'warn', 'error'] as const;
const LOG_FORMAT_CHOICES = ['json', 'text'] as const;
const TELEMETRY_LEVEL_CHOICES = ['none', 'errors', 'stats', 'all'] as const;

export type ServeArgs = {
address: string;
network: (typeof NETWORK_CHOICES)[number];
logLevel: (typeof LOG_LEVEL_CHOICES)[number];
logFormat: (typeof LOG_FORMAT_CHOICES)[number];
sentry: boolean;
otelEndpoint: string;
otelEndpointInsecure: boolean;
telemetryLevel: (typeof TELEMETRY_LEVEL_CHOICES)[number];
};

export const serve = yargs(hideBin(process.argv))
.command<ServeArgs>(
'serve',
'start plugin gRPC server',
() => {},
({ address, network, logLevel, logFormat, sentry: sentry, otelEndpoint, telemetryLevel }: ServeArgs) => {
console.log({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel });
},
)
.options({
address: {
alias: 'a',
type: 'string',
description: 'address to bind to',
default: 'localhost:7777',
},
network: {
alias: 'n',
type: 'string',
choices: NETWORK_CHOICES,
description: 'network to bind to',
default: 'tcp',
},
'log-level': {
alias: 'l',
type: 'string',
choices: LOG_LEVEL_CHOICES,
description: 'log level',
default: 'info',
},
'log-format': {
alias: 'f',
type: 'string',
choices: LOG_FORMAT_CHOICES,
description: 'log format',
default: 'text',
},
sentry: {
type: 'boolean',
description: 'enable sentry reporting. Pass `--no-sentry` to disable.',
default: true,
},
'otel-endpoint': {
type: 'string',
description: 'OpenTelemetry collector endpoint',
default: '',
},
'otel-endpoint-insecure': {
type: 'boolean',
description: 'use Open Telemetry HTTP endpoint (for development only)',
default: false,
},
'telemetry-level': {
type: 'string',
description: 'CQ Telemetry level',
hidden: true,
choices: TELEMETRY_LEVEL_CHOICES,
default: 'all',
},
})
.env('CQ_')
.strict()
.demandCommand(1, 1, 'Specify a command to run');
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"allowJs": true,
"declaration": true,
"outDir": "dist"
"outDir": "dist",
"sourceMap": true
}
}