@opscompass/opscompass-cli 1.0.23 → 1.0.24

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.
@@ -23,6 +23,14 @@ const exec = util.promisify(require("child_process").exec);
23
23
  * Lists script types that are locally installed
24
24
  */
25
25
  async function listLocalScripts(args) {
26
+ let quiet = false;
27
+ for (let i = 0; i < args.length; i++) {
28
+ switch (args[i].toLowerCase()) {
29
+ case "--quiet":
30
+ quiet = true;
31
+ }
32
+ }
33
+
26
34
  const scriptTypes = Object.values(ScriptType);
27
35
  let scriptsLocal;
28
36
 
@@ -50,16 +58,19 @@ async function listLocalScripts(args) {
50
58
  }
51
59
  }
52
60
 
53
- if (items.length > 0) {
54
- // Go with compact output for this display
55
- //console.log(JSON.stringify(scriptsLocal, null, 2));
56
- console.log(scriptsLocal);
57
- } else {
61
+ if (items.length <= 0) {
58
62
  throw "Cannot list scripts. Make sure the scripts are downloaded.\nIf the issue persists, contact your OpsCompass rep.";
63
+ } else {
64
+ if (!quiet) {
65
+ // Go with compact output for this display
66
+ // console.log(JSON.stringify(scriptsLocal, null, 2));
67
+ console.log(scriptsLocal);
68
+ }
59
69
  }
60
70
  } catch (err) {
61
71
  console.log(err);
62
72
  }
73
+
63
74
  return scriptsLocal;
64
75
  }
65
76
 
@@ -114,11 +125,14 @@ async function downloadScripts(args) {
114
125
  * Checks the available script versions from the server
115
126
  */
116
127
  async function listAvailableScripts(args) {
117
- for (let i = 0; i < args.length - 1; i++) {
128
+ let quiet = false;
129
+ for (let i = 0; i < args.length; i++) {
118
130
  switch (args[i].toLowerCase()) {
119
131
  case "--company":
120
132
  companySlug = args[++i];
121
133
  break;
134
+ case "--quiet":
135
+ quiet = true;
122
136
  }
123
137
  }
124
138
 
@@ -128,9 +142,13 @@ async function listAvailableScripts(args) {
128
142
  const scriptsRemote = await client.dataGatheringScripts.getScriptVersions(
129
143
  company
130
144
  );
131
- // Go with compact output for this display
132
- //console.log(JSON.stringify(scriptsRemote, null, 2));
133
- console.log(scriptsRemote);
145
+
146
+ if (!quiet) {
147
+ // Go with compact output for this display
148
+ //console.log(JSON.stringify(scriptsRemote, null, 2));
149
+ console.log(scriptsRemote);
150
+ }
151
+ return scriptsRemote;
134
152
  } catch (err) {
135
153
  console.log("List available script versions failed...");
136
154
  console.log(err);
@@ -139,11 +157,17 @@ async function listAvailableScripts(args) {
139
157
 
140
158
  /**
141
159
  * Checks the installed scripts version against the latest scripts version for a given scriptType.
160
+ * or all scriptTypes if not specified
142
161
  */
143
162
  async function checkScriptUpdates(args) {
144
163
  let scriptType = undefined;
145
- for (let i = 0; i < args.length - 1; i++) {
164
+ let autoUpdate = false;
165
+ for (let i = 0; i < args.length; i++) {
146
166
  switch (args[i].toLowerCase()) {
167
+ case "--auto-update":
168
+ case "--autoupdate":
169
+ autoUpdate = true;
170
+ break;
147
171
  case "--company":
148
172
  companySlug = args[++i];
149
173
  break;
@@ -154,47 +178,129 @@ async function checkScriptUpdates(args) {
154
178
  }
155
179
  }
156
180
 
157
- // If script type is not supported, we do not want to continue.
158
- const scriptTypes = Object.values(ScriptType);
159
- if (!scriptTypes.includes(scriptType)) {
160
- console.log("Unsupported script type...");
161
- return;
162
- }
163
-
164
181
  const client = await getOpsCompassClient();
165
- const currentScriptVersion = await getInstalledScriptVersion(scriptType);
166
182
 
167
- if (!currentScriptVersion) {
168
- console.log(
169
- `Local scripts for type ${scriptType} not found. Please make sure they are installed.`
170
- );
171
- console.log("If the issue persists, please contact your OpsCompass rep.");
172
- return;
173
- }
183
+ // If script type was specified, just check that one
184
+ if (scriptType) {
185
+ // If script type is not supported, we do not want to continue.
186
+ const scriptTypes = Object.values(ScriptType);
187
+ if (!scriptTypes.includes(scriptType)) {
188
+ console.log("Unsupported script type...");
189
+ return;
190
+ }
174
191
 
175
- const latestScriptVersion =
176
- await client.dataGatheringScripts.checkScriptUpdates(
177
- companySlug,
178
- scriptType
179
- );
192
+ const currentScriptVersion = await getInstalledScriptVersion(scriptType);
180
193
 
181
- //Semver returns true if the latest version is higher than the currentScriptVersion.
182
- if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
183
- console.log(
184
- yellowText,
185
- `Warning! You are using version ${currentScriptVersion} of the the ${scriptType} scripts, but the latest version is ${latestScriptVersion.scriptVersion}. To update to the latest version, please run:`
186
- );
187
- console.log(
188
- greenText,
189
- ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `
190
- );
191
- console.log();
194
+ if (!currentScriptVersion) {
195
+ console.log(
196
+ `Local scripts for type ${scriptType} not found. Please make sure they are installed.`
197
+ );
198
+ console.log("If the issue persists, please contact your OpsCompass rep.");
199
+ return;
200
+ }
201
+
202
+ console.log("Checking for latest version of module %s...", scriptType);
203
+ const latestScriptVersion =
204
+ await client.dataGatheringScripts.checkScriptUpdates(
205
+ companySlug,
206
+ scriptType
207
+ );
208
+
209
+ //Semver returns true if the latest version is higher than the currentScriptVersion.
210
+ if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
211
+ console.log(
212
+ yellowText,
213
+ `Warning! You are using version ${currentScriptVersion} of the the ${scriptType} scripts, but the latest version is ${latestScriptVersion.scriptVersion}. To update to the latest version, please run:`
214
+ );
215
+ console.log(
216
+ greenText,
217
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `
218
+ );
219
+ console.log();
220
+ } else {
221
+ console.log(
222
+ greenText,
223
+ `You are already using the latest version of ${scriptType} script, ${latestScriptVersion.scriptVersion}.`
224
+ );
225
+ console.log();
226
+ }
192
227
  } else {
193
- console.log(
194
- greenText,
195
- `You are already using the latest version of ${scriptType} script, ${latestScriptVersion.scriptVersion}.`
196
- );
197
- console.log();
228
+ const scriptsLocal = await listLocalScripts([...args, "--quiet"]);
229
+
230
+ if (
231
+ scriptsLocal &&
232
+ Array.isArray(scriptsLocal?.versions) &&
233
+ scriptsLocal.versions?.length > 0
234
+ ) {
235
+ // We have at least one scriptType downloaded
236
+ } else {
237
+ console.error("Scripts available for download:");
238
+ listAvailableScripts(args);
239
+ return;
240
+ }
241
+
242
+ // Sort by script-type
243
+ scriptsLocal.versions.sort(function (a, b) {
244
+ return a.scriptType < b.scriptType
245
+ ? -1
246
+ : a.scriptType > b.scriptType
247
+ ? 1
248
+ : 0;
249
+ });
250
+
251
+ console.log("\nFetching remote versions...");
252
+ const scriptsRemote = await listAvailableScripts([...args, "--quiet"]);
253
+
254
+ for (const scriptVer of scriptsLocal.versions) {
255
+ const remoteVer = scriptsRemote.versions.find(
256
+ (version) => version.scriptType === scriptVer.scriptType
257
+ );
258
+
259
+ const needsUpdate = semver.gt(
260
+ remoteVer.scriptVersion,
261
+ scriptVer.scriptVersion
262
+ );
263
+
264
+ if (needsUpdate) {
265
+ console.log(
266
+ "\nmodule %s: Local=%s, needs update=%s",
267
+ scriptVer.scriptType,
268
+ scriptVer.scriptVersion,
269
+ remoteVer.scriptVersion
270
+ );
271
+ console.log(
272
+ greenText,
273
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptVer.scriptType} \n`
274
+ );
275
+ if (autoUpdate) {
276
+ try {
277
+ // Get the latest version
278
+ const rcvdVersion = await downloadScriptsToHome(
279
+ scriptVer.scriptType,
280
+ undefined,
281
+ companySlug,
282
+ client
283
+ );
284
+
285
+ console.log(
286
+ "Downloaded %s (v%s)",
287
+ scriptVer.scriptType,
288
+ rcvdVersion
289
+ );
290
+ } catch (err) {
291
+ console.log("Download failed...");
292
+ console.log(err);
293
+ }
294
+ }
295
+ } else {
296
+ console.log(
297
+ "\nmodule %s: Local=%s, is up to date",
298
+ scriptVer.scriptType,
299
+ scriptVer.scriptVersion
300
+ );
301
+ }
302
+ }
303
+ //console.log(scriptsRemote);
198
304
  }
199
305
  }
200
306
 
@@ -288,11 +394,16 @@ async function printHelp() {
288
394
  "list-available",
289
395
  "Lists all script types and versions available for download"
290
396
  );
397
+ console.log("Parameters:");
398
+ console.log("--company [company_slug]");
399
+ console.log("--quiet does not generate output on stdout");
291
400
  console.log();
292
401
  console.log(
293
402
  "list-local",
294
403
  "Lists all locally downloaded script types and versions"
295
404
  );
405
+ console.log("Parameters:");
406
+ console.log("--quiet does not generate output on stdout");
296
407
  console.log();
297
408
  console.log(
298
409
  "download-scripts",
@@ -309,9 +420,10 @@ async function printHelp() {
309
420
  console.log();
310
421
  console.log(
311
422
  "check-script-updates",
312
- "Checks locally installed script version against the latest published version for the passed script type."
423
+ "Checks local script version against the latest published version for the passed script type, or all locally downloaded script types if none specified"
313
424
  );
314
425
  console.log("Parameters:");
426
+ console.log("--auto-update automatically downloads any needed updates");
315
427
  console.log("--company [company_slug]");
316
428
  console.log(
317
429
  "--script-type ['oracle-compliance', 'oracle-features', 'vsphere', 'msql-license', 'msql-compliance']"
@@ -343,7 +455,7 @@ const dataGatheringScriptsModule = (action, ...args) => {
343
455
  case "list-available":
344
456
  return listAvailableScripts(args);
345
457
  case "list-local":
346
- return listLocalScripts();
458
+ return listLocalScripts(args);
347
459
  case "manually-run-scripts":
348
460
  return manuallyRunScripts();
349
461
  case "help":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opscompass/opscompass-cli",
3
- "version": "1.0.23",
3
+ "version": "1.0.24",
4
4
  "description": "Calls OpsCompass's APIs",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,7 +15,7 @@
15
15
  "author": "opscompass",
16
16
  "license": "SEE LICENSE IN license.txt",
17
17
  "dependencies": {
18
- "@opscompass/opscompass-api": "^1.0.23",
18
+ "@opscompass/opscompass-api": "^1.0.24",
19
19
  "adm-zip": "^0.5.9",
20
20
  "axios": "^0.21.1",
21
21
  "fs-extra": "^10.1.0",