@mindconnect/mindconnect-nodejs
Version:
MindConnect Library for NodeJS (community based)
76 lines • 4.1 kB
JavaScript
;
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-files")
.alias("ls")
.option("-i, --assetid <assetid>", "asset id from the mindsphere ")
.option("-f, --filter [filter]", `filter (see: ${color("https://developer.mindsphere.io/apis/iot-iotfile/api-iotfile-references-filtering.html")}) `)
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", 3)
.option("-v, --verbose", "verbose output")
.description(color(`list files stored with the asset *`))
.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 iotFile = sdk.GetIoTFileClient();
let offset = 0;
let files;
console.log(`timestamp etag size path`);
do {
files = (yield utils_1.retry(options.retry, () => iotFile.GetFiles(`${options.assetid}`, {
offset: offset,
limit: 500,
count: true,
filter: options.filter
})));
for (const file of files) {
console.log(`${file.timestamp} ${file.etag.toString().padStart(4)} ${command_utils_1.humanFileSize(file.size || 0).padStart(10)}\t${file.path || ""}${color(file.name)}`);
command_utils_1.verboseLog(`${JSON.stringify(file, null, 2)}`, options.verbose);
}
offset += 500;
if (files.length <= 500)
break;
} while (files && files.length > 0);
}
catch (err) {
command_utils_1.errorLog(err, options.verbose);
}
}))();
})
.on("--help", () => {
console_1.log("\n Examples:\n");
console_1.log(` mc ls --assetid 1234...ef --passkey mypasskey \t\t\t\tlist all files for assetid`);
console_1.log(` mc ls --assetid 1234...ef --filter "path=upload*" --passkey mypasskey \tlist all files where path contains upload`);
command_utils_1.serviceCredentialLog();
});
};
function checkRequiredParameters(options) {
!options.passkey &&
command_utils_1.errorLog("you have to provide a passkey to get the service token (run mc ls --help for full description)", true);
!options.assetid && command_utils_1.errorLog("You have to provide an assetid.", true);
}
//# sourceMappingURL=mc-list-files.js.map