UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

96 lines 5.15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const console_1 = require("console"); const sdk_1 = require("../../api/sdk"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const color = command_utils_1.getColor("magenta"); exports.default = (program) => { program .command("list-assets") .alias("la") .option("-f, --filter [filter]", `filter (see: ${color("https://developer.mindsphere.io/apis/advanced-assetmanagement/api-assetmanagement-references-filtering.html")}) `) .option("-a, --assetname [name]", "search for assets with string [name] in asset name") .option("-t, --typeid [typeid]", "search for assets with string [typeid] in typeid") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", 3) .option("-v, --verbose", "verbose output") .description(color(`list assets in the tenant *`)) .action(options => { (() => __awaiter(void 0, void 0, void 0, function* () { try { command_utils_1.homeDirLog(options.verbose, color); command_utils_1.proxyLog(options.verbose, color); checkRequiredParameters(options); const auth = utils_1.loadAuth(); const sdk = new sdk_1.MindSphereSdk({ gateway: auth.gateway, basicAuth: utils_1.decrypt(auth, options.passkey), tenant: auth.tenant }); const assetMgmt = sdk.GetAssetManagementClient(); let page = 0; let assets; const filter = buildFilter(options); command_utils_1.verboseLog(JSON.stringify(filter, null, 2), options.verbose); console.log(`assetid etag twintype [typeid] name`); let assetCount = 0; do { assets = (yield utils_1.retry(options.retry, () => assetMgmt.GetAssets({ page: page, size: 100, filter: Object.keys(filter).length === 0 ? undefined : JSON.stringify(filter), sort: "name,asc" }))); assets._embedded = assets._embedded || { assets: [] }; assets.page = assets.page || { totalPages: 0 }; for (const asset of assets._embedded.assets || []) { assetCount++; console.log(`${asset.assetId} ${asset.etag}\t${asset.twinType}\t[${asset.typeId}]\t${color(asset.name)}`); command_utils_1.verboseLog(JSON.stringify(asset, null, 2), options.verbose); } } while (page++ < (assets.page.totalPages || 0)); console.log(`${color(assetCount)} assets listed.\n`); } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { console_1.log("\n Examples:\n"); console_1.log(` mc list-assets --passkey mypasskey \t\t\t\t\t\tlist all assets`); console_1.log(` mc la --typeid core.mclib --assetname nodered --passkey mypasskey \t\tlist all agents (assets of type core.mclib) with nodered in the name`); console_1.log(` mc la --filter '{"name" : {"contains" : "Engine"}}' --passkey mypasskey \tlist all assets where name contains string Engine`); command_utils_1.serviceCredentialLog(); }); }; function buildFilter(options) { const filter = (options.filter && JSON.parse(options.filter)) || {}; let pointer = filter; if (options.assetname !== undefined && options.typeid !== undefined) { filter.and = {}; pointer = filter.and; } if (options.assetname) { pointer.name = { contains: `${options.assetname}` }; } if (options.typeid) { pointer.typeId = { contains: `${options.typeid}` }; } return filter; } function checkRequiredParameters(options) { !options.passkey && command_utils_1.errorLog("you have to provide a passkey to get the service token (run mc la --help for full description)", true); } //# sourceMappingURL=mc-list-assets.js.map