@opscompass/opscompass-cli 1.0.6 → 1.0.9

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/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
+ })();
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Users need to be able to enter array data into the CLI. They do this by submitting identical argument names with each element value.
3
+ * This method consolidates all data associated with the specified argument names, and creates an array object.
4
+ * @param {string[]} argumentNames an array of the argument names to consolidate on
5
+ * @param {string[]} args the raw arguments to use when consolidating the arrays
6
+ * @returns the arguments with the array arguments consolidated into arrays.
7
+ */
8
+ function setArgumentArrays(argumentNames, args) {
9
+ const indexesToRemove = [];
10
+
11
+ // iterate over each expected array argument name
12
+ argumentNames.forEach((argumentName) => {
13
+ let capturingArgument = false;
14
+
15
+ // Filter args down to array of values that match the current argument name
16
+ const argumentArray = args.filter(function(value, index) {
17
+ // The args array elements first have the argument name, and the element following is the value.
18
+ if (value === argumentName) {
19
+ // we want to mark this element for future deletion because it is a matching argument name,
20
+ // and set capturingArgument to true so it captures the next element in the array
21
+ indexesToRemove.push(index);
22
+ capturingArgument = true;
23
+ } else if (capturingArgument) {
24
+ // we want to mark this value for future deletion, set capturingArgument to false (since we just want to capture one array element),
25
+ // and then return true to return the argument value in the end array.
26
+ indexesToRemove.push(index);
27
+ capturingArgument = false;
28
+ return true;
29
+ }
30
+ });
31
+
32
+ // Appending the original argument name and the finalized argument array
33
+ // to the end of the args array will prevent it from being deleted.
34
+ if (argumentArray.length) {
35
+ args.push(argumentName);
36
+ args.push(argumentArray);
37
+ }
38
+ });
39
+
40
+ // Now that we are done, we want to delete all elements that match the CLI array argument structure
41
+ args = args.filter(function(value, index) {
42
+ return indexesToRemove.indexOf(index) == -1;
43
+ });
44
+
45
+ return args;
46
+ }
47
+
48
+ module.exports = {
49
+ setArgumentArrays
50
+ };
package/license.txt ADDED
@@ -0,0 +1,33 @@
1
+ OPCOMPASS™ COMMAND LINE INTERFACE
2
+ LICENSE AGREEMENT
3
+
4
+ IMPORTANT! READ CAREFULLY: THIS IS A LEGAL AGREEMENT. BY DOWNLOADING, INSTALLING, COPYING, SAVING ON YOUR COMPUTER, OR OTHERWISE USING THIS TOOL, YOU (LICENSEE, AS DEFINED BELOW) ARE BECOMING A PARTY TO THIS AGREEMENT AND YOU ARE CONSENTING TO BE BOUND BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
5
+ IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT, YOU SHOULD NOT DOWNLOAD, INSTALL, OR USE THE TOOL.
6
+
7
+ 1. PARTIES
8
+ (a) "Licensor" means OpsCompass, Inc., having its principal place of business at 11422 Miracle Hills Dr #300, Omaha, NE 68154.
9
+ (b) "Licensee" means an individual or a legal entity entering into and exercising rights under this Agreement or future versions thereof. Licensee is also referred to herein as "You".
10
+
11
+ 2. DEFINITIONS
12
+ "Tool" means the software program known as the OpsCompass™ Command Line Interface in its delivered form, including its source code and documentation (if any).
13
+
14
+ 3. OWNERSHIP
15
+ The Tool is the property of Licensor. The Tool is licensed, not sold. Title and copyrights to the Tool, in whole and in part and all copies thereof, and all modifications, enhancements, derivatives and other alterations of the Tool regardless of who made any modifications, if any, are, and will remain, the sole and exclusive property of Licensor.
16
+
17
+ 4. GRANT OF LICENSE
18
+ Subject to the terms, conditions, and limitations set forth in this Agreement, including any amendments thereto, Licensor hereby grants to Licensee a limited, non-exclusive, perpetual license to install and use the Tool in accordance with the Tool documentation and pursuant to the terms and conditions of this Agreement.
19
+
20
+ 5. FEEDBACK
21
+ Licensee may, but is not obligated to, provide to Licensor or its subsidiaries suggestions, comments and feedback regarding the Tool, including but not limited to usability, bug reports and test results (collectively, "Feedback"). All right, title, and interest in the Feedback shall belong to Licensor for its unlimited use and Licensee agrees to perform any reasonable tasks necessary to document Licensor’s ownership in the same.
22
+
23
+ 6. NO WARRANTY
24
+ THE TOOL IS PROVIDED TO LICENSEE "AS IS" AND WITHOUT WARRANTIES. LICENSOR MAKES NO WARRANTY AS TO ITS USE OR PERFORMANCE. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, LICENSOR AND ITS AFFILIATES, SUPPLIERS, AND RESELLERS, DISCLAIM ALL OTHER WARRANTIES AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE TOOL, AND THE PROVISION OF OR FAILURE TO PROVIDE SUPPORT SERVICES.
25
+
26
+ 7. DISCLAIMER OF DAMAGES
27
+ (a) TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR OR ITS AFFILIATES, LICENSORS, SUPPLIERS OR RESELLERS BE LIABLE TO LICENSEE UNDER ANY THEORY FOR ANY DAMAGES SUFFERED BY LICENSEE OR ANY USER OF THE TOOL, OR FOR ANY SPECIAL, INCIDENTAL, INDIRECT, CONSEQUENTIAL, OR SIMILAR DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS OR CONFIDENTIAL OR OTHER INFORMATION, FOR BUSINESS INTERRUPTION, FOR PERSONAL INJURY, FOR LOSS OF PRIVACY, FOR FAILURE TO MEET ANY DUTY INCLUDING OF GOOD FAITH OR OF REASONABLE CARE, FOR NEGLIGENCE, AND FOR ANY OTHER PECUNIARY OR OTHER LOSS WHATSOEVER) ARISING OUT OF THE USE OR INABILITY TO USE THE TOOL, OR THE PROVISION OF OR FAILURE TO PROVIDE SUPPORT SERVICES, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND REGARDLESS OF THE LEGAL OR EQUITABLE THEORY (CONTRACT, TORT OR OTHERWISE) UPON WHICH THE CLAIM IS BASED.
28
+ (b) IN ANY CASE, LICENSOR'S ENTIRE LIABILITY UNDER ANY PROVISION OF THIS AGREEMENT WILL BE LIMITED TO THE AMOUNT ACTUALLY PAID BY LICENSEE FOR THE TOOL UNDER THIS AGREEMENT OR FIVE (5) USD, WHICHEVER IS GREATER.
29
+
30
+ 8. GENERAL
31
+ (a) Licensor reserves the right at any time to cease the support of the Tool and to alter features, specifications, capabilities, functions, licensing terms, release dates, general availability or other characteristics of the Tool.
32
+ (b) This Agreement constitutes the entire agreement between the parties concerning Licensee's use of the Tool, and supersedes any and all prior or contemporaneous oral or written representations, communications, or advertising with respect to the Tool.
33
+ (c) This Agreement will be governed by the laws of Nebraska, in the United States, without reference to conflict of laws principles. Licensee agrees that any litigation relating to this Agreement may only be brought in, and will be subject to the jurisdiction of, the state, local, and federal courts located in Douglas County, Nebraska.
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;
@@ -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;