@zowe/cli
Version:
Zowe CLI is a command line interface (CLI) that provides a simple and streamlined way to interact with IBM z/OS.
143 lines • 7.54 kB
JavaScript
;
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
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 imperative_1 = require("@zowe/imperative");
const Tag_1 = require("./doc/Tag");
const ZosFiles_messages_1 = require("../../constants/ZosFiles.messages");
const ZosFiles_constants_1 = require("../../constants/ZosFiles.constants");
const rest_1 = require("../../../../../rest");
const path = require("path");
class Utilities {
/**
* Retrieve various details from USS file functions
*
* This function uses a JSON payload to retrieve information via zosmf utilities function
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {string} USSFileName - contains the file name
* @param {JSON} payload - contains the options to be sent
*
* @returns {Promise<Buffer>} Promise that resolves to json information
*
* @throws {ImperativeError}
*/
static putUSSPayload(session, USSFileName, payload) {
return __awaiter(this, void 0, void 0, function* () {
imperative_1.ImperativeExpect.toNotBeNullOrUndefined(USSFileName, ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message);
imperative_1.ImperativeExpect.toNotBeEqual(USSFileName, "", ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message);
imperative_1.ImperativeExpect.toNotBeNullOrUndefined(payload, ZosFiles_messages_1.ZosFilesMessages.missingPayload.message);
imperative_1.ImperativeExpect.toNotBeEqual(payload, {}, ZosFiles_messages_1.ZosFilesMessages.missingPayload.message);
USSFileName = path.posix.normalize(USSFileName);
// Get a proper destination for the file to be downloaded
// If the "file" is not provided, we create a folder structure similar to the uss file structure
if (USSFileName.substr(0, 1) === "/") {
USSFileName = USSFileName.substr(1);
}
USSFileName = encodeURIComponent(USSFileName);
const endpoint = path.posix.join(ZosFiles_constants_1.ZosFilesConstants.RESOURCE, ZosFiles_constants_1.ZosFilesConstants.RES_USS_FILES, USSFileName);
const reqHeaders = [imperative_1.Headers.APPLICATION_JSON, { [imperative_1.Headers.CONTENT_LENGTH]: JSON.stringify(payload).length.toString() }];
const response = yield rest_1.ZosmfRestClient.putExpectBuffer(session, endpoint, reqHeaders, payload);
return response;
});
}
/**
* Changes the tag attributes associate with a file using function chtag
*
* This function uses a JSON payload to set the information via zosmf utilities function
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {Tag} type - enum of chtag type of Test, Binary or Mixed
* @param {string} codeset - optional string describing codeset e.g. IBM-1047
*
* @returns {IZosFilesResponse>} Promise that resolves to response object
*
* @throws {ImperativeError}
*/
static chtag(session, ussFileName, type, codeset) {
return __awaiter(this, void 0, void 0, function* () {
imperative_1.ImperativeExpect.toNotBeNullOrUndefined(ussFileName, ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message);
if (type === Tag_1.Tag.BINARY) {
imperative_1.ImperativeExpect.toBeEqual(codeset, undefined, "A codeset cannot be specified for a binary file.");
}
const payload = { request: "chtag", action: "set", type: type.valueOf() };
if (codeset) {
payload.codeset = codeset;
}
yield Utilities.putUSSPayload(session, ussFileName, payload);
return {
success: true,
commandResponse: "File tagged successfully."
};
});
}
/**
* Based upon the files chtag value. Identify how the tagging should be interpretted when transferring the contents
* For example an EBCDIC file would always be converted from it's EBCDIC value to the ASCII
*
* An ASCII file value or binary should not be converted.
*
* The default value if the tag is not set or in an invalid state correctly is to convert.
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {string} USSFileName - contains the file name
*
* @returns {Promise<boolean>} Promise that resolves to true if the file is binary or ASCII text or false if file
* should likely be converted to text. Default is false which aligns with the zosmf default behavior converting
*
* @throws {ImperativeError}
*/
static isFileTagBinOrAscii(session, USSFileName) {
return __awaiter(this, void 0, void 0, function* () {
const payload = { request: "chtag", action: "list" };
const response = yield Utilities.putUSSPayload(session, USSFileName, payload);
const jsonObj = JSON.parse(response.toString());
if (jsonObj.hasOwnProperty("stdout")) {
const stdout = JSON.parse(response.toString()).stdout[0];
// Tests if binary tag set
return (stdout.indexOf("b ") > -1) ||
(stdout.indexOf("UTF-") > -1) || (stdout.indexOf("ISO8859-") > -1) || (stdout.indexOf("IBM-850") > -1);
}
return false;
});
}
/**
* Re-name USS file or directory
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {string} USSFilePath - contains the current filepath
* @param {string} newFilePath - contains the new filepath
*
* @returns {Promise<Buffer>} Promise that resolves to json information
*
* @throws {ImperativeError}
*/
static renameUSSFile(session, USSFilePath, newFilePath) {
return __awaiter(this, void 0, void 0, function* () {
imperative_1.ImperativeExpect.toNotBeNullOrUndefined(newFilePath, ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message);
const oldFilePath = USSFilePath.charAt(0) === "/" ? USSFilePath : "/" + USSFilePath;
const payload = { request: "move", from: path.posix.normalize(oldFilePath) };
const response = yield Utilities.putUSSPayload(session, newFilePath, payload);
return response;
});
}
}
exports.Utilities = Utilities;
//# sourceMappingURL=Utilities.js.map