@opscompass/opscompass-cli 1.0.6 → 1.0.7
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 +106 -106
- package/credentials.js +96 -96
- package/environment/current.js +21 -21
- package/environment/demo.js +21 -0
- package/environment/dev.js +21 -0
- package/environment/local.js +22 -0
- package/environment/production.js +21 -0
- package/error.js +13 -13
- package/index.js +51 -51
- package/login.js +147 -147
- package/modules/accounts.js +56 -56
- package/modules/resources.js +197 -197
- package/modules.js +11 -11
- package/oauth.js +29 -29
- package/opscompass.js +12 -12
- package/package.json +25 -22
package/index.js
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const modules = require("./modules.js");
|
|
4
|
-
const handleError = require('./error.js');
|
|
5
|
-
const { canReadStoredCredentials } = require("./credentials.js");
|
|
6
|
-
|
|
7
|
-
function requiresLogin() {
|
|
8
|
-
console.log("You aren't logged in. Please run `opscompass login` to log in.");
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function loggedIn() {
|
|
12
|
-
return canReadStoredCredentials();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function help() {
|
|
16
|
-
console.log("OpsCompass Command-Line Interface");
|
|
17
|
-
console.log();
|
|
18
|
-
console.log("Command format:");
|
|
19
|
-
console.log(" opscompass module action --parameter1 value --parameter2 value");
|
|
20
|
-
console.log();
|
|
21
|
-
console.log("Available modules:");
|
|
22
|
-
console.log();
|
|
23
|
-
for (const module of Object.getOwnPropertyNames(modules)) {
|
|
24
|
-
console.log(module);
|
|
25
|
-
console.log(modules[module].description);
|
|
26
|
-
console.log();
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
(async function() {
|
|
31
|
-
const [proc, command, commandModule, ...args] = process.argv;
|
|
32
|
-
const moduleName = commandModule?.toLowerCase() ?? "";
|
|
33
|
-
if (moduleName === "" || moduleName === "help") {
|
|
34
|
-
return help();
|
|
35
|
-
}
|
|
36
|
-
if (modules[moduleName]) {
|
|
37
|
-
const module = modules[moduleName];
|
|
38
|
-
if (!module.requiresLogin || loggedIn()) {
|
|
39
|
-
try {
|
|
40
|
-
await module(...args);
|
|
41
|
-
} catch (e) {
|
|
42
|
-
handleError(e);
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
requiresLogin();
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
console.error("Unknown module:", moduleName);
|
|
49
|
-
return help();
|
|
50
|
-
}
|
|
51
|
-
})();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const modules = require("./modules.js");
|
|
4
|
+
const handleError = require('./error.js');
|
|
5
|
+
const { canReadStoredCredentials } = require("./credentials.js");
|
|
6
|
+
|
|
7
|
+
function requiresLogin() {
|
|
8
|
+
console.log("You aren't logged in. Please run `opscompass login` to log in.");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function loggedIn() {
|
|
12
|
+
return canReadStoredCredentials();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function help() {
|
|
16
|
+
console.log("OpsCompass Command-Line Interface");
|
|
17
|
+
console.log();
|
|
18
|
+
console.log("Command format:");
|
|
19
|
+
console.log(" opscompass module action --parameter1 value --parameter2 value");
|
|
20
|
+
console.log();
|
|
21
|
+
console.log("Available modules:");
|
|
22
|
+
console.log();
|
|
23
|
+
for (const module of Object.getOwnPropertyNames(modules)) {
|
|
24
|
+
console.log(module);
|
|
25
|
+
console.log(modules[module].description);
|
|
26
|
+
console.log();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
(async function() {
|
|
31
|
+
const [proc, command, commandModule, ...args] = process.argv;
|
|
32
|
+
const moduleName = commandModule?.toLowerCase() ?? "";
|
|
33
|
+
if (moduleName === "" || moduleName === "help") {
|
|
34
|
+
return help();
|
|
35
|
+
}
|
|
36
|
+
if (modules[moduleName]) {
|
|
37
|
+
const module = modules[moduleName];
|
|
38
|
+
if (!module.requiresLogin || loggedIn()) {
|
|
39
|
+
try {
|
|
40
|
+
await module(...args);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
handleError(e);
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
requiresLogin();
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
console.error("Unknown module:", moduleName);
|
|
49
|
+
return help();
|
|
50
|
+
}
|
|
51
|
+
})();
|
package/login.js
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
const crypto = require("crypto");
|
|
2
|
-
const open = require("open");
|
|
3
|
-
const http = require("http");
|
|
4
|
-
const { storeCredentials } = require("./credentials");
|
|
5
|
-
const { redeemDeviceCode, getDeviceCode, redeemAuthorizationCode, generateAuthorizeUrl } = require("./oauth");
|
|
6
|
-
|
|
7
|
-
function base64UrlEncode(data) {
|
|
8
|
-
return data.toString("base64")
|
|
9
|
-
.replace(/\+/g, '-')
|
|
10
|
-
.replace(/\//g, '_')
|
|
11
|
-
.replace(/=/g, '')
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function randomBytesBase64UrlEncoded(bytes) {
|
|
15
|
-
return base64UrlEncode(crypto.randomBytes(bytes));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function checkForToken(deviceCode, interval, timeToExpire) {
|
|
19
|
-
try {
|
|
20
|
-
const tokenResponse = await redeemDeviceCode(deviceCode);
|
|
21
|
-
await storeCredentials(tokenResponse.data);
|
|
22
|
-
console.log("You're logged into OpsCompass!");
|
|
23
|
-
} catch (e) {
|
|
24
|
-
if (!e.response) {
|
|
25
|
-
console.error(e);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
switch (e.response.data.error) {
|
|
29
|
-
case "access_denied":
|
|
30
|
-
console.log("Unable to authorize this device.");
|
|
31
|
-
console.log(e.data.error_description);
|
|
32
|
-
break;
|
|
33
|
-
case "expired_token":
|
|
34
|
-
console.log("The device was not authorized in time. Run `opscompass login --use-device-code` again.");
|
|
35
|
-
break;
|
|
36
|
-
case "authorization_pending":
|
|
37
|
-
const newTimeToExpire = timeToExpire - interval;
|
|
38
|
-
if (newTimeToExpire > 0) {
|
|
39
|
-
setTimeout(() => checkForToken(deviceCode, interval, newTimeToExpire), interval * 1000);
|
|
40
|
-
} else {
|
|
41
|
-
console.log("The device was not authorized in time. Run `opscompass login --use-device-code` again.");
|
|
42
|
-
}
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async function deviceCodeFlow() {
|
|
49
|
-
try {
|
|
50
|
-
const deviceCodeResponse = await getDeviceCode();
|
|
51
|
-
|
|
52
|
-
const deviceCodeData = deviceCodeResponse.data;
|
|
53
|
-
if (deviceCodeData) {
|
|
54
|
-
console.log("Log in to OpsCompass by loading the following URL into your external browser")
|
|
55
|
-
console.log();
|
|
56
|
-
console.log(deviceCodeData.verification_uri_complete);
|
|
57
|
-
console.log();
|
|
58
|
-
console.log("Need to type the URL manually? You can visit and provide the device code.");
|
|
59
|
-
console.log();
|
|
60
|
-
console.log(deviceCodeData.verification_uri);
|
|
61
|
-
console.log();
|
|
62
|
-
console.log("Provide or verify this device code in the activation process:");
|
|
63
|
-
console.log();
|
|
64
|
-
console.log(deviceCodeData.user_code);
|
|
65
|
-
setTimeout(() => checkForToken(deviceCodeData.device_code, deviceCodeData.interval, deviceCodeData.expires_in - deviceCodeData.interval), deviceCodeData.interval * 1000);
|
|
66
|
-
}
|
|
67
|
-
} catch (e) {
|
|
68
|
-
console.error(e);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function printHelp() {
|
|
73
|
-
console.log("Logging in to OpsCompass");
|
|
74
|
-
console.log();
|
|
75
|
-
console.log("By default, `opscompass login` will open the system's default browser to");
|
|
76
|
-
console.log("complete the login process. You log in to OpsCompass in the browser with the");
|
|
77
|
-
console.log("credentials you use to log into the OpsCompass portal. We use that identity to");
|
|
78
|
-
console.log("access OpsCompass's APIs. You have access to the same data through the APIs");
|
|
79
|
-
console.log("that you do through the portal.");
|
|
80
|
-
console.log();
|
|
81
|
-
console.log("If this device does not have access to an interactive browser (for instance,");
|
|
82
|
-
console.log("a Docker container or a \"headless\" virtual machine), you can run:");
|
|
83
|
-
console.log();
|
|
84
|
-
console.log("opscompass login --use-device-code");
|
|
85
|
-
console.log();
|
|
86
|
-
console.log("It will print a URL and a code which you can use to authorize this device using");
|
|
87
|
-
console.log("a browser on another device, such as a laptop or mobile device.");
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
let loginModule = async (...args) => {
|
|
91
|
-
if (args?.some(arg => "help" === arg.toLowerCase())) return printHelp();
|
|
92
|
-
if (args?.some(arg => "--use-device-code" === arg.toLowerCase())) return deviceCodeFlow();
|
|
93
|
-
|
|
94
|
-
console.log("Launching the native browser.");
|
|
95
|
-
console.log("If no browser is available or a browser can't be launched, cancel this command with ^C");
|
|
96
|
-
console.log("and run the command `opscompass login --use-device-code`.");
|
|
97
|
-
|
|
98
|
-
// code_verifier: 32 bytes of CRNG data encoded in url-safe base64, without padding
|
|
99
|
-
const code_verifier = randomBytesBase64UrlEncoded(32);
|
|
100
|
-
|
|
101
|
-
// code_challenge: the sha256 hash of the code_verifier
|
|
102
|
-
const code_challenge = base64UrlEncode(crypto.createHash('sha256')
|
|
103
|
-
.update(code_verifier)
|
|
104
|
-
.digest());
|
|
105
|
-
|
|
106
|
-
// csrfState: 32 more CRNG bytes used to verify the tokens sent into the response url
|
|
107
|
-
// actually are in response to this request
|
|
108
|
-
const csrfState = randomBytesBase64UrlEncoded(32);
|
|
109
|
-
|
|
110
|
-
const loginUrl = generateAuthorizeUrl(code_challenge, csrfState);
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
await open(loginUrl);
|
|
114
|
-
const server = http.createServer(async (req, res) => {
|
|
115
|
-
// req.url is the path, it leaves off the origin
|
|
116
|
-
const requestUrl = new URL("http://localhost:8400" + req.url);
|
|
117
|
-
const requestQuery = requestUrl.searchParams;
|
|
118
|
-
const requestState = requestQuery.get("state");
|
|
119
|
-
if (csrfState === requestState) {
|
|
120
|
-
const code = requestQuery.get("code");
|
|
121
|
-
|
|
122
|
-
const authResult = await redeemAuthorizationCode(code, code_verifier);
|
|
123
|
-
if (authResult.data?.access_token) {
|
|
124
|
-
await storeCredentials(authResult.data);
|
|
125
|
-
console.log("You're logged into OpsCompass!");
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
res.writeHead(200, {"Content-Type": "text/html"});
|
|
129
|
-
res.end("<html><head><title>Logging in...</title></head><body><h1>Logging in...</h1><p>The OpsCompass CLI should now be logged in.</p><p>You can close this window.</p></body></html>");
|
|
130
|
-
} else {
|
|
131
|
-
res.writeHead(200, {"Content-Type": "text/html"});
|
|
132
|
-
res.end("<html><head><title>Problems...</title></head><body><h1>Problems logging in</h1><p>Please try running `opscompass login` again. If it doesn't work, try running `opscompass login --use-device-code` instead.</p></body></html>");
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// only allow one request...
|
|
136
|
-
process.nextTick(() => server.close());
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
server.listen(8400);
|
|
140
|
-
} catch {
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
loginModule.requiresLogin = false;
|
|
145
|
-
loginModule.description = "Authorize the CLI using your OpsCompass credentials.";
|
|
146
|
-
|
|
147
|
-
module.exports = loginModule;
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
const open = require("open");
|
|
3
|
+
const http = require("http");
|
|
4
|
+
const { storeCredentials } = require("./credentials");
|
|
5
|
+
const { redeemDeviceCode, getDeviceCode, redeemAuthorizationCode, generateAuthorizeUrl } = require("./oauth");
|
|
6
|
+
|
|
7
|
+
function base64UrlEncode(data) {
|
|
8
|
+
return data.toString("base64")
|
|
9
|
+
.replace(/\+/g, '-')
|
|
10
|
+
.replace(/\//g, '_')
|
|
11
|
+
.replace(/=/g, '')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function randomBytesBase64UrlEncoded(bytes) {
|
|
15
|
+
return base64UrlEncode(crypto.randomBytes(bytes));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function checkForToken(deviceCode, interval, timeToExpire) {
|
|
19
|
+
try {
|
|
20
|
+
const tokenResponse = await redeemDeviceCode(deviceCode);
|
|
21
|
+
await storeCredentials(tokenResponse.data);
|
|
22
|
+
console.log("You're logged into OpsCompass!");
|
|
23
|
+
} catch (e) {
|
|
24
|
+
if (!e.response) {
|
|
25
|
+
console.error(e);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
switch (e.response.data.error) {
|
|
29
|
+
case "access_denied":
|
|
30
|
+
console.log("Unable to authorize this device.");
|
|
31
|
+
console.log(e.data.error_description);
|
|
32
|
+
break;
|
|
33
|
+
case "expired_token":
|
|
34
|
+
console.log("The device was not authorized in time. Run `opscompass login --use-device-code` again.");
|
|
35
|
+
break;
|
|
36
|
+
case "authorization_pending":
|
|
37
|
+
const newTimeToExpire = timeToExpire - interval;
|
|
38
|
+
if (newTimeToExpire > 0) {
|
|
39
|
+
setTimeout(() => checkForToken(deviceCode, interval, newTimeToExpire), interval * 1000);
|
|
40
|
+
} else {
|
|
41
|
+
console.log("The device was not authorized in time. Run `opscompass login --use-device-code` again.");
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function deviceCodeFlow() {
|
|
49
|
+
try {
|
|
50
|
+
const deviceCodeResponse = await getDeviceCode();
|
|
51
|
+
|
|
52
|
+
const deviceCodeData = deviceCodeResponse.data;
|
|
53
|
+
if (deviceCodeData) {
|
|
54
|
+
console.log("Log in to OpsCompass by loading the following URL into your external browser")
|
|
55
|
+
console.log();
|
|
56
|
+
console.log(deviceCodeData.verification_uri_complete);
|
|
57
|
+
console.log();
|
|
58
|
+
console.log("Need to type the URL manually? You can visit and provide the device code.");
|
|
59
|
+
console.log();
|
|
60
|
+
console.log(deviceCodeData.verification_uri);
|
|
61
|
+
console.log();
|
|
62
|
+
console.log("Provide or verify this device code in the activation process:");
|
|
63
|
+
console.log();
|
|
64
|
+
console.log(deviceCodeData.user_code);
|
|
65
|
+
setTimeout(() => checkForToken(deviceCodeData.device_code, deviceCodeData.interval, deviceCodeData.expires_in - deviceCodeData.interval), deviceCodeData.interval * 1000);
|
|
66
|
+
}
|
|
67
|
+
} catch (e) {
|
|
68
|
+
console.error(e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function printHelp() {
|
|
73
|
+
console.log("Logging in to OpsCompass");
|
|
74
|
+
console.log();
|
|
75
|
+
console.log("By default, `opscompass login` will open the system's default browser to");
|
|
76
|
+
console.log("complete the login process. You log in to OpsCompass in the browser with the");
|
|
77
|
+
console.log("credentials you use to log into the OpsCompass portal. We use that identity to");
|
|
78
|
+
console.log("access OpsCompass's APIs. You have access to the same data through the APIs");
|
|
79
|
+
console.log("that you do through the portal.");
|
|
80
|
+
console.log();
|
|
81
|
+
console.log("If this device does not have access to an interactive browser (for instance,");
|
|
82
|
+
console.log("a Docker container or a \"headless\" virtual machine), you can run:");
|
|
83
|
+
console.log();
|
|
84
|
+
console.log("opscompass login --use-device-code");
|
|
85
|
+
console.log();
|
|
86
|
+
console.log("It will print a URL and a code which you can use to authorize this device using");
|
|
87
|
+
console.log("a browser on another device, such as a laptop or mobile device.");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let loginModule = async (...args) => {
|
|
91
|
+
if (args?.some(arg => "help" === arg.toLowerCase())) return printHelp();
|
|
92
|
+
if (args?.some(arg => "--use-device-code" === arg.toLowerCase())) return deviceCodeFlow();
|
|
93
|
+
|
|
94
|
+
console.log("Launching the native browser.");
|
|
95
|
+
console.log("If no browser is available or a browser can't be launched, cancel this command with ^C");
|
|
96
|
+
console.log("and run the command `opscompass login --use-device-code`.");
|
|
97
|
+
|
|
98
|
+
// code_verifier: 32 bytes of CRNG data encoded in url-safe base64, without padding
|
|
99
|
+
const code_verifier = randomBytesBase64UrlEncoded(32);
|
|
100
|
+
|
|
101
|
+
// code_challenge: the sha256 hash of the code_verifier
|
|
102
|
+
const code_challenge = base64UrlEncode(crypto.createHash('sha256')
|
|
103
|
+
.update(code_verifier)
|
|
104
|
+
.digest());
|
|
105
|
+
|
|
106
|
+
// csrfState: 32 more CRNG bytes used to verify the tokens sent into the response url
|
|
107
|
+
// actually are in response to this request
|
|
108
|
+
const csrfState = randomBytesBase64UrlEncoded(32);
|
|
109
|
+
|
|
110
|
+
const loginUrl = generateAuthorizeUrl(code_challenge, csrfState);
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
await open(loginUrl);
|
|
114
|
+
const server = http.createServer(async (req, res) => {
|
|
115
|
+
// req.url is the path, it leaves off the origin
|
|
116
|
+
const requestUrl = new URL("http://localhost:8400" + req.url);
|
|
117
|
+
const requestQuery = requestUrl.searchParams;
|
|
118
|
+
const requestState = requestQuery.get("state");
|
|
119
|
+
if (csrfState === requestState) {
|
|
120
|
+
const code = requestQuery.get("code");
|
|
121
|
+
|
|
122
|
+
const authResult = await redeemAuthorizationCode(code, code_verifier);
|
|
123
|
+
if (authResult.data?.access_token) {
|
|
124
|
+
await storeCredentials(authResult.data);
|
|
125
|
+
console.log("You're logged into OpsCompass!");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
res.writeHead(200, {"Content-Type": "text/html"});
|
|
129
|
+
res.end("<html><head><title>Logging in...</title></head><body><h1>Logging in...</h1><p>The OpsCompass CLI should now be logged in.</p><p>You can close this window.</p></body></html>");
|
|
130
|
+
} else {
|
|
131
|
+
res.writeHead(200, {"Content-Type": "text/html"});
|
|
132
|
+
res.end("<html><head><title>Problems...</title></head><body><h1>Problems logging in</h1><p>Please try running `opscompass login` again. If it doesn't work, try running `opscompass login --use-device-code` instead.</p></body></html>");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// only allow one request...
|
|
136
|
+
process.nextTick(() => server.close());
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
server.listen(8400);
|
|
140
|
+
} catch {
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
loginModule.requiresLogin = false;
|
|
145
|
+
loginModule.description = "Authorize the CLI using your OpsCompass credentials.";
|
|
146
|
+
|
|
147
|
+
module.exports = loginModule;
|
package/modules/accounts.js
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
const { getOpsCompassClient } = require("../opscompass.js");
|
|
2
|
-
|
|
3
|
-
async function listAccounts(args) {
|
|
4
|
-
const callParams = {};
|
|
5
|
-
for (let i = 0; i < args.length - 1; i++) {
|
|
6
|
-
switch (args[i].toLowerCase()) {
|
|
7
|
-
case "--company":
|
|
8
|
-
callParams.company = args[++i];
|
|
9
|
-
break;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const client = await getOpsCompassClient();
|
|
14
|
-
const account = await client.accounts.listAccounts(callParams);
|
|
15
|
-
|
|
16
|
-
console.log(JSON.stringify(account, null, 2));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function listCompanies() {
|
|
20
|
-
const client = await getOpsCompassClient();
|
|
21
|
-
const company = await client.accounts.listCompanies();
|
|
22
|
-
|
|
23
|
-
console.log(JSON.stringify(company, null, 2));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function printHelp() {
|
|
27
|
-
console.log("Actions available for accounts:");
|
|
28
|
-
console.log();
|
|
29
|
-
console.log("list-companies", "Lists all of the companies associated with your user.");
|
|
30
|
-
console.log("Parameters:");
|
|
31
|
-
console.log("None");
|
|
32
|
-
console.log();
|
|
33
|
-
console.log("list-accounts", "Lists all of the accounts associated with the given company.");
|
|
34
|
-
console.log("Parameters:");
|
|
35
|
-
console.log("--company [company_id]");
|
|
36
|
-
console.log();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const accountsModule = (action, ...args) => {
|
|
40
|
-
switch (action?.toLowerCase() ?? "") {
|
|
41
|
-
case "list-companies":
|
|
42
|
-
return listCompanies();
|
|
43
|
-
case "list-accounts":
|
|
44
|
-
return listAccounts(args);
|
|
45
|
-
case "help":
|
|
46
|
-
case "":
|
|
47
|
-
return printHelp();
|
|
48
|
-
default:
|
|
49
|
-
console.log("Unknown command:", action);
|
|
50
|
-
return printHelp();
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
accountsModule.requiresLogin = true;
|
|
55
|
-
accountsModule.description = "Provides access to details about Accounts in OpsCompass.";
|
|
56
|
-
|
|
1
|
+
const { getOpsCompassClient } = require("../opscompass.js");
|
|
2
|
+
|
|
3
|
+
async function listAccounts(args) {
|
|
4
|
+
const callParams = {};
|
|
5
|
+
for (let i = 0; i < args.length - 1; i++) {
|
|
6
|
+
switch (args[i].toLowerCase()) {
|
|
7
|
+
case "--company":
|
|
8
|
+
callParams.company = args[++i];
|
|
9
|
+
break;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const client = await getOpsCompassClient();
|
|
14
|
+
const account = await client.accounts.listAccounts(callParams);
|
|
15
|
+
|
|
16
|
+
console.log(JSON.stringify(account, null, 2));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function listCompanies() {
|
|
20
|
+
const client = await getOpsCompassClient();
|
|
21
|
+
const company = await client.accounts.listCompanies();
|
|
22
|
+
|
|
23
|
+
console.log(JSON.stringify(company, null, 2));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function printHelp() {
|
|
27
|
+
console.log("Actions available for accounts:");
|
|
28
|
+
console.log();
|
|
29
|
+
console.log("list-companies", "Lists all of the companies associated with your user.");
|
|
30
|
+
console.log("Parameters:");
|
|
31
|
+
console.log("None");
|
|
32
|
+
console.log();
|
|
33
|
+
console.log("list-accounts", "Lists all of the accounts associated with the given company.");
|
|
34
|
+
console.log("Parameters:");
|
|
35
|
+
console.log("--company [company_id]");
|
|
36
|
+
console.log();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const accountsModule = (action, ...args) => {
|
|
40
|
+
switch (action?.toLowerCase() ?? "") {
|
|
41
|
+
case "list-companies":
|
|
42
|
+
return listCompanies();
|
|
43
|
+
case "list-accounts":
|
|
44
|
+
return listAccounts(args);
|
|
45
|
+
case "help":
|
|
46
|
+
case "":
|
|
47
|
+
return printHelp();
|
|
48
|
+
default:
|
|
49
|
+
console.log("Unknown command:", action);
|
|
50
|
+
return printHelp();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
accountsModule.requiresLogin = true;
|
|
55
|
+
accountsModule.description = "Provides access to details about Accounts in OpsCompass.";
|
|
56
|
+
|
|
57
57
|
module.exports = accountsModule;
|