MCPcopy Create free account
hub / github.com/acode/cli / TokensAddToEnvCommand

Class TokensAddToEnvCommand

cli/commands/tokens/add-to-env.js:17–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15const TokensListCommand = require('./list.js');
16
17class TokensAddToEnvCommand extends Command {
18
19 constructor() {
20 super('tokens', 'add-to-env');
21 }
22
23 help() {
24 return {
25 description: 'Sets STDLIB_SECRET_TOKEN in env.json "local" field to the value of an existing token',
26 };
27 }
28
29 run(params, callback) {
30
31 let host = params.flags.h ? params.flags.h[0] : 'https://api.autocode.com';
32 let port = params.flags.p && params.flags.p[0];
33
34 let resource = new APIResource(host, port);
35
36 let pkg;
37 let env;
38
39 try {
40 pkg = serviceConfig.get();
41 } catch (e) {
42 return callback(e);
43 }
44
45 try {
46 env = require(path.join(process.cwd(), 'env.json'));
47 } catch (e) {
48 return callback(new Error(`Could not parse the "env.json" file in this directory.`))
49 }
50
51 let serviceName = pkg.stdlib.name;
52 if (!serviceName) {
53 return callback(new Error(`No "name" field found in package.json`));
54 }
55
56 resource.authorize(config.get('ACCESS_TOKEN'));
57 resource.request('/v1/library_tokens').index({
58 environment: 'dev'
59 }, async (err, response) => {
60
61 if (err) {
62 return callback(err);
63 }
64
65 let libraryTokens = response.data;
66 let matchingLibraryToken = libraryTokens.find((libraryToken) => {
67 return libraryToken.cachedToken &&
68 libraryToken.cachedToken.services &&
69 libraryToken.cachedToken.services.find((service) => {
70 return service.name === serviceName.toLowerCase() && service.environment === 'dev';
71 });
72 });
73
74 if (!!matchingLibraryToken) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected