import { AbstractSession , IHandlerParameters , TextUtils , ITaskWithStatus , TaskStage , ImperativeError } from "@zowe/imperative" ; import { IZosFilesResponse , ZosFilesAttributes , ZosFilesMessages , Upload , IUploadMap } from "@zowe/zos-files-for-zowe-sdk" ; import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler" ; import * as path from "path" ; import * as fs from "fs" ; export export default class DirToUSSDirHandler extends ZosFilesBaseHandler { public async processWithSession ( commandParameters , session ) { const status = { statusMessage : "Uploading all files" , percentComplete : 0 , stageName : TaskStage . IN_PROGRESS } ; let inputDir ; if ( path . isAbsolute ( commandParameters . arguments . inputDir ) ) { inputDir = commandParameters . arguments . inputDir ; } else { inputDir = path . resolve ( commandParameters . arguments . inputDir ) ; } let response ; const attributesFile = this . findAttributesFile ( commandParameters , inputDir ) ; if ( attributesFile ) { response = await this . uploadWithAttributesFile ( attributesFile , response , session , inputDir , commandParameters , status ) ; } else { const filesMap = this . buildFilesMap ( commandParameters ) ; if ( commandParameters . arguments . recursive ) { response = await Upload . dirToUSSDirRecursive ( session , inputDir , commandParameters . arguments . USSDir , { binary : commandParameters . arguments . binary , filesMap , maxConcurrentRequests : commandParameters . arguments . maxConcurrentRequests , task : status , responseTimeout : commandParameters . arguments . responseTimeout } ) ; } else { response = await Upload . dirToUSSDir ( session , inputDir , commandParameters . arguments . USSDir , { binary : commandParameters . arguments . binary , filesMap , maxConcurrentRequests : commandParameters . arguments . maxConcurrentRequests , task : status , responseTimeout : commandParameters . arguments . responseTimeout } ) ; } } const formatMessage = TextUtils . prettyJson ( response . apiResponse ) ; commandParameters . response . console . log ( formatMessage ) ; return response ; } private findAttributesFile ( commandParameters , inputDir ) { let attributesFile ; if ( commandParameters . arguments . attributes ) { if ( ! fs . existsSync ( commandParameters . arguments . attributes ) ) { throw new ImperativeError ( { msg : TextUtils . formatMessage ( ZosFilesMessages . attributesFileNotFound . message , { file : commandParameters . arguments . attributes } ) } ) ; } attributesFile = commandParameters . arguments . attributes ; } else { const localAttributesFile = path . join ( inputDir , ".zosattributes" ) ; if ( fs . existsSync ( localAttributesFile ) ) { attributesFile = localAttributesFile ; } } return attributesFile ; } private async uploadWithAttributesFile ( attributesFile , response , session , inputDir , commandParameters , status ) { let attributesFileContents ; try { attributesFileContents = fs . readFileSync ( attributesFile ) . toString ( ) ; } catch ( err ) { throw new ImperativeError ( { msg : TextUtils . formatMessage ( ZosFilesMessages . errorReadingAttributesFile . message , { file : attributesFile , message : err . message } ) } ) ; } let attributes ; try { attributes = new ZosFilesAttributes ( attributesFileContents , inputDir ) ; } catch ( err ) { throw new ImperativeError ( { msg : TextUtils . formatMessage ( ZosFilesMessages . errorParsingAttributesFile . message , { file : attributesFile , message : err . message } ) } ) ; } if ( commandParameters . arguments . recursive ) { response = await Upload . dirToUSSDirRecursive ( session , inputDir , commandParameters . arguments . USSDir , { attributes , maxConcurrentRequests : commandParameters . arguments . maxConcurrentRequests , task : status , responseTimeout : commandParameters . arguments . responseTimeout } ) ; } else { response = await Upload . dirToUSSDir ( session , inputDir , commandParameters . arguments . USSDir , { attributes , maxConcurrentRequests : commandParameters . arguments . maxConcurrentRequests , task : status , responseTimeout : commandParameters . arguments . responseTimeout } ) ; } return response ; } private buildFilesMap ( commandParameters ) { let filesMap = null ; if ( commandParameters . arguments . binaryFiles ) { filesMap = { binary : true , fileNames : commandParameters . arguments . binaryFiles . split ( "," ) . map ( ( fileName ) => fileName . trim ( ) ) } ; } if ( commandParameters . arguments . asciiFiles ) { filesMap = { binary : false , fileNames : commandParameters . arguments . asciiFiles . split ( "," ) . map ( ( fileName ) => fileName . trim ( ) ) } ; } return filesMap ; } }