neonctl 1.29.1 → 1.29.4

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.
Files changed (184) hide show
  1. package/analytics.js +78 -0
  2. package/api.js +35 -0
  3. package/auth.js +101 -0
  4. package/{src/cli.ts → cli.js} +0 -1
  5. package/commands/auth.js +102 -0
  6. package/commands/auth.test.js +42 -0
  7. package/commands/branches.js +303 -0
  8. package/commands/branches.test.js +321 -0
  9. package/commands/connection_string.js +158 -0
  10. package/commands/connection_string.test.js +253 -0
  11. package/commands/databases.js +79 -0
  12. package/commands/databases.test.js +51 -0
  13. package/commands/help.test.js +11 -0
  14. package/{src/commands/index.ts → commands/index.js} +10 -11
  15. package/commands/ip_allow.js +135 -0
  16. package/commands/ip_allow.test.js +78 -0
  17. package/commands/operations.js +28 -0
  18. package/commands/operations.test.js +11 -0
  19. package/commands/projects.js +186 -0
  20. package/commands/projects.test.js +132 -0
  21. package/commands/roles.js +57 -0
  22. package/commands/roles.test.js +42 -0
  23. package/commands/set_context.js +22 -0
  24. package/commands/set_context.test.js +53 -0
  25. package/commands/user.js +15 -0
  26. package/config.js +11 -0
  27. package/context.js +48 -0
  28. package/env.js +6 -0
  29. package/errors.js +16 -0
  30. package/help.js +146 -0
  31. package/index.js +168 -0
  32. package/log.js +15 -0
  33. package/package.json +1 -1
  34. package/parameters.gen.js +322 -0
  35. package/pkg.js +3 -45
  36. package/test_utils/mock_server.js +16 -0
  37. package/test_utils/oauth_server.js +9 -0
  38. package/test_utils/test_cli_command.js +80 -0
  39. package/types.js +1 -0
  40. package/utils/enrichers.js +49 -0
  41. package/utils/formats.js +5 -0
  42. package/utils/formats.test.js +32 -0
  43. package/utils/middlewares.js +20 -0
  44. package/utils/point_in_time.js +50 -0
  45. package/utils/psql.js +24 -0
  46. package/utils/string.js +5 -0
  47. package/utils/ui.js +59 -0
  48. package/writer.js +87 -0
  49. package/writer.test.js +86 -0
  50. package/.bump +0 -1
  51. package/.editorconfig +0 -7
  52. package/.eslintrc.cjs +0 -15
  53. package/.github/workflows/commitlint.yml +0 -46
  54. package/.github/workflows/pr.yml +0 -25
  55. package/.github/workflows/release.yml +0 -30
  56. package/.husky/commit-msg +0 -4
  57. package/.husky/pre-commit +0 -4
  58. package/.nvmrc +0 -1
  59. package/.prettierignore +0 -3
  60. package/.prettierrc.json +0 -3
  61. package/.releaserc.json +0 -47
  62. package/LICENSE +0 -202
  63. package/commitlint.config.cjs +0 -7
  64. package/generateOptionsFromSpec.ts +0 -68
  65. package/jest/setup.js +0 -5
  66. package/jest.config.ts +0 -199
  67. package/mocks/bin/psql.cjs +0 -9
  68. package/mocks/main/projects/GET.js +0 -27
  69. package/mocks/main/projects/POST.js +0 -22
  70. package/mocks/main/projects/shared/GET.js +0 -16
  71. package/mocks/main/projects/test/DELETE.json +0 -7
  72. package/mocks/main/projects/test/GET.json +0 -13
  73. package/mocks/main/projects/test/PATCH.js +0 -18
  74. package/mocks/main/projects/test/branches/GET.json +0 -25
  75. package/mocks/main/projects/test/branches/POST.js +0 -83
  76. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/DELETE.json +0 -7
  77. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/GET.json +0 -9
  78. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/PATCH.js +0 -14
  79. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/databases/GET.json +0 -6
  80. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/databases/POST.js +0 -13
  81. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/databases/test_db/DELETE.json +0 -6
  82. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/endpoints/GET.json +0 -26
  83. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/endpoints/POST.json +0 -6
  84. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/GET.json +0 -3
  85. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/POST.js +0 -14
  86. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/test_role/DELETE.json +0 -6
  87. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/test_role/reveal_password/GET.json +0 -3
  88. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/set_as_primary/POST.json +0 -9
  89. package/mocks/main/projects/test/branches/br-numbered-branch-123456/GET.json +0 -10
  90. package/mocks/main/projects/test/branches/br-sunny-branch-123456/DELETE.json +0 -7
  91. package/mocks/main/projects/test/branches/br-sunny-branch-123456/GET.json +0 -10
  92. package/mocks/main/projects/test/branches/br-sunny-branch-123456/PATCH.js +0 -14
  93. package/mocks/main/projects/test/branches/br-sunny-branch-123456/databases/GET.json +0 -6
  94. package/mocks/main/projects/test/branches/br-sunny-branch-123456/databases/POST.js +0 -13
  95. package/mocks/main/projects/test/branches/br-sunny-branch-123456/databases/test_db/DELETE.json +0 -6
  96. package/mocks/main/projects/test/branches/br-sunny-branch-123456/endpoints/GET.json +0 -26
  97. package/mocks/main/projects/test/branches/br-sunny-branch-123456/endpoints/POST.json +0 -6
  98. package/mocks/main/projects/test/branches/br-sunny-branch-123456/restore/POST.js +0 -16
  99. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/GET.json +0 -3
  100. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/POST.js +0 -14
  101. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/test_role/DELETE.json +0 -6
  102. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/test_role/reveal_password/GET.json +0 -3
  103. package/mocks/main/projects/test/branches/br-sunny-branch-123456/set_as_primary/POST.json +0 -9
  104. package/mocks/main/projects/test/endpoints/GET.json +0 -9
  105. package/mocks/main/projects/test/endpoints/POST.js +0 -32
  106. package/mocks/main/projects/test/endpoints/test_endpoint_id/DELETE.json +0 -7
  107. package/mocks/main/projects/test/endpoints/test_endpoint_id/GET.json +0 -9
  108. package/mocks/main/projects/test/endpoints/test_endpoint_id/PATCH.js +0 -17
  109. package/mocks/main/projects/test/operations/GET.json +0 -22
  110. package/mocks/main/users/me/GET.json +0 -5
  111. package/mocks/restore/projects/test/branches/GET.json +0 -21
  112. package/mocks/restore/projects/test/branches/br-another-branch-123456/GET.json +0 -6
  113. package/mocks/restore/projects/test/branches/br-another-branch-123456/restore/POST.js +0 -13
  114. package/mocks/restore/projects/test/branches/br-any-branch-123456/GET.json +0 -6
  115. package/mocks/restore/projects/test/branches/br-parent-tots-123456/GET.json +0 -7
  116. package/mocks/restore/projects/test/branches/br-parent-tots-123456/restore/POST.js +0 -14
  117. package/mocks/restore/projects/test/branches/br-self-tolsn-123456/GET.json +0 -6
  118. package/mocks/restore/projects/test/branches/br-self-tolsn-123456/restore/POST.js +0 -15
  119. package/mocks/single_project/projects/GET.json +0 -10
  120. package/mocks/single_project/projects/test-project-123456/GET.json +0 -14
  121. package/mocks/single_project/projects/test-project-123456/branches/GET.json +0 -11
  122. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/databases/GET.json +0 -3
  123. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/endpoints/GET.json +0 -10
  124. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/roles/GET.json +0 -3
  125. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/roles/test_role/reveal_password/GET.json +0 -3
  126. package/rollup.config.js +0 -20
  127. package/snapshots/commands/branches.test.snap +0 -221
  128. package/snapshots/commands/connection_string.test.snap +0 -70
  129. package/snapshots/commands/databases.test.snap +0 -20
  130. package/snapshots/commands/ip_allow.test.snap +0 -55
  131. package/snapshots/commands/operations.test.snap +0 -17
  132. package/snapshots/commands/projects.test.snap +0 -141
  133. package/snapshots/commands/roles.test.snap +0 -19
  134. package/snapshots/commands/set_context.test.snap +0 -30
  135. package/snapshots/writer.test.snap +0 -60
  136. package/snapshotsResolver.cjs +0 -32
  137. package/src/analytics.ts +0 -95
  138. package/src/api.ts +0 -44
  139. package/src/auth.ts +0 -137
  140. package/src/commands/auth.test.ts +0 -62
  141. package/src/commands/auth.ts +0 -148
  142. package/src/commands/branches.test.ts +0 -354
  143. package/src/commands/branches.ts +0 -451
  144. package/src/commands/connection_string.test.ts +0 -250
  145. package/src/commands/connection_string.ts +0 -210
  146. package/src/commands/databases.test.ts +0 -55
  147. package/src/commands/databases.ts +0 -129
  148. package/src/commands/help.test.ts +0 -13
  149. package/src/commands/ip_allow.test.ts +0 -86
  150. package/src/commands/ip_allow.ts +0 -202
  151. package/src/commands/operations.test.ts +0 -13
  152. package/src/commands/operations.ts +0 -41
  153. package/src/commands/projects.test.ts +0 -147
  154. package/src/commands/projects.ts +0 -275
  155. package/src/commands/roles.test.ts +0 -46
  156. package/src/commands/roles.ts +0 -100
  157. package/src/commands/set_context.test.ts +0 -64
  158. package/src/commands/set_context.ts +0 -27
  159. package/src/commands/user.ts +0 -21
  160. package/src/config.ts +0 -22
  161. package/src/context.ts +0 -61
  162. package/src/env.ts +0 -7
  163. package/src/errors.ts +0 -24
  164. package/src/help.ts +0 -185
  165. package/src/index.ts +0 -180
  166. package/src/log.ts +0 -16
  167. package/src/parameters.gen.ts +0 -332
  168. package/src/pkg.ts +0 -9
  169. package/src/test_utils/mock_server.ts +0 -27
  170. package/src/test_utils/oauth_server.ts +0 -10
  171. package/src/test_utils/test_cli_command.ts +0 -117
  172. package/src/types.ts +0 -25
  173. package/src/utils/enrichers.ts +0 -73
  174. package/src/utils/formats.test.ts +0 -41
  175. package/src/utils/formats.ts +0 -11
  176. package/src/utils/middlewares.ts +0 -23
  177. package/src/utils/point_in_time.ts +0 -86
  178. package/src/utils/psql.ts +0 -29
  179. package/src/utils/string.ts +0 -8
  180. package/src/utils/ui.ts +0 -64
  181. package/src/writer.test.ts +0 -98
  182. package/src/writer.ts +0 -131
  183. package/tsconfig.json +0 -17
  184. /package/{src/callback.html → callback.html} +0 -0
@@ -0,0 +1,186 @@
1
+ import { log } from '../log.js';
2
+ import { projectCreateRequest, projectUpdateRequest, } from '../parameters.gen.js';
3
+ import { writer } from '../writer.js';
4
+ import { psql } from '../utils/psql.js';
5
+ import { updateContextFile } from '../context.js';
6
+ const PROJECT_FIELDS = ['id', 'name', 'region_id', 'created_at'];
7
+ const REGIONS = [
8
+ 'aws-us-west-2',
9
+ 'aws-ap-southeast-1',
10
+ 'aws-eu-central-1',
11
+ 'aws-us-east-2',
12
+ 'aws-us-east-1',
13
+ ];
14
+ const PROJECTS_LIST_LIMIT = 100;
15
+ export const command = 'projects';
16
+ export const describe = 'Manage projects';
17
+ export const aliases = ['project'];
18
+ export const builder = (argv) => {
19
+ return argv
20
+ .usage('$0 projects <sub-command> [options]')
21
+ .command('list', 'List projects', (yargs) => yargs, async (args) => {
22
+ await list(args);
23
+ })
24
+ .command('create', 'Create a project', (yargs) => yargs.options({
25
+ name: {
26
+ describe: projectCreateRequest['project.name'].description,
27
+ type: 'string',
28
+ },
29
+ 'region-id': {
30
+ describe: `The region ID. Possible values: ${REGIONS.join(', ')}`,
31
+ type: 'string',
32
+ },
33
+ psql: {
34
+ type: 'boolean',
35
+ describe: 'Connect to a new project via psql',
36
+ default: false,
37
+ },
38
+ database: {
39
+ describe: projectCreateRequest['project.branch.database_name'].description,
40
+ type: 'string',
41
+ },
42
+ role: {
43
+ describe: projectCreateRequest['project.branch.role_name'].description,
44
+ type: 'string',
45
+ },
46
+ 'set-context': {
47
+ type: 'boolean',
48
+ describe: 'Set the current context to the new project',
49
+ default: false,
50
+ },
51
+ }), async (args) => {
52
+ await create(args);
53
+ })
54
+ .command('update <id>', 'Update a project', (yargs) => yargs.options({
55
+ name: {
56
+ describe: projectCreateRequest['project.name'].description,
57
+ type: 'string',
58
+ },
59
+ 'ip-allow': {
60
+ describe: projectUpdateRequest['project.settings.allowed_ips.ips']
61
+ .description,
62
+ type: 'string',
63
+ array: true,
64
+ deprecated: "Deprecated. Use 'ip-allow' command",
65
+ },
66
+ 'ip-primary-only': {
67
+ describe: projectUpdateRequest['project.settings.allowed_ips.primary_branch_only'].description,
68
+ type: 'boolean',
69
+ deprecated: "Deprecated. Use 'ip-allow' command",
70
+ },
71
+ }), async (args) => {
72
+ await update(args);
73
+ })
74
+ .command('delete <id>', 'Delete a project', (yargs) => yargs, async (args) => {
75
+ await deleteProject(args);
76
+ })
77
+ .command('get <id>', 'Get a project', (yargs) => yargs, async (args) => {
78
+ await get(args);
79
+ });
80
+ };
81
+ export const handler = (args) => {
82
+ return args;
83
+ };
84
+ const list = async (props) => {
85
+ const getList = async (fn) => {
86
+ const result = [];
87
+ let cursor;
88
+ let end = false;
89
+ while (!end) {
90
+ const { data } = await fn({
91
+ limit: PROJECTS_LIST_LIMIT,
92
+ cursor,
93
+ });
94
+ result.push(...data.projects);
95
+ cursor = data.pagination?.cursor;
96
+ log.debug('Got %d projects, with cursor: %s', data.projects.length, cursor);
97
+ if (data.projects.length < PROJECTS_LIST_LIMIT) {
98
+ end = true;
99
+ }
100
+ }
101
+ return result;
102
+ };
103
+ const [ownedProjects, sharedProjects] = await Promise.all([
104
+ getList(props.apiClient.listProjects),
105
+ getList(props.apiClient.listSharedProjects),
106
+ ]);
107
+ const out = writer(props);
108
+ out.write(ownedProjects, {
109
+ fields: PROJECT_FIELDS,
110
+ title: 'Projects',
111
+ });
112
+ out.write(sharedProjects, {
113
+ fields: PROJECT_FIELDS,
114
+ title: 'Shared with me',
115
+ });
116
+ out.end();
117
+ };
118
+ const create = async (props) => {
119
+ const project = {};
120
+ if (props.name) {
121
+ project.name = props.name;
122
+ }
123
+ if (props.regionId) {
124
+ project.region_id = props.regionId;
125
+ }
126
+ project.branch = {};
127
+ if (props.database) {
128
+ project.branch.database_name = props.database;
129
+ }
130
+ if (props.role) {
131
+ project.branch.role_name = props.role;
132
+ }
133
+ const { data } = await props.apiClient.createProject({
134
+ project,
135
+ });
136
+ if (props.setContext) {
137
+ updateContextFile(props.contextFile, {
138
+ projectId: data.project.id,
139
+ branchId: data.branch.id,
140
+ });
141
+ }
142
+ const out = writer(props);
143
+ out.write(data.project, { fields: PROJECT_FIELDS, title: 'Project' });
144
+ out.write(data.connection_uris, {
145
+ fields: ['connection_uri'],
146
+ title: 'Connection URIs',
147
+ });
148
+ out.end();
149
+ if (props.psql) {
150
+ const connection_uri = data.connection_uris[0].connection_uri;
151
+ const psqlArgs = props['--'];
152
+ await psql(connection_uri, psqlArgs);
153
+ }
154
+ };
155
+ const deleteProject = async (props) => {
156
+ const { data } = await props.apiClient.deleteProject(props.id);
157
+ writer(props).end(data.project, {
158
+ fields: PROJECT_FIELDS,
159
+ });
160
+ };
161
+ const update = async (props) => {
162
+ const project = {};
163
+ if (props.name) {
164
+ project.name = props.name;
165
+ }
166
+ if (props.ipAllow || props.ipPrimaryOnly != undefined) {
167
+ const { data } = await props.apiClient.getProject(props.id);
168
+ const existingAllowedIps = data.project.settings?.allowed_ips;
169
+ project.settings = {
170
+ allowed_ips: {
171
+ ips: props.ipAllow ?? existingAllowedIps?.ips ?? [],
172
+ primary_branch_only: props.ipPrimaryOnly ??
173
+ existingAllowedIps?.primary_branch_only ??
174
+ false,
175
+ },
176
+ };
177
+ }
178
+ const { data } = await props.apiClient.updateProject(props.id, {
179
+ project,
180
+ });
181
+ writer(props).end(data.project, { fields: PROJECT_FIELDS });
182
+ };
183
+ const get = async (props) => {
184
+ const { data } = await props.apiClient.getProject(props.id);
185
+ writer(props).end(data.project, { fields: PROJECT_FIELDS });
186
+ };
@@ -0,0 +1,132 @@
1
+ import { afterAll, describe, expect, test } from '@jest/globals';
2
+ import { readFileSync, rmSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { testCliCommand } from '../test_utils/test_cli_command.js';
6
+ const CONTEXT_FILE = join(tmpdir(), `neon_project_create_ctx_${Date.now()}`);
7
+ describe('projects', () => {
8
+ testCliCommand({
9
+ name: 'list',
10
+ args: ['projects', 'list'],
11
+ expected: {
12
+ snapshot: true,
13
+ },
14
+ });
15
+ testCliCommand({
16
+ name: 'create',
17
+ args: ['projects', 'create', '--name', 'test_project'],
18
+ expected: {
19
+ snapshot: true,
20
+ },
21
+ });
22
+ testCliCommand({
23
+ name: 'create with database and role',
24
+ args: [
25
+ 'projects',
26
+ 'create',
27
+ '--name',
28
+ 'test_project',
29
+ '--database',
30
+ 'test_db',
31
+ '--role',
32
+ 'test_role',
33
+ ],
34
+ expected: {
35
+ snapshot: true,
36
+ },
37
+ });
38
+ testCliCommand({
39
+ name: 'create and connect with psql',
40
+ args: ['projects', 'create', '--name', 'test_project', '--psql'],
41
+ expected: {
42
+ snapshot: true,
43
+ },
44
+ });
45
+ testCliCommand({
46
+ name: 'create and connect with psql and psql args',
47
+ args: [
48
+ 'projects',
49
+ 'create',
50
+ '--name',
51
+ 'test_project',
52
+ '--psql',
53
+ '--',
54
+ '-c',
55
+ 'SELECT 1',
56
+ ],
57
+ expected: {
58
+ snapshot: true,
59
+ },
60
+ });
61
+ testCliCommand({
62
+ name: 'create project with setting the context',
63
+ args: [
64
+ 'projects',
65
+ 'create',
66
+ '--name',
67
+ 'test_project',
68
+ '--context-file',
69
+ CONTEXT_FILE,
70
+ '--set-context',
71
+ ],
72
+ expected: {
73
+ snapshot: true,
74
+ },
75
+ });
76
+ afterAll(() => {
77
+ rmSync(CONTEXT_FILE);
78
+ });
79
+ test('context file should exist and contain the project id', () => {
80
+ expect(readFileSync(CONTEXT_FILE, 'utf-8')).toContain('new-project-123456');
81
+ });
82
+ testCliCommand({
83
+ name: 'delete',
84
+ args: ['projects', 'delete', 'test'],
85
+ expected: {
86
+ snapshot: true,
87
+ },
88
+ });
89
+ testCliCommand({
90
+ name: 'update name',
91
+ args: ['projects', 'update', 'test', '--name', 'test_project_new_name'],
92
+ expected: {
93
+ snapshot: true,
94
+ },
95
+ });
96
+ testCliCommand({
97
+ name: 'update ip allow',
98
+ args: [
99
+ 'projects',
100
+ 'update',
101
+ 'test',
102
+ '--ip-allow',
103
+ '127.0.0.1',
104
+ '192.168.1.2/22',
105
+ '--ip-primary-only',
106
+ ],
107
+ expected: {
108
+ snapshot: true,
109
+ },
110
+ });
111
+ testCliCommand({
112
+ name: 'update ip allow primary only flag',
113
+ args: ['projects', 'update', 'test', '--ip-primary-only', 'false'],
114
+ expected: {
115
+ snapshot: true,
116
+ },
117
+ });
118
+ testCliCommand({
119
+ name: 'update ip allow remove',
120
+ args: ['projects', 'update', 'test', '--ip-allow'],
121
+ expected: {
122
+ snapshot: true,
123
+ },
124
+ });
125
+ testCliCommand({
126
+ name: 'get',
127
+ args: ['projects', 'get', 'test'],
128
+ expected: {
129
+ snapshot: true,
130
+ },
131
+ });
132
+ });
@@ -0,0 +1,57 @@
1
+ import { retryOnLock } from '../api.js';
2
+ import { branchIdFromProps, fillSingleProject } from '../utils/enrichers.js';
3
+ import { writer } from '../writer.js';
4
+ const ROLES_FIELDS = ['name', 'created_at'];
5
+ export const command = 'roles';
6
+ export const describe = 'Manage roles';
7
+ export const aliases = ['role'];
8
+ export const builder = (argv) => argv
9
+ .usage('$0 roles <sub-command> [options]')
10
+ .options({
11
+ 'project-id': {
12
+ describe: 'Project ID',
13
+ type: 'string',
14
+ },
15
+ branch: {
16
+ describe: 'Branch ID or name',
17
+ type: 'string',
18
+ },
19
+ })
20
+ .middleware(fillSingleProject)
21
+ .command('list', 'List roles', (yargs) => yargs, async (args) => await list(args))
22
+ .command('create', 'Create a role', (yargs) => yargs.options({
23
+ name: {
24
+ describe: 'Role name',
25
+ type: 'string',
26
+ demandOption: true,
27
+ },
28
+ }), async (args) => await create(args))
29
+ .command('delete <role>', 'Delete a role', (yargs) => yargs, async (args) => await deleteRole(args));
30
+ export const handler = (args) => {
31
+ return args;
32
+ };
33
+ export const list = async (props) => {
34
+ const branchId = await branchIdFromProps(props);
35
+ const { data } = await props.apiClient.listProjectBranchRoles(props.projectId, branchId);
36
+ writer(props).end(data.roles, {
37
+ fields: ROLES_FIELDS,
38
+ });
39
+ };
40
+ export const create = async (props) => {
41
+ const branchId = await branchIdFromProps(props);
42
+ const { data } = await retryOnLock(() => props.apiClient.createProjectBranchRole(props.projectId, branchId, {
43
+ role: {
44
+ name: props.name,
45
+ },
46
+ }));
47
+ writer(props).end(data.role, {
48
+ fields: ROLES_FIELDS,
49
+ });
50
+ };
51
+ export const deleteRole = async (props) => {
52
+ const branchId = await branchIdFromProps(props);
53
+ const { data } = await retryOnLock(() => props.apiClient.deleteProjectBranchRole(props.projectId, branchId, props.role));
54
+ writer(props).end(data.role, {
55
+ fields: ROLES_FIELDS,
56
+ });
57
+ };
@@ -0,0 +1,42 @@
1
+ import { describe } from '@jest/globals';
2
+ import { testCliCommand } from '../test_utils/test_cli_command.js';
3
+ describe('roles', () => {
4
+ testCliCommand({
5
+ name: 'list',
6
+ args: ['roles', 'list', '--project-id', 'test', '--branch', 'test_branch'],
7
+ expected: {
8
+ snapshot: true,
9
+ },
10
+ });
11
+ testCliCommand({
12
+ name: 'create',
13
+ args: [
14
+ 'roles',
15
+ 'create',
16
+ '--project-id',
17
+ 'test',
18
+ '--branch',
19
+ 'test_branch',
20
+ '--name',
21
+ 'test_role',
22
+ ],
23
+ expected: {
24
+ snapshot: true,
25
+ },
26
+ });
27
+ testCliCommand({
28
+ name: 'delete',
29
+ args: [
30
+ 'roles',
31
+ 'delete',
32
+ 'test_role',
33
+ '--project-id',
34
+ 'test',
35
+ '--branch',
36
+ 'test_branch',
37
+ ],
38
+ expected: {
39
+ snapshot: true,
40
+ },
41
+ });
42
+ });
@@ -0,0 +1,22 @@
1
+ import { updateContextFile } from '../context.js';
2
+ import { branchIdFromProps } from '../utils/enrichers.js';
3
+ export const command = 'set-context';
4
+ export const describe = 'Set the current context';
5
+ export const builder = (argv) => argv.usage('$0 set-context [options]').options({
6
+ 'project-id': {
7
+ describe: 'Project ID',
8
+ type: 'string',
9
+ },
10
+ branch: {
11
+ describe: 'Branch ID or name',
12
+ type: 'string',
13
+ },
14
+ });
15
+ export const handler = async (props) => {
16
+ const branchId = await branchIdFromProps(props);
17
+ const context = {
18
+ projectId: props.projectId,
19
+ branchId,
20
+ };
21
+ updateContextFile(props.contextFile, context);
22
+ };
@@ -0,0 +1,53 @@
1
+ import { tmpdir } from 'node:os';
2
+ import { join } from 'node:path';
3
+ import { rmSync, writeFileSync } from 'node:fs';
4
+ import { afterAll, describe } from '@jest/globals';
5
+ import { testCliCommand } from '../test_utils/test_cli_command';
6
+ const CONTEXT_FILE = join(tmpdir(), `neon_${Date.now()}`);
7
+ describe('set_context', () => {
8
+ afterAll(() => {
9
+ rmSync(CONTEXT_FILE);
10
+ });
11
+ describe('should set the context', () => {
12
+ testCliCommand({
13
+ name: 'set-context',
14
+ args: [
15
+ 'set-context',
16
+ '--project-id',
17
+ 'test',
18
+ '--context-file',
19
+ CONTEXT_FILE,
20
+ ],
21
+ });
22
+ testCliCommand({
23
+ name: 'list branches selecting project from the context',
24
+ args: ['branches', 'list', '--context-file', CONTEXT_FILE],
25
+ expected: {
26
+ snapshot: true,
27
+ },
28
+ });
29
+ const overrideContextFile = join(tmpdir(), `neon_override_ctx_${Date.now()}`);
30
+ testCliCommand({
31
+ name: 'get branch id overrides context set branch',
32
+ before: async () => {
33
+ writeFileSync(overrideContextFile, JSON.stringify({
34
+ projectId: 'test',
35
+ branchId: 'br-cloudy-branch-12345678',
36
+ }));
37
+ },
38
+ after: async () => {
39
+ rmSync(overrideContextFile);
40
+ },
41
+ args: [
42
+ 'branches',
43
+ 'get',
44
+ 'br-sunny-branch-123456',
45
+ '--context-file',
46
+ overrideContextFile,
47
+ ],
48
+ expected: {
49
+ snapshot: true,
50
+ },
51
+ });
52
+ });
53
+ });
@@ -0,0 +1,15 @@
1
+ import { writer } from '../writer.js';
2
+ export const command = 'me';
3
+ export const describe = 'Show current user';
4
+ export const builder = (yargs) => yargs.option('context-file', {
5
+ hidden: true,
6
+ });
7
+ export const handler = async (args) => {
8
+ await me(args);
9
+ };
10
+ const me = async (props) => {
11
+ const { data } = await props.apiClient.getCurrentUserInfo();
12
+ writer(props).end(data, {
13
+ fields: ['login', 'email', 'name', 'projects_limit'],
14
+ });
15
+ };
package/config.js ADDED
@@ -0,0 +1,11 @@
1
+ import { join } from 'node:path';
2
+ import { homedir } from 'node:os';
3
+ import { existsSync, mkdirSync } from 'node:fs';
4
+ import { isCi } from './env.js';
5
+ export const CREDENTIALS_FILE = 'credentials.json';
6
+ export const defaultDir = join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'neonctl');
7
+ export const ensureConfigDir = async ({ 'config-dir': configDir, 'force-auth': forceAuth, }) => {
8
+ if (!existsSync(configDir) && (!isCi() || forceAuth)) {
9
+ mkdirSync(configDir, { recursive: true });
10
+ }
11
+ };
package/context.js ADDED
@@ -0,0 +1,48 @@
1
+ import { accessSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { normalize, resolve } from 'node:path';
4
+ const CONTEXT_FILE = '.neon';
5
+ const CHECK_FILES = [CONTEXT_FILE, 'package.json', '.git'];
6
+ const wrapWithContextFile = (dir) => resolve(dir, CONTEXT_FILE);
7
+ export const currentContextFile = () => {
8
+ const cwd = process.cwd();
9
+ let currentDir = cwd;
10
+ const root = normalize('/');
11
+ const home = homedir();
12
+ while (currentDir !== root && currentDir !== home) {
13
+ for (const file of CHECK_FILES) {
14
+ try {
15
+ accessSync(resolve(currentDir, file));
16
+ return wrapWithContextFile(currentDir);
17
+ }
18
+ catch (e) {
19
+ // ignore
20
+ }
21
+ }
22
+ currentDir = resolve(currentDir, '..');
23
+ }
24
+ return wrapWithContextFile(cwd);
25
+ };
26
+ export const readContextFile = (file) => {
27
+ try {
28
+ return JSON.parse(readFileSync(file, 'utf-8'));
29
+ }
30
+ catch (e) {
31
+ return {};
32
+ }
33
+ };
34
+ export const enrichFromContext = (args) => {
35
+ if (args._[0] === 'set-context') {
36
+ return;
37
+ }
38
+ const context = readContextFile(args.contextFile);
39
+ if (!args.branch && !args.id && !args.name) {
40
+ args.branch = context.branchId;
41
+ }
42
+ if (!args.projectId) {
43
+ args.projectId = context.projectId;
44
+ }
45
+ };
46
+ export const updateContextFile = (file, context) => {
47
+ writeFileSync(file, JSON.stringify(context, null, 2));
48
+ };
package/env.js ADDED
@@ -0,0 +1,6 @@
1
+ export const isCi = () => {
2
+ return process.env.CI !== 'false' && Boolean(process.env.CI);
3
+ };
4
+ export const isDebug = () => {
5
+ return Boolean(process.env.DEBUG);
6
+ };
package/errors.js ADDED
@@ -0,0 +1,16 @@
1
+ const ERROR_MATCHERS = [
2
+ [/^Unknown command: (.*)$/, 'UNKNOWN_COMMAND'],
3
+ [/^Missing required argument: (.*)$/, 'MISSING_ARGUMENT'],
4
+ ];
5
+ export const matchErrorCode = (message) => {
6
+ if (!message) {
7
+ return 'UNKNOWN_ERROR';
8
+ }
9
+ for (const [matcher, code] of ERROR_MATCHERS) {
10
+ const match = message.match(matcher);
11
+ if (match) {
12
+ return code;
13
+ }
14
+ }
15
+ return 'UNKNOWN_ERROR';
16
+ };