@sanity/runtime-cli 4.3.2 → 4.3.3
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/README.md +17 -17
- package/dist/actions/blueprints/assets.js +2 -2
- package/dist/actions/blueprints/logs.js +2 -2
- package/dist/actions/blueprints/operations.js +2 -2
- package/dist/actions/blueprints/projects.js +1 -1
- package/dist/actions/blueprints/stacks.js +2 -2
- package/dist/actions/functions/env/list.js +2 -2
- package/dist/actions/functions/env/remove.js +2 -2
- package/dist/actions/functions/env/update.js +2 -2
- package/dist/actions/functions/invoke.js +2 -2
- package/dist/actions/functions/logs.js +3 -3
- package/dist/commands/blueprints/config.d.ts +1 -0
- package/dist/commands/blueprints/config.js +15 -5
- package/dist/commands/blueprints/deploy.js +4 -2
- package/dist/commands/blueprints/destroy.js +4 -2
- package/dist/commands/blueprints/info.js +4 -2
- package/dist/commands/blueprints/init.d.ts +1 -0
- package/dist/commands/blueprints/init.js +14 -6
- package/dist/commands/blueprints/logs.js +4 -2
- package/dist/commands/blueprints/plan.js +8 -7
- package/dist/commands/blueprints/stacks.js +4 -2
- package/dist/commands/functions/env/add.js +6 -3
- package/dist/commands/functions/env/list.js +6 -3
- package/dist/commands/functions/env/remove.js +6 -3
- package/dist/commands/functions/invoke.js +6 -3
- package/dist/commands/functions/logs.d.ts +5 -0
- package/dist/commands/functions/logs.js +25 -17
- package/dist/config.d.ts +3 -6
- package/dist/config.js +34 -22
- package/dist/utils/validated-token.d.ts +11 -0
- package/dist/utils/validated-token.js +59 -0
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ $ npm install -g @sanity/runtime-cli
|
|
|
20
20
|
$ sanity-run COMMAND
|
|
21
21
|
running command...
|
|
22
22
|
$ sanity-run (--version)
|
|
23
|
-
@sanity/runtime-cli/4.3.
|
|
23
|
+
@sanity/runtime-cli/4.3.3 linux-x64 node-v22.14.0
|
|
24
24
|
$ sanity-run --help [COMMAND]
|
|
25
25
|
USAGE
|
|
26
26
|
$ sanity-run COMMAND
|
|
@@ -74,7 +74,7 @@ EXAMPLES
|
|
|
74
74
|
$ sanity-run blueprints add function --name my-function --function-type document-publish
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
77
|
+
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/add.ts)_
|
|
78
78
|
|
|
79
79
|
## `sanity-run blueprints config`
|
|
80
80
|
|
|
@@ -103,7 +103,7 @@ EXAMPLES
|
|
|
103
103
|
$ sanity-run blueprints config --edit --project-id <projectId> --stack-id <stackId>
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
106
|
+
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/config.ts)_
|
|
107
107
|
|
|
108
108
|
## `sanity-run blueprints deploy`
|
|
109
109
|
|
|
@@ -123,7 +123,7 @@ EXAMPLES
|
|
|
123
123
|
$ sanity-run blueprints deploy
|
|
124
124
|
```
|
|
125
125
|
|
|
126
|
-
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
126
|
+
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/deploy.ts)_
|
|
127
127
|
|
|
128
128
|
## `sanity-run blueprints destroy`
|
|
129
129
|
|
|
@@ -146,7 +146,7 @@ EXAMPLES
|
|
|
146
146
|
$ sanity-run blueprints destroy --id ST-a1b2c3
|
|
147
147
|
```
|
|
148
148
|
|
|
149
|
-
_See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
149
|
+
_See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/destroy.ts)_
|
|
150
150
|
|
|
151
151
|
## `sanity-run blueprints info`
|
|
152
152
|
|
|
@@ -168,7 +168,7 @@ EXAMPLES
|
|
|
168
168
|
$ sanity-run blueprints info --id ST-a1b2c3
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
-
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
171
|
+
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/info.ts)_
|
|
172
172
|
|
|
173
173
|
## `sanity-run blueprints init`
|
|
174
174
|
|
|
@@ -198,7 +198,7 @@ EXAMPLES
|
|
|
198
198
|
$ sanity-run blueprints init --blueprint-type <json|js|ts> --project-id <projectId> --stack-name <stackName>
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
-
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
201
|
+
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/init.ts)_
|
|
202
202
|
|
|
203
203
|
## `sanity-run blueprints logs`
|
|
204
204
|
|
|
@@ -220,7 +220,7 @@ EXAMPLES
|
|
|
220
220
|
$ sanity-run blueprints logs --watch
|
|
221
221
|
```
|
|
222
222
|
|
|
223
|
-
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
223
|
+
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/logs.ts)_
|
|
224
224
|
|
|
225
225
|
## `sanity-run blueprints plan`
|
|
226
226
|
|
|
@@ -237,7 +237,7 @@ EXAMPLES
|
|
|
237
237
|
$ sanity-run blueprints plan
|
|
238
238
|
```
|
|
239
239
|
|
|
240
|
-
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
240
|
+
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/plan.ts)_
|
|
241
241
|
|
|
242
242
|
## `sanity-run blueprints stacks`
|
|
243
243
|
|
|
@@ -259,7 +259,7 @@ EXAMPLES
|
|
|
259
259
|
$ sanity-run blueprints stacks --projectId a1b2c3
|
|
260
260
|
```
|
|
261
261
|
|
|
262
|
-
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
262
|
+
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/blueprints/stacks.ts)_
|
|
263
263
|
|
|
264
264
|
## `sanity-run functions dev`
|
|
265
265
|
|
|
@@ -279,7 +279,7 @@ EXAMPLES
|
|
|
279
279
|
$ sanity-run functions dev --port 8974
|
|
280
280
|
```
|
|
281
281
|
|
|
282
|
-
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
282
|
+
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/dev.ts)_
|
|
283
283
|
|
|
284
284
|
## `sanity-run functions env add NAME KEY VALUE`
|
|
285
285
|
|
|
@@ -301,7 +301,7 @@ EXAMPLES
|
|
|
301
301
|
$ sanity-run functions env add MyFunction API_URL https://api.example.com/
|
|
302
302
|
```
|
|
303
303
|
|
|
304
|
-
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
304
|
+
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/env/add.ts)_
|
|
305
305
|
|
|
306
306
|
## `sanity-run functions env list NAME`
|
|
307
307
|
|
|
@@ -321,7 +321,7 @@ EXAMPLES
|
|
|
321
321
|
$ sanity-run functions env list MyFunction
|
|
322
322
|
```
|
|
323
323
|
|
|
324
|
-
_See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
324
|
+
_See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/env/list.ts)_
|
|
325
325
|
|
|
326
326
|
## `sanity-run functions env remove NAME KEY`
|
|
327
327
|
|
|
@@ -342,7 +342,7 @@ EXAMPLES
|
|
|
342
342
|
$ sanity-run functions env remove MyFunction API_URL
|
|
343
343
|
```
|
|
344
344
|
|
|
345
|
-
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
345
|
+
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/env/remove.ts)_
|
|
346
346
|
|
|
347
347
|
## `sanity-run functions invoke NAME`
|
|
348
348
|
|
|
@@ -368,7 +368,7 @@ EXAMPLES
|
|
|
368
368
|
$ sanity-run functions invoke <name> --file 'payload.json'
|
|
369
369
|
```
|
|
370
370
|
|
|
371
|
-
_See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
371
|
+
_See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/invoke.ts)_
|
|
372
372
|
|
|
373
373
|
## `sanity-run functions logs NAME`
|
|
374
374
|
|
|
@@ -400,7 +400,7 @@ EXAMPLES
|
|
|
400
400
|
$ sanity-run functions logs <name> --delete
|
|
401
401
|
```
|
|
402
402
|
|
|
403
|
-
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
403
|
+
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/logs.ts)_
|
|
404
404
|
|
|
405
405
|
## `sanity-run functions test NAME`
|
|
406
406
|
|
|
@@ -433,7 +433,7 @@ EXAMPLES
|
|
|
433
433
|
$ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
|
|
434
434
|
```
|
|
435
435
|
|
|
436
|
-
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.
|
|
436
|
+
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v4.3.3/src/commands/functions/test.ts)_
|
|
437
437
|
|
|
438
438
|
## `sanity-run help [COMMAND]`
|
|
439
439
|
|
|
@@ -4,8 +4,8 @@ import { cwd } from 'node:process';
|
|
|
4
4
|
import AdmZip from 'adm-zip';
|
|
5
5
|
import config from '../../config.js';
|
|
6
6
|
import getHeaders from '../../utils/get-headers.js';
|
|
7
|
-
const {
|
|
8
|
-
export const stashUrl = `${
|
|
7
|
+
const { apiUrl } = config;
|
|
8
|
+
export const stashUrl = `${apiUrl}vX/blueprints/assets/stash`;
|
|
9
9
|
export async function stashAsset({ resource, auth, }) {
|
|
10
10
|
if (!resource.src)
|
|
11
11
|
throw new Error('Resource src is required');
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { EventSource } from 'eventsource';
|
|
2
2
|
import config from '../../config.js';
|
|
3
3
|
import getHeaders from '../../utils/get-headers.js';
|
|
4
|
-
const {
|
|
5
|
-
export const logsUrl = `${
|
|
4
|
+
const { apiUrl } = config;
|
|
5
|
+
export const logsUrl = `${apiUrl}vX/blueprints/logs`;
|
|
6
6
|
export async function getLogs(stackId, auth) {
|
|
7
7
|
const url = new URL(logsUrl);
|
|
8
8
|
url.searchParams.append('stackId', stackId);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import config from '../../config.js';
|
|
2
2
|
import getHeaders from '../../utils/get-headers.js';
|
|
3
|
-
const {
|
|
4
|
-
export const stacksUrl = `${
|
|
3
|
+
const { apiUrl } = config;
|
|
4
|
+
export const stacksUrl = `${apiUrl}vX/blueprints/stacks`;
|
|
5
5
|
export async function getOperation({ stackId, operationId, auth, }) {
|
|
6
6
|
const path = `${stacksUrl}/${stackId}/operations/${operationId}`;
|
|
7
7
|
const response = await fetch(path, {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import config from '../../config.js';
|
|
2
2
|
const { apiUrl } = config;
|
|
3
|
-
const projectsUrl = `${apiUrl
|
|
3
|
+
const projectsUrl = `${apiUrl}v2021-06-07/projects`;
|
|
4
4
|
export async function listProjects({ token }) {
|
|
5
5
|
const response = await fetch(projectsUrl, {
|
|
6
6
|
method: 'GET',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import config from '../../config.js';
|
|
2
2
|
import getHeaders from '../../utils/get-headers.js';
|
|
3
|
-
const {
|
|
4
|
-
export const stacksUrl = `${
|
|
3
|
+
const { apiUrl } = config;
|
|
4
|
+
export const stacksUrl = `${apiUrl}vX/blueprints/stacks`;
|
|
5
5
|
export async function listStacks(auth) {
|
|
6
6
|
const response = await fetch(stacksUrl, {
|
|
7
7
|
method: 'GET',
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import config from '../../../config.js';
|
|
2
2
|
import getHeaders from '../../../utils/get-headers.js';
|
|
3
|
-
const {
|
|
3
|
+
const { apiUrl } = config;
|
|
4
4
|
export async function list(id, auth) {
|
|
5
|
-
const response = await fetch(`${
|
|
5
|
+
const response = await fetch(`${apiUrl}vX/functions/${id}/envvars`, {
|
|
6
6
|
headers: getHeaders(auth),
|
|
7
7
|
method: 'GET',
|
|
8
8
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import config from '../../../config.js';
|
|
2
2
|
import getHeaders from '../../../utils/get-headers.js';
|
|
3
|
-
const {
|
|
3
|
+
const { apiUrl } = config;
|
|
4
4
|
export async function remove(id, key, auth) {
|
|
5
|
-
const response = await fetch(`${
|
|
5
|
+
const response = await fetch(`${apiUrl}vX/functions/${id}/envvars/${key}`, {
|
|
6
6
|
headers: getHeaders(auth),
|
|
7
7
|
method: 'DELETE',
|
|
8
8
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import config from '../../../config.js';
|
|
2
2
|
import getHeaders from '../../../utils/get-headers.js';
|
|
3
|
-
const {
|
|
3
|
+
const { apiUrl } = config;
|
|
4
4
|
export async function update(id, key, value, auth) {
|
|
5
|
-
const response = await fetch(`${
|
|
5
|
+
const response = await fetch(`${apiUrl}vX/functions/${id}/envvars/${key}`, {
|
|
6
6
|
body: JSON.stringify({ value }),
|
|
7
7
|
headers: getHeaders(auth),
|
|
8
8
|
method: 'PUT',
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import config from '../../config.js';
|
|
2
2
|
import buildPayload from '../../utils/build-payload.js';
|
|
3
3
|
import getHeaders from '../../utils/get-headers.js';
|
|
4
|
-
const {
|
|
4
|
+
const { apiUrl } = config;
|
|
5
5
|
export async function invoke(id, options, auth) {
|
|
6
6
|
const payload = buildPayload(options);
|
|
7
|
-
const response = await fetch(`${
|
|
7
|
+
const response = await fetch(`${apiUrl}vX/functions/${id}/invoke`, {
|
|
8
8
|
body: JSON.stringify({ data: payload }),
|
|
9
9
|
headers: getHeaders(auth),
|
|
10
10
|
method: 'POST',
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import config from '../../config.js';
|
|
2
2
|
import getHeaders from '../../utils/get-headers.js';
|
|
3
|
-
const {
|
|
3
|
+
const { apiUrl } = config;
|
|
4
4
|
export async function logs(id, options, auth) {
|
|
5
5
|
const { limit } = options;
|
|
6
|
-
const response = await fetch(`${
|
|
6
|
+
const response = await fetch(`${apiUrl}vX/functions/${id}/logs?limit=${limit}`, {
|
|
7
7
|
headers: getHeaders(auth),
|
|
8
8
|
method: 'GET',
|
|
9
9
|
});
|
|
@@ -16,7 +16,7 @@ export async function logs(id, options, auth) {
|
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
export async function deleteLogs(id, auth) {
|
|
19
|
-
const response = await fetch(`${
|
|
19
|
+
const response = await fetch(`${apiUrl}vX/functions/${id}/logs`, {
|
|
20
20
|
headers: getHeaders(auth),
|
|
21
21
|
method: 'DELETE',
|
|
22
22
|
});
|
|
@@ -5,10 +5,10 @@ import Spinner from 'yocto-spinner';
|
|
|
5
5
|
import { readBlueprintOnDisk, writeConfigFile } from '../../actions/blueprints/blueprint.js';
|
|
6
6
|
import { listProjects } from '../../actions/blueprints/projects.js';
|
|
7
7
|
import { getStack, listStacks } from '../../actions/blueprints/stacks.js';
|
|
8
|
-
import config from '../../config.js';
|
|
9
8
|
import { bold, dim, niceId } from '../../utils/display/colors.js';
|
|
10
|
-
|
|
9
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
11
10
|
export default class Config extends Command {
|
|
11
|
+
token;
|
|
12
12
|
static description = 'View or edit Blueprint configuration';
|
|
13
13
|
static examples = [
|
|
14
14
|
'<%= config.bin %> <%= command.id %>',
|
|
@@ -56,6 +56,10 @@ export default class Config extends Command {
|
|
|
56
56
|
}
|
|
57
57
|
if (!editConfig && !testConfig)
|
|
58
58
|
return;
|
|
59
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
60
|
+
if (tokenErr)
|
|
61
|
+
this.error(tokenErr.message);
|
|
62
|
+
this.token = token;
|
|
59
63
|
if (testConfig && !editConfig) {
|
|
60
64
|
if (configStackId && configProjectId) {
|
|
61
65
|
await this.testConfigAndReport({ stackId: configStackId, projectId: configProjectId });
|
|
@@ -92,7 +96,9 @@ export default class Config extends Command {
|
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
async promptForProjectId({ knownProjectId }) {
|
|
95
|
-
|
|
99
|
+
if (!this.token)
|
|
100
|
+
this.error('Unable to list projects. Missing API token.');
|
|
101
|
+
const { ok, projects, error } = await listProjects({ token: this.token });
|
|
96
102
|
if (!ok)
|
|
97
103
|
this.error(error ?? 'Unknown error listing projects');
|
|
98
104
|
if (projects.length === 0) {
|
|
@@ -114,7 +120,9 @@ export default class Config extends Command {
|
|
|
114
120
|
return pickedProjectId;
|
|
115
121
|
}
|
|
116
122
|
async promptForStackId({ projectId, knownStackId, }) {
|
|
117
|
-
|
|
123
|
+
if (!this.token)
|
|
124
|
+
this.error('Unable to list Stacks. Missing API token.');
|
|
125
|
+
const auth = { token: this.token, projectId };
|
|
118
126
|
// get stacks for selected project
|
|
119
127
|
const { ok: stacksOk, stacks, error: stacksError } = await listStacks(auth);
|
|
120
128
|
if (!stacksOk)
|
|
@@ -139,10 +147,12 @@ export default class Config extends Command {
|
|
|
139
147
|
return undefined;
|
|
140
148
|
}
|
|
141
149
|
async testConfigAndReport({ stackId, projectId }) {
|
|
150
|
+
if (!this.token)
|
|
151
|
+
this.error('Unable to test the configuration. Missing API token.');
|
|
142
152
|
const spinner = Spinner({ text: 'Testing the configuration...' }).start();
|
|
143
153
|
const { ok, error } = await getStack({
|
|
144
154
|
stackId,
|
|
145
|
-
auth: { token, projectId },
|
|
155
|
+
auth: { token: this.token, projectId },
|
|
146
156
|
});
|
|
147
157
|
if (!ok) {
|
|
148
158
|
spinner.error(error ?? 'Unknown error testing the configuration');
|
|
@@ -4,10 +4,9 @@ import Spinner from 'yocto-spinner';
|
|
|
4
4
|
import { stashAsset } from '../../actions/blueprints/assets.js';
|
|
5
5
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
6
6
|
import { getStack, updateStack } from '../../actions/blueprints/stacks.js';
|
|
7
|
-
import config from '../../config.js';
|
|
8
7
|
import { bold, niceId, red } from '../../utils/display/colors.js';
|
|
9
8
|
import { isLocalFunctionResource } from '../../utils/types.js';
|
|
10
|
-
|
|
9
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
11
10
|
export default class Deploy extends Command {
|
|
12
11
|
static description = 'Deploy a Blueprint';
|
|
13
12
|
static examples = ['<%= config.bin %> <%= command.id %>'];
|
|
@@ -18,6 +17,9 @@ export default class Deploy extends Command {
|
|
|
18
17
|
}),
|
|
19
18
|
};
|
|
20
19
|
async run() {
|
|
20
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
21
|
+
if (tokenErr)
|
|
22
|
+
this.error(tokenErr.message);
|
|
21
23
|
const { flags } = await this.parse(Deploy);
|
|
22
24
|
const { errors, projectId, stackId, parsedBlueprint: { resources }, deployedStack, } = await readBlueprintOnDisk({ getStack: true, token });
|
|
23
25
|
if (errors.length > 0) {
|
|
@@ -4,9 +4,8 @@ import inquirer from 'inquirer';
|
|
|
4
4
|
import Spinner from 'yocto-spinner';
|
|
5
5
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
6
6
|
import { destroyStack, getStack } from '../../actions/blueprints/stacks.js';
|
|
7
|
-
import config from '../../config.js';
|
|
8
7
|
import { bold, niceId } from '../../utils/display/colors.js';
|
|
9
|
-
|
|
8
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
10
9
|
export default class Destroy extends Command {
|
|
11
10
|
static description = 'Destroy a deployed Blueprint Stack';
|
|
12
11
|
static examples = [
|
|
@@ -24,6 +23,9 @@ export default class Destroy extends Command {
|
|
|
24
23
|
}),
|
|
25
24
|
};
|
|
26
25
|
async run() {
|
|
26
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
27
|
+
if (tokenErr)
|
|
28
|
+
this.error(tokenErr.message);
|
|
27
29
|
const { flags } = await this.parse(Destroy);
|
|
28
30
|
const { errors, deployedStack, projectId } = await readBlueprintOnDisk({ getStack: true, token });
|
|
29
31
|
if (errors.length > 0) {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Command, Flags } from '@oclif/core';
|
|
2
2
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
3
3
|
import { getStack } from '../../actions/blueprints/stacks.js';
|
|
4
|
-
import config from '../../config.js';
|
|
5
4
|
import { formatResourceTree, formatStackInfo } from '../../utils/display/blueprints-formatting.js';
|
|
6
5
|
import { niceId } from '../../utils/display/colors.js';
|
|
7
|
-
|
|
6
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
8
7
|
export default class Info extends Command {
|
|
9
8
|
static description = 'Show information about a deployed Blueprint Stack';
|
|
10
9
|
static examples = [
|
|
@@ -18,6 +17,9 @@ export default class Info extends Command {
|
|
|
18
17
|
}),
|
|
19
18
|
};
|
|
20
19
|
async run() {
|
|
20
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
21
|
+
if (tokenErr)
|
|
22
|
+
this.error(tokenErr.message);
|
|
21
23
|
const { flags } = await this.parse(Info);
|
|
22
24
|
const { errors, deployedStack, projectId, stackId } = await readBlueprintOnDisk({
|
|
23
25
|
getStack: true,
|
|
@@ -9,6 +9,7 @@ export default class Init extends Command {
|
|
|
9
9
|
'stack-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
10
|
'stack-name': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
11
|
};
|
|
12
|
+
token: string | undefined;
|
|
12
13
|
run(): Promise<void>;
|
|
13
14
|
promptForBlueprintType(): Promise<string>;
|
|
14
15
|
promptForProjectId(): Promise<string>;
|
|
@@ -5,9 +5,8 @@ import inquirer from 'inquirer';
|
|
|
5
5
|
import { findBlueprintFile, writeBlueprintToDisk, writeConfigFile, } from '../../actions/blueprints/blueprint.js';
|
|
6
6
|
import { listProjects } from '../../actions/blueprints/projects.js';
|
|
7
7
|
import { createStack, listStacks } from '../../actions/blueprints/stacks.js';
|
|
8
|
-
import config from '../../config.js';
|
|
9
8
|
import { bold, dim, niceId, underline } from '../../utils/display/colors.js';
|
|
10
|
-
|
|
9
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
11
10
|
export default class Init extends Command {
|
|
12
11
|
static description = 'Initialize a new Blueprint';
|
|
13
12
|
static examples = [
|
|
@@ -40,6 +39,7 @@ export default class Init extends Command {
|
|
|
40
39
|
exclusive: ['stack-id'],
|
|
41
40
|
}),
|
|
42
41
|
};
|
|
42
|
+
token;
|
|
43
43
|
async run() {
|
|
44
44
|
const { flags } = await this.parse(Init);
|
|
45
45
|
const { 'blueprint-type': flagBlueprintType, 'project-id': flagProjectId, 'stack-id': flagStackId, 'stack-name': flagStackName, } = flags;
|
|
@@ -47,6 +47,10 @@ export default class Init extends Command {
|
|
|
47
47
|
if (existingBlueprint) {
|
|
48
48
|
this.error(`A blueprint file already exists: ${existingBlueprint.fileName}`);
|
|
49
49
|
}
|
|
50
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
51
|
+
if (tokenErr)
|
|
52
|
+
this.error(tokenErr.message);
|
|
53
|
+
this.token = token;
|
|
50
54
|
const blueprintExtension = flagBlueprintType || (await this.promptForBlueprintType());
|
|
51
55
|
if (!blueprintExtension)
|
|
52
56
|
this.error('Blueprint type is required.');
|
|
@@ -58,7 +62,7 @@ export default class Init extends Command {
|
|
|
58
62
|
if (flagStackName) {
|
|
59
63
|
const stack = await this.createEmptyStack({
|
|
60
64
|
stackPayload: { name: flagStackName, projectId, document: { resources: [] } },
|
|
61
|
-
auth: { token, projectId },
|
|
65
|
+
auth: { token: this.token, projectId },
|
|
62
66
|
});
|
|
63
67
|
stackId = stack.id;
|
|
64
68
|
}
|
|
@@ -93,7 +97,9 @@ export default class Init extends Command {
|
|
|
93
97
|
return pickedBlueprintsType;
|
|
94
98
|
}
|
|
95
99
|
async promptForProjectId() {
|
|
96
|
-
|
|
100
|
+
if (!this.token)
|
|
101
|
+
this.error('Unable to list projects. Missing API token.');
|
|
102
|
+
const { ok: projectsOk, error: projectsErr, projects } = await listProjects({ token: this.token });
|
|
97
103
|
if (!projectsOk)
|
|
98
104
|
this.error(projectsErr ?? 'Unknown error listing projects');
|
|
99
105
|
if (projects.length === 0) {
|
|
@@ -114,7 +120,9 @@ export default class Init extends Command {
|
|
|
114
120
|
return pickedProjectId;
|
|
115
121
|
}
|
|
116
122
|
async promptForStackId({ projectId }) {
|
|
117
|
-
|
|
123
|
+
if (!this.token)
|
|
124
|
+
this.error('Unable to list Stacks. Missing API token.');
|
|
125
|
+
const { ok: stacksOk, error: stacksErr, stacks, } = await listStacks({ token: this.token, projectId });
|
|
118
126
|
if (!stacksOk)
|
|
119
127
|
this.error(stacksErr || 'Failed to list Stacks');
|
|
120
128
|
const stackChoices = [{ name: bold('✨ Create a new Stack'), value: 'new' }];
|
|
@@ -144,7 +152,7 @@ export default class Init extends Command {
|
|
|
144
152
|
]);
|
|
145
153
|
const stack = await this.createEmptyStack({
|
|
146
154
|
stackPayload: { name: stackName, projectId, document: { resources: [] } },
|
|
147
|
-
auth: { token, projectId },
|
|
155
|
+
auth: { token: this.token, projectId },
|
|
148
156
|
});
|
|
149
157
|
return stack.id;
|
|
150
158
|
}
|
|
@@ -2,11 +2,10 @@ import { Command, Flags } from '@oclif/core';
|
|
|
2
2
|
import Spinner from 'yocto-spinner';
|
|
3
3
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
4
4
|
import { findNewestLogTimestamp, getLogs, getRecentLogs, isNewerLog, streamLogs, } from '../../actions/blueprints/logs.js';
|
|
5
|
-
import config from '../../config.js';
|
|
6
5
|
import { formatTitle } from '../../utils/display/blueprints-formatting.js';
|
|
7
6
|
import { bold, niceId, red } from '../../utils/display/colors.js';
|
|
8
7
|
import { formatLogEntry, formatLogsByDay, organizeLogsByDay, } from '../../utils/display/logs-formatting.js';
|
|
9
|
-
|
|
8
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
10
9
|
export default class Logs extends Command {
|
|
11
10
|
static description = 'Display logs for a Blueprint stack';
|
|
12
11
|
static examples = [
|
|
@@ -22,6 +21,9 @@ export default class Logs extends Command {
|
|
|
22
21
|
}),
|
|
23
22
|
};
|
|
24
23
|
async run() {
|
|
24
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
25
|
+
if (tokenErr)
|
|
26
|
+
this.error(tokenErr.message);
|
|
25
27
|
const { flags } = await this.parse(Logs);
|
|
26
28
|
const watchMode = flags.watch;
|
|
27
29
|
try {
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { Command } from '@oclif/core';
|
|
2
2
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
3
|
-
import config from '../../config.js';
|
|
4
3
|
import { formatResourceTree, formatTitle } from '../../utils/display/blueprints-formatting.js';
|
|
5
|
-
|
|
4
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
6
5
|
export default class Plan extends Command {
|
|
7
6
|
static description = 'Enumerate resources to be deployed - will not modify any resources';
|
|
8
7
|
static examples = ['<%= config.bin %> <%= command.id %>'];
|
|
9
8
|
async run() {
|
|
10
|
-
const {
|
|
9
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
10
|
+
if (tokenErr)
|
|
11
|
+
this.error(tokenErr.message);
|
|
12
|
+
const { errors, projectId, stackId, parsedBlueprint: { resources }, fileInfo, deployedStack, } = await readBlueprintOnDisk({ getStack: true, token });
|
|
11
13
|
if (errors.length > 0) {
|
|
12
14
|
// printErrors(errors)
|
|
13
15
|
this.log('Blueprint parse errors:');
|
|
@@ -15,10 +17,9 @@ export default class Plan extends Command {
|
|
|
15
17
|
}
|
|
16
18
|
if (!projectId)
|
|
17
19
|
this.log('Blueprint must contain a project resource');
|
|
18
|
-
const name = stackId
|
|
19
|
-
this.log(`${formatTitle('
|
|
20
|
-
this.log(`
|
|
21
|
-
this.log('');
|
|
20
|
+
const name = deployedStack?.name ?? stackId ?? 'Unknown';
|
|
21
|
+
this.log(`${formatTitle('Stack', name)} Plan`);
|
|
22
|
+
this.log(`(${fileInfo.fileName})\n`);
|
|
22
23
|
this.log(formatResourceTree(resources));
|
|
23
24
|
this.log('\nRun `sanity blueprints deploy` to deploy these changes');
|
|
24
25
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Command, Flags } from '@oclif/core';
|
|
2
2
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
3
3
|
import { listStacks } from '../../actions/blueprints/stacks.js';
|
|
4
|
-
import config from '../../config.js';
|
|
5
4
|
import { formatStacksListing } from '../../utils/display/blueprints-formatting.js';
|
|
6
5
|
import { bold, niceId } from '../../utils/display/colors.js';
|
|
7
|
-
|
|
6
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
8
7
|
export default class Stacks extends Command {
|
|
9
8
|
static description = 'List all Blueprint stacks';
|
|
10
9
|
static examples = [
|
|
@@ -18,6 +17,9 @@ export default class Stacks extends Command {
|
|
|
18
17
|
}),
|
|
19
18
|
};
|
|
20
19
|
async run() {
|
|
20
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
21
|
+
if (tokenErr)
|
|
22
|
+
this.error(tokenErr.message);
|
|
21
23
|
const { flags } = await this.parse(Stacks);
|
|
22
24
|
let { projectId } = flags;
|
|
23
25
|
let stackId;
|
|
@@ -2,9 +2,9 @@ import { Args, Command } from '@oclif/core';
|
|
|
2
2
|
import Spinner from 'yocto-spinner';
|
|
3
3
|
import { readBlueprintOnDisk } from '../../../actions/blueprints/blueprint.js';
|
|
4
4
|
import { update } from '../../../actions/functions/env/update.js';
|
|
5
|
-
import config from '../../../config.js';
|
|
6
5
|
import { red } from '../../../utils/display/colors.js';
|
|
7
6
|
import { findFunctionByName } from '../../../utils/find-function.js';
|
|
7
|
+
import { validTokenOrErrorMessage } from '../../../utils/validated-token.js';
|
|
8
8
|
export default class Add extends Command {
|
|
9
9
|
static args = {
|
|
10
10
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
@@ -16,17 +16,20 @@ export default class Add extends Command {
|
|
|
16
16
|
'<%= config.bin %> <%= command.id %> MyFunction API_URL https://api.example.com/',
|
|
17
17
|
];
|
|
18
18
|
async run() {
|
|
19
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
20
|
+
if (tokenErr)
|
|
21
|
+
this.error(tokenErr.message);
|
|
19
22
|
const { args } = await this.parse(Add);
|
|
20
23
|
const spinner = Spinner({
|
|
21
24
|
text: `Updating "${args.key}" environment variable in "${args.name}"`,
|
|
22
25
|
}).start();
|
|
23
|
-
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token
|
|
26
|
+
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token });
|
|
24
27
|
if (!deployedStack)
|
|
25
28
|
this.error('Stack not found'); // returns
|
|
26
29
|
const { projectId } = deployedStack;
|
|
27
30
|
const { externalId } = findFunctionByName(deployedStack, args.name);
|
|
28
31
|
const result = await update(externalId, args.key, args.value, {
|
|
29
|
-
token
|
|
32
|
+
token,
|
|
30
33
|
projectId,
|
|
31
34
|
});
|
|
32
35
|
if (result.ok) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Args, Command } from '@oclif/core';
|
|
2
2
|
import { readBlueprintOnDisk } from '../../../actions/blueprints/blueprint.js';
|
|
3
3
|
import { list } from '../../../actions/functions/env/list.js';
|
|
4
|
-
import config from '../../../config.js';
|
|
5
4
|
import { findFunctionByName } from '../../../utils/find-function.js';
|
|
5
|
+
import { validTokenOrErrorMessage } from '../../../utils/validated-token.js';
|
|
6
6
|
export default class List extends Command {
|
|
7
7
|
static args = {
|
|
8
8
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
@@ -10,14 +10,17 @@ export default class List extends Command {
|
|
|
10
10
|
static description = 'List the environment variables for a Sanity function';
|
|
11
11
|
static examples = ['<%= config.bin %> <%= command.id %> MyFunction'];
|
|
12
12
|
async run() {
|
|
13
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
14
|
+
if (tokenErr)
|
|
15
|
+
this.error(tokenErr.message);
|
|
13
16
|
const { args } = await this.parse(List);
|
|
14
|
-
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token
|
|
17
|
+
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token });
|
|
15
18
|
if (!deployedStack)
|
|
16
19
|
this.error('Stack not found. Is it deployed?'); // returns
|
|
17
20
|
const { projectId } = deployedStack;
|
|
18
21
|
const { externalId } = findFunctionByName(deployedStack, args.name);
|
|
19
22
|
const result = await list(externalId, {
|
|
20
|
-
token
|
|
23
|
+
token,
|
|
21
24
|
projectId,
|
|
22
25
|
});
|
|
23
26
|
if (!result.ok) {
|
|
@@ -2,9 +2,9 @@ import { Args, Command } from '@oclif/core';
|
|
|
2
2
|
import Spinner from 'yocto-spinner';
|
|
3
3
|
import { readBlueprintOnDisk } from '../../../actions/blueprints/blueprint.js';
|
|
4
4
|
import { remove } from '../../../actions/functions/env/remove.js';
|
|
5
|
-
import config from '../../../config.js';
|
|
6
5
|
import { red } from '../../../utils/display/colors.js';
|
|
7
6
|
import { findFunctionByName } from '../../../utils/find-function.js';
|
|
7
|
+
import { validTokenOrErrorMessage } from '../../../utils/validated-token.js';
|
|
8
8
|
export default class Remove extends Command {
|
|
9
9
|
static args = {
|
|
10
10
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
@@ -13,17 +13,20 @@ export default class Remove extends Command {
|
|
|
13
13
|
static description = 'Remove an environment variable for a Sanity function';
|
|
14
14
|
static examples = ['<%= config.bin %> <%= command.id %> MyFunction API_URL'];
|
|
15
15
|
async run() {
|
|
16
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
17
|
+
if (tokenErr)
|
|
18
|
+
this.error(tokenErr.message);
|
|
16
19
|
const { args } = await this.parse(Remove);
|
|
17
20
|
const spinner = Spinner({
|
|
18
21
|
text: `Removing "${args.key}" environment variable in "${args.name}"`,
|
|
19
22
|
}).start();
|
|
20
|
-
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token
|
|
23
|
+
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token });
|
|
21
24
|
if (!deployedStack)
|
|
22
25
|
this.error('Stack not found'); // returns
|
|
23
26
|
const { projectId } = deployedStack;
|
|
24
27
|
const { externalId } = findFunctionByName(deployedStack, args.name);
|
|
25
28
|
const result = await remove(externalId, args.key, {
|
|
26
|
-
token
|
|
29
|
+
token,
|
|
27
30
|
projectId,
|
|
28
31
|
});
|
|
29
32
|
if (result.ok) {
|
|
@@ -2,9 +2,9 @@ import { Args, Command, Flags } from '@oclif/core';
|
|
|
2
2
|
import Spinner from 'yocto-spinner';
|
|
3
3
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
4
4
|
import { invoke } from '../../actions/functions/invoke.js';
|
|
5
|
-
import config from '../../config.js';
|
|
6
5
|
import { red } from '../../utils/display/colors.js';
|
|
7
6
|
import { findFunctionByName } from '../../utils/find-function.js';
|
|
7
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
8
8
|
export default class Invoke extends Command {
|
|
9
9
|
static args = {
|
|
10
10
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
@@ -23,14 +23,17 @@ export default class Invoke extends Command {
|
|
|
23
23
|
}),
|
|
24
24
|
};
|
|
25
25
|
async run() {
|
|
26
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
27
|
+
if (tokenErr)
|
|
28
|
+
this.error(tokenErr.message);
|
|
26
29
|
const { args, flags } = await this.parse(Invoke);
|
|
27
30
|
const spinner = Spinner({ text: `Invoking function "${args.name}"` }).start();
|
|
28
|
-
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token
|
|
31
|
+
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token });
|
|
29
32
|
if (!deployedStack)
|
|
30
33
|
this.error('Stack not found'); // returns
|
|
31
34
|
const { projectId } = deployedStack;
|
|
32
35
|
const { externalId } = findFunctionByName(deployedStack, args.name);
|
|
33
|
-
const result = await invoke(externalId, { data: flags.data, file: flags.file }, { token
|
|
36
|
+
const result = await invoke(externalId, { data: flags.data, file: flags.file }, { token, projectId });
|
|
34
37
|
if (result.ok) {
|
|
35
38
|
spinner.success(`Invocation of ${args.name} succeeded`);
|
|
36
39
|
if (result.json?.data?.type === 'Buffer') {
|
|
@@ -18,8 +18,13 @@ export default class Logs extends Command {
|
|
|
18
18
|
delete: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
19
19
|
force: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
20
20
|
};
|
|
21
|
+
token: string | undefined;
|
|
21
22
|
run(): Promise<void>;
|
|
22
23
|
runDeleteLogs(name: string, options: RunDeleteOptions): Promise<void>;
|
|
23
24
|
runGetLogs(name: string, options: RunGetOptions): Promise<void>;
|
|
25
|
+
getProjectAndExternalId(name: string): Promise<{
|
|
26
|
+
projectId?: string;
|
|
27
|
+
externalId?: string;
|
|
28
|
+
}>;
|
|
24
29
|
}
|
|
25
30
|
export {};
|
|
@@ -3,10 +3,10 @@ import inquirer from 'inquirer';
|
|
|
3
3
|
import Spinner from 'yocto-spinner';
|
|
4
4
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
5
5
|
import { deleteLogs as deleteLogsAction, logs as logsAction } from '../../actions/functions/logs.js';
|
|
6
|
-
import config from '../../config.js';
|
|
7
6
|
import { formatTitle } from '../../utils/display/blueprints-formatting.js';
|
|
8
7
|
import { blue, bold, green, red, yellow } from '../../utils/display/colors.js';
|
|
9
8
|
import { findFunctionByName } from '../../utils/find-function.js';
|
|
9
|
+
import { validTokenOrErrorMessage } from '../../utils/validated-token.js';
|
|
10
10
|
function logLevel(level) {
|
|
11
11
|
if (level === 'ERROR') {
|
|
12
12
|
return red(level);
|
|
@@ -16,16 +16,6 @@ function logLevel(level) {
|
|
|
16
16
|
}
|
|
17
17
|
return green(level);
|
|
18
18
|
}
|
|
19
|
-
// TODO extract to a shared module
|
|
20
|
-
async function getProjectAndExternalId(name) {
|
|
21
|
-
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token: config.token });
|
|
22
|
-
if (!deployedStack) {
|
|
23
|
-
return {};
|
|
24
|
-
}
|
|
25
|
-
const { projectId } = deployedStack;
|
|
26
|
-
const { externalId } = findFunctionByName(deployedStack, name);
|
|
27
|
-
return { projectId, externalId };
|
|
28
|
-
}
|
|
29
19
|
export default class Logs extends Command {
|
|
30
20
|
static args = {
|
|
31
21
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
@@ -62,7 +52,12 @@ export default class Logs extends Command {
|
|
|
62
52
|
required: false,
|
|
63
53
|
}),
|
|
64
54
|
};
|
|
55
|
+
token;
|
|
65
56
|
async run() {
|
|
57
|
+
const { token, error: tokenErr } = await validTokenOrErrorMessage();
|
|
58
|
+
if (tokenErr)
|
|
59
|
+
this.error(tokenErr.message);
|
|
60
|
+
this.token = token;
|
|
66
61
|
const { args, flags } = await this.parse(Logs);
|
|
67
62
|
if (flags.delete === true) {
|
|
68
63
|
await this.runDeleteLogs(args.name, flags);
|
|
@@ -82,13 +77,14 @@ export default class Logs extends Command {
|
|
|
82
77
|
if (!certain)
|
|
83
78
|
return;
|
|
84
79
|
}
|
|
85
|
-
const { projectId, externalId } = await getProjectAndExternalId(name);
|
|
80
|
+
const { projectId, externalId } = await this.getProjectAndExternalId(name);
|
|
86
81
|
if (!projectId || !externalId) {
|
|
87
82
|
this.error('Stack not found');
|
|
88
|
-
return;
|
|
89
83
|
}
|
|
84
|
+
if (!this.token)
|
|
85
|
+
this.error('Unable to delete logs. Missing API token.');
|
|
90
86
|
const spinner = Spinner({ text: `Deleting logs for function ${yellow(name)}` }).start();
|
|
91
|
-
const { ok, error } = await deleteLogsAction(externalId, { token:
|
|
87
|
+
const { ok, error } = await deleteLogsAction(externalId, { token: this.token, projectId });
|
|
92
88
|
if (!ok) {
|
|
93
89
|
spinner.error(`${red('Failed')} to retrieve logs`);
|
|
94
90
|
this.log(`Error: ${error || 'Unknown error'}`);
|
|
@@ -98,12 +94,13 @@ export default class Logs extends Command {
|
|
|
98
94
|
}
|
|
99
95
|
async runGetLogs(name, options) {
|
|
100
96
|
const spinner = Spinner({ text: `Finding logs for function "${name}"` }).start();
|
|
101
|
-
const { projectId, externalId } = await getProjectAndExternalId(name);
|
|
97
|
+
const { projectId, externalId } = await this.getProjectAndExternalId(name);
|
|
102
98
|
if (!projectId || !externalId) {
|
|
103
99
|
this.error('Stack not found');
|
|
104
|
-
return;
|
|
105
100
|
}
|
|
106
|
-
|
|
101
|
+
if (!this.token)
|
|
102
|
+
this.error('Unable to retrieve logs. Missing API token.');
|
|
103
|
+
const { ok, error, logs, total } = await logsAction(externalId, { limit: options.limit }, { token: this.token, projectId });
|
|
107
104
|
if (!ok) {
|
|
108
105
|
spinner.error(`${red('Failed')} to retrieve logs`);
|
|
109
106
|
this.log(`Error: ${error || 'Unknown error'}`);
|
|
@@ -131,4 +128,15 @@ export default class Logs extends Command {
|
|
|
131
128
|
this.log(JSON.stringify(filteredLogs, null, 2));
|
|
132
129
|
}
|
|
133
130
|
}
|
|
131
|
+
async getProjectAndExternalId(name) {
|
|
132
|
+
if (!this.token)
|
|
133
|
+
this.error('Missing API token.');
|
|
134
|
+
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token: this.token });
|
|
135
|
+
if (!deployedStack) {
|
|
136
|
+
return {};
|
|
137
|
+
}
|
|
138
|
+
const { projectId } = deployedStack;
|
|
139
|
+
const { externalId } = findFunctionByName(deployedStack, name);
|
|
140
|
+
return { projectId, externalId };
|
|
141
|
+
}
|
|
134
142
|
}
|
package/dist/config.d.ts
CHANGED
package/dist/config.js
CHANGED
|
@@ -1,33 +1,45 @@
|
|
|
1
1
|
import { env } from 'node:process';
|
|
2
2
|
import getToken from './utils/get-token.js';
|
|
3
|
-
const nodeEnv = env.
|
|
3
|
+
const nodeEnv = env.NODE_ENV?.toLowerCase() ?? 'production';
|
|
4
4
|
const isTest = nodeEnv === 'test';
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const isPublishing = env?.PUBLISHING === 'true';
|
|
5
|
+
const sanityEnv = env.SANITY_INTERNAL_ENV?.toLowerCase() ?? 'production';
|
|
6
|
+
const sanityProd = sanityEnv === 'production';
|
|
7
|
+
const isPublishing = env?.PUBLISHING?.toLowerCase() === 'true';
|
|
8
8
|
const apiUrls = {
|
|
9
9
|
production: 'https://api.sanity.io/',
|
|
10
10
|
staging: 'https://api.sanity.work/',
|
|
11
|
+
default: 'http://api.sanity/',
|
|
11
12
|
};
|
|
12
|
-
const apiUrl =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
const apiUrl = apiUrls[sanityEnv] ?? apiUrls.default;
|
|
14
|
+
let _token = undefined;
|
|
15
|
+
function loadToken() {
|
|
16
|
+
let token = null;
|
|
17
|
+
if (isTest || isPublishing) {
|
|
18
|
+
token = 'token';
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
try {
|
|
22
|
+
token = getToken({ prod: sanityProd });
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
|
|
26
|
+
// do nothing, validateToken and commands handle missing config file
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// rethrow other errors like JSON.parse failure
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return token;
|
|
35
|
+
}
|
|
26
36
|
export default {
|
|
27
|
-
|
|
37
|
+
isTest,
|
|
28
38
|
apiUrl,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
get token() {
|
|
40
|
+
return _token === undefined ? loadToken() : _token;
|
|
41
|
+
},
|
|
42
|
+
set token(value) {
|
|
43
|
+
_token = value;
|
|
32
44
|
},
|
|
33
45
|
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import config from '../config.js';
|
|
2
|
+
export async function validToken() {
|
|
3
|
+
if (config.isTest)
|
|
4
|
+
return 'token';
|
|
5
|
+
const token = config.token;
|
|
6
|
+
if (!token)
|
|
7
|
+
throw new Error('NO_TOKEN');
|
|
8
|
+
const url = `${config.apiUrl}v2025-04-23/users/me`;
|
|
9
|
+
const response = await fetch(url, {
|
|
10
|
+
method: 'GET',
|
|
11
|
+
headers: {
|
|
12
|
+
Accept: 'application/json',
|
|
13
|
+
Authorization: `Bearer ${token}`,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
if (response.ok) {
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
if (data.id)
|
|
19
|
+
return token;
|
|
20
|
+
throw new Error('NO_USER');
|
|
21
|
+
}
|
|
22
|
+
throw new Error('SERVER_ERROR', { cause: response.statusText });
|
|
23
|
+
}
|
|
24
|
+
export async function validTokenOrErrorMessage() {
|
|
25
|
+
try {
|
|
26
|
+
const token = await validToken();
|
|
27
|
+
return { token };
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
if (e instanceof Error) {
|
|
31
|
+
switch (e.message) {
|
|
32
|
+
case 'NO_TOKEN':
|
|
33
|
+
return {
|
|
34
|
+
error: {
|
|
35
|
+
e,
|
|
36
|
+
message: 'No API token found. Use `npx @sanity/cli login` to login.',
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
case 'NO_USER':
|
|
40
|
+
return {
|
|
41
|
+
error: {
|
|
42
|
+
e,
|
|
43
|
+
message: 'User is not authenticated. Use `npx @sanity/cli login` to login.',
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
case 'SERVER_ERROR':
|
|
47
|
+
return {
|
|
48
|
+
error: {
|
|
49
|
+
e,
|
|
50
|
+
message: `Authentication server error: "${e.cause}". Try again later`,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
default:
|
|
54
|
+
return { error: { e, message: e.message } };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { error: { e, message: 'Unknown error' } };
|
|
58
|
+
}
|
|
59
|
+
}
|
package/oclif.manifest.json
CHANGED