neonctl 1.18.0 → 1.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth.js +1 -1
- package/commands/auth.js +2 -5
- package/commands/auth.test.js +37 -0
- package/commands/branches.test.js +1 -1
- package/commands/connection_string.test.js +1 -1
- package/commands/databases.test.js +1 -1
- package/commands/help.test.js +1 -1
- package/commands/operations.test.js +1 -1
- package/commands/projects.test.js +1 -1
- package/commands/roles.test.js +1 -1
- package/config.js +2 -2
- package/index.js +8 -0
- package/package.json +3 -2
- package/test_utils/mock_server.js +14 -0
- package/test_utils/oauth_server.js +9 -0
- package/{test_utils.js → test_utils/test_cli_command.js} +5 -18
package/auth.js
CHANGED
|
@@ -40,7 +40,7 @@ export const auth = async ({ oauthHost, clientId }) => {
|
|
|
40
40
|
//
|
|
41
41
|
const server = createServer();
|
|
42
42
|
server.listen(0, '127.0.0.1', function () {
|
|
43
|
-
log.
|
|
43
|
+
log.debug(`Listening on port ${this.address().port}`);
|
|
44
44
|
});
|
|
45
45
|
await new Promise((resolve) => server.once('listening', resolve));
|
|
46
46
|
const listen_port = server.address().port;
|
package/commands/auth.js
CHANGED
|
@@ -13,13 +13,10 @@ export const builder = (yargs) => yargs;
|
|
|
13
13
|
export const handler = async (args) => {
|
|
14
14
|
await authFlow(args);
|
|
15
15
|
};
|
|
16
|
-
export const authFlow = async ({ configDir, oauthHost, clientId, }) => {
|
|
17
|
-
if (isCi()) {
|
|
16
|
+
export const authFlow = async ({ configDir, oauthHost, clientId, forceAuth, }) => {
|
|
17
|
+
if (!forceAuth && isCi()) {
|
|
18
18
|
throw new Error('Cannot run interactive auth in CI');
|
|
19
19
|
}
|
|
20
|
-
if (!clientId) {
|
|
21
|
-
throw new Error('Missing client id');
|
|
22
|
-
}
|
|
23
20
|
const tokenSet = await auth({
|
|
24
21
|
oauthHost: oauthHost,
|
|
25
22
|
clientId: clientId,
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { beforeAll, describe, test, jest, afterAll, expect, } from '@jest/globals';
|
|
3
|
+
import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { startOauthServer } from '../test_utils/oauth_server';
|
|
5
|
+
jest.unstable_mockModule('open', () => ({
|
|
6
|
+
__esModule: true,
|
|
7
|
+
default: jest.fn((url) => {
|
|
8
|
+
axios.get(url);
|
|
9
|
+
}),
|
|
10
|
+
}));
|
|
11
|
+
// "open" module should be imported after mocking
|
|
12
|
+
const authModule = await import('./auth');
|
|
13
|
+
describe('auth', () => {
|
|
14
|
+
let configDir = '';
|
|
15
|
+
let server;
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
configDir = mkdtempSync('test-config');
|
|
18
|
+
server = await startOauthServer();
|
|
19
|
+
});
|
|
20
|
+
afterAll(() => {
|
|
21
|
+
rmSync(configDir, { recursive: true });
|
|
22
|
+
server.stop();
|
|
23
|
+
});
|
|
24
|
+
test('should auth', async () => {
|
|
25
|
+
await authModule.authFlow({
|
|
26
|
+
_: ['auth'],
|
|
27
|
+
apiHost: 'http://localhost:1111',
|
|
28
|
+
clientId: 'test-client-id',
|
|
29
|
+
configDir,
|
|
30
|
+
forceAuth: true,
|
|
31
|
+
oauthHost: 'http://localhost:7777',
|
|
32
|
+
});
|
|
33
|
+
const credentials = JSON.parse(readFileSync(`${configDir}/credentials.json`, 'utf-8'));
|
|
34
|
+
expect(credentials.access_token).toEqual(expect.any(String));
|
|
35
|
+
expect(credentials.refresh_token).toEqual(expect.any(String));
|
|
36
|
+
});
|
|
37
|
+
});
|
package/commands/help.test.js
CHANGED
package/commands/roles.test.js
CHANGED
package/config.js
CHANGED
|
@@ -3,8 +3,8 @@ import { homedir } from 'node:os';
|
|
|
3
3
|
import { existsSync, mkdirSync } from 'node:fs';
|
|
4
4
|
import { isCi } from './env.js';
|
|
5
5
|
export const defaultDir = join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'neonctl');
|
|
6
|
-
export const ensureConfigDir = async ({ 'config-dir': configDir, }) => {
|
|
7
|
-
if (!existsSync(configDir) && !isCi()) {
|
|
6
|
+
export const ensureConfigDir = async ({ 'config-dir': configDir, 'force-auth': forceAuth, }) => {
|
|
7
|
+
if (!existsSync(configDir) && (!isCi() || forceAuth)) {
|
|
8
8
|
mkdirSync(configDir, { recursive: true });
|
|
9
9
|
}
|
|
10
10
|
};
|
package/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
1
2
|
import yargs from 'yargs';
|
|
2
3
|
import { hideBin } from 'yargs/helpers';
|
|
3
4
|
import axiosDebug from 'axios-debug-log';
|
|
@@ -46,6 +47,12 @@ builder = builder
|
|
|
46
47
|
group: 'Global options:',
|
|
47
48
|
type: 'string',
|
|
48
49
|
default: defaultDir,
|
|
50
|
+
})
|
|
51
|
+
.option('force-auth', {
|
|
52
|
+
describe: 'Force authentication',
|
|
53
|
+
type: 'boolean',
|
|
54
|
+
hidden: true,
|
|
55
|
+
default: false,
|
|
49
56
|
})
|
|
50
57
|
.middleware(ensureConfigDir)
|
|
51
58
|
// Auth flow
|
|
@@ -87,6 +94,7 @@ builder = builder
|
|
|
87
94
|
.group('help', 'Global options:')
|
|
88
95
|
.alias('help', 'h')
|
|
89
96
|
.completion()
|
|
97
|
+
.scriptName(basename(process.argv[1]) === 'neon' ? 'neon' : 'neonctl')
|
|
90
98
|
.fail(async (msg, err) => {
|
|
91
99
|
if (isAxiosError(err)) {
|
|
92
100
|
if (err.code === 'ECONNABORTED') {
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"url": "git@github.com:neondatabase/neonctl.git"
|
|
6
6
|
},
|
|
7
7
|
"type": "module",
|
|
8
|
-
"version": "1.18.
|
|
8
|
+
"version": "1.18.1",
|
|
9
9
|
"description": "CLI tool for NeonDB Cloud management",
|
|
10
10
|
"main": "index.js",
|
|
11
11
|
"author": "NeonDB",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"husky": "^8.0.3",
|
|
42
42
|
"jest": "^29.5.0",
|
|
43
43
|
"lint-staged": "^13.0.3",
|
|
44
|
+
"oauth2-mock-server": "^6.0.0",
|
|
44
45
|
"pkg": "^5.8.1",
|
|
45
46
|
"prettier": "^2.7.1",
|
|
46
47
|
"rollup": "^3.26.2",
|
|
@@ -84,7 +85,7 @@
|
|
|
84
85
|
"lint": "tsc --noEmit && eslint src --ext .ts",
|
|
85
86
|
"build": "npm run generateParams && npm run clean && tsc && cp src/*.html package*.json README.md ./dist",
|
|
86
87
|
"clean": "rm -rf dist",
|
|
87
|
-
"generateParams": "ts-node
|
|
88
|
+
"generateParams": "node --loader ts-node/esm ./generateOptionsFromSpec.ts",
|
|
88
89
|
"start": "node src/index.js",
|
|
89
90
|
"test": "node --experimental-vm-modules node_modules/.bin/jest"
|
|
90
91
|
},
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import emocks from 'emocks';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { log } from '../log';
|
|
5
|
+
export const runMockServer = async (mockDir) => new Promise((resolve) => {
|
|
6
|
+
const app = express();
|
|
7
|
+
app.use(express.json());
|
|
8
|
+
app.use('/', emocks(join(process.cwd(), 'mocks', mockDir)));
|
|
9
|
+
const server = app.listen(0);
|
|
10
|
+
server.on('listening', () => {
|
|
11
|
+
log.info('Mock server listening at %d', server.address().port);
|
|
12
|
+
});
|
|
13
|
+
resolve(server);
|
|
14
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { log } from '../log';
|
|
2
|
+
import { OAuth2Server } from 'oauth2-mock-server';
|
|
3
|
+
export const startOauthServer = async () => {
|
|
4
|
+
const server = new OAuth2Server();
|
|
5
|
+
await server.issuer.keys.generate('RS256');
|
|
6
|
+
await server.start(7777, 'localhost');
|
|
7
|
+
log.info('Started OAuth server');
|
|
8
|
+
return server;
|
|
9
|
+
};
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
1
|
import { test, expect, describe, beforeAll, afterAll } from '@jest/globals';
|
|
3
|
-
import emocks from 'emocks';
|
|
4
|
-
import express from 'express';
|
|
5
2
|
import { fork } from 'node:child_process';
|
|
6
3
|
import { join } from 'node:path';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
app.use(express.json());
|
|
10
|
-
app.use('/', emocks(join(process.cwd(), 'mocks', mockDir)));
|
|
11
|
-
const server = app.listen(0);
|
|
12
|
-
server.on('listening', () => {
|
|
13
|
-
console.log(`Mock server listening at ${server.address().port}`);
|
|
14
|
-
});
|
|
15
|
-
resolve(server);
|
|
16
|
-
});
|
|
4
|
+
import { log } from '../log.js';
|
|
5
|
+
import { runMockServer } from './mock_server.js';
|
|
17
6
|
export const testCliCommand = ({ args, name, expected, mockDir = 'main', }) => {
|
|
18
7
|
let server;
|
|
19
8
|
describe(name, () => {
|
|
@@ -33,10 +22,10 @@ export const testCliCommand = ({ args, name, expected, mockDir = 'main', }) => {
|
|
|
33
22
|
const cp = fork(join(process.cwd(), './dist/index.js'), [
|
|
34
23
|
'--api-host',
|
|
35
24
|
`http://localhost:${server.address().port}`,
|
|
36
|
-
'--api-key',
|
|
37
|
-
'test-key',
|
|
38
25
|
'--output',
|
|
39
26
|
'yaml',
|
|
27
|
+
'--api-key',
|
|
28
|
+
'test-key',
|
|
40
29
|
...args,
|
|
41
30
|
], {
|
|
42
31
|
stdio: 'pipe',
|
|
@@ -47,15 +36,13 @@ export const testCliCommand = ({ args, name, expected, mockDir = 'main', }) => {
|
|
|
47
36
|
});
|
|
48
37
|
cp.stderr?.on('data', (data) => {
|
|
49
38
|
error += data.toString();
|
|
39
|
+
log.error(data.toString());
|
|
50
40
|
});
|
|
51
41
|
cp.on('error', (err) => {
|
|
52
42
|
throw err;
|
|
53
43
|
});
|
|
54
44
|
cp.on('close', (code) => {
|
|
55
45
|
try {
|
|
56
|
-
if (code !== 0 && error) {
|
|
57
|
-
console.error(error);
|
|
58
|
-
}
|
|
59
46
|
expect(code).toBe(0);
|
|
60
47
|
if (code === 0 && expected) {
|
|
61
48
|
if (expected.snapshot) {
|