import { IZosFilesResponse } from "../../doc/IZosFilesResponse" ; import { AbstractSession , ImperativeExpect , Headers } from "@zowe/imperative" ; import { Tag } from "./doc/Tag" ; import { ZosFilesMessages } from "../../constants/ZosFiles.messages" ; import { ZosFilesConstants } from "../../constants/ZosFiles.constants" ; import { ZosmfRestClient , IHeaderContent , ZosmfHeaders } from "@zowe/core-for-zowe-sdk" ; import * as path from "path" ; export class Utilities { param { AbstractSession } session param { string } USSFileName param { JSON } payload returns { Promise < Buffer > } throws public static async putUSSPayload ( session , USSFileName , payload ) { ImperativeExpect . toNotBeNullOrUndefined ( USSFileName , ZosFilesMessages . missingUSSFileName . message ) ; ImperativeExpect . toNotBeEqual ( USSFileName , "" , ZosFilesMessages . missingUSSFileName . message ) ; ImperativeExpect . toNotBeNullOrUndefined ( payload , ZosFilesMessages . missingPayload . message ) ; ImperativeExpect . toNotBeEqual ( payload , { } , ZosFilesMessages . missingPayload . message ) ; USSFileName = path . posix . normalize ( USSFileName ) ; if ( USSFileName . substr ( 0 , 1 ) === "/" ) { USSFileName = USSFileName . substr ( 1 ) ; } USSFileName = encodeURIComponent ( USSFileName ) ; const endpoint = path . posix . join ( ZosFilesConstants . RESOURCE , ZosFilesConstants . RES_USS_FILES , USSFileName ) ; const reqHeaders = [ Headers . APPLICATION_JSON , { [ Headers . CONTENT_LENGTH ] : JSON . stringify ( payload ) . length . toString ( ) } , ZosmfHeaders . ACCEPT_ENCODING ] ; const response = await ZosmfRestClient . putExpectBuffer ( session , endpoint , reqHeaders , payload ) ; return response ; } param { AbstractSession } session param { Tag } type param { string } codeset returns { IZosFilesResponse throws public static async chtag ( session , ussFileName , type , codeset ? ) { ImperativeExpect . toNotBeNullOrUndefined ( ussFileName , ZosFilesMessages . missingUSSFileName . message ) ; if ( type === Tag . BINARY ) { ImperativeExpect . toBeEqual ( codeset , undefined , "A codeset cannot be specified for a binary file." ) ; } const payload = { request : "chtag" , action : "set" , type : type . valueOf ( ) } as any ; if ( codeset ) { payload . codeset = codeset ; } await Utilities . putUSSPayload ( session , ussFileName , payload ) ; return { success : true , commandResponse : "File tagged successfully." } ; } param { AbstractSession } session param { string } USSFileName returns { Promise < boolean > } throws public static async isFileTagBinOrAscii ( session , USSFileName ) { const payload = { request : "chtag" , action : "list" } ; const response = await Utilities . putUSSPayload ( session , USSFileName , payload ) ; const jsonObj = JSON . parse ( response . toString ( ) ) ; if ( Object . prototype . hasOwnProperty . call ( jsonObj , "stdout" ) ) { const stdout = JSON . parse ( response . toString ( ) ) . stdout [ 0 ] ; return ( stdout . indexOf ( "b " ) > - 1 ) || ( stdout . indexOf ( "UTF-" ) > - 1 ) || ( stdout . indexOf ( "ISO8859-" ) > - 1 ) || ( stdout . indexOf ( "IBM-850" ) > - 1 ) ; } return false ; } param { AbstractSession } session param { string } USSFilePath param { string } newFilePath returns { Promise < Buffer > } throws public static async renameUSSFile ( session , USSFilePath , newFilePath ) { ImperativeExpect . toNotBeNullOrUndefined ( newFilePath , ZosFilesMessages . missingUSSFileName . message ) ; const oldFilePath = USSFilePath . charAt ( 0 ) === "/" ? USSFilePath : "/" + USSFilePath ; const payload = { request : "move" , from : path . posix . normalize ( oldFilePath ) } ; const response = await Utilities . putUSSPayload ( session , newFilePath , payload ) ; return response ; } }