import { IHandlerParameters , ImperativeError , ITaskWithStatus , TaskProgress , TaskStage } from "@zowe/imperative" ; import { isNullOrUndefined } from "util" ; import * as fs from "fs" ; import { ISubmitParms , SubmitJobs , IJob , ISpoolFile } from "@zowe/zos-jobs-for-zowe-sdk" ; import { IDownloadOptions , Get } from "@zowe/zos-files-for-zowe-sdk" ; import { ZosmfBaseHandler } from "@zowe/zosmf-for-zowe-sdk" ; import getstdin = require ( "get-stdin" ) ; export class implements ICommandHandler export default class SharedSubmitHandler extends ZosmfBaseHandler { param { IHandlerParameters } params returns { Promise < void > } memberof public async processCmd ( params ) { const status = { statusMessage : "Submitting job" , percentComplete : TaskProgress . TEN_PERCENT , stageName : TaskStage . IN_PROGRESS } ; const parms = { jclSource : undefined , viewAllSpoolContent : this . mArguments . viewAllSpoolContent , directory : this . mArguments . directory , extension : this . mArguments . extension , volume : this . mArguments . volume , waitForActive : this . mArguments . waitForActive , waitForOutput : this . mArguments . waitForOutput , task : status , jclSymbols : this . mArguments . jclSymbols } ; const options = { } ; params . response . progress . startBar ( { task : status } ) ; let sourceType ; if ( this . mArguments . dataset ) { sourceType = "dataset" ; } else if ( this . mArguments . localFile ) { sourceType = "local-file" ; } else if ( params . definition . name === "stdin" ) { sourceType = "stdin" ; } let response ; let apiObj ; let spoolFilesResponse ; let source ; let directory = this . mArguments . directory ; switch ( sourceType ) { case "dataset" : if ( parms . volume ) { options . volume = parms . volume ; const getJcl = await Get . dataSet ( this . mSession , this . mArguments . dataset , options ) ; source = this . mArguments . dataset ; apiObj = await SubmitJobs . submitJclString ( this . mSession , getJcl . toString ( ) , parms ) ; if ( parms . viewAllSpoolContent ) { spoolFilesResponse = apiObj ; } break ; } else { response = await SubmitJobs . submitJobCommon ( this . mSession , { jobDataSet : this . mArguments . dataset , jclSymbols : this . mArguments . jclSymbols } ) ; apiObj = await SubmitJobs . checkSubmitOptions ( this . mSession , parms , response ) ; source = this . mArguments . dataset ; if ( parms . viewAllSpoolContent ) { spoolFilesResponse = apiObj ; } } break ; case "local-file" : { parms . jclSource = this . mArguments . localFile ; const JclString = fs . readFileSync ( this . mArguments . localFile ) . toString ( ) ; apiObj = await SubmitJobs . submitJclString ( this . mSession , JclString , parms ) ; source = this . mArguments . localFile ; if ( parms . viewAllSpoolContent ) { spoolFilesResponse = apiObj ; } break ; } case "stdin" : { const Jcl = await getstdin ( ) ; apiObj = await SubmitJobs . submitJclString ( this . mSession , Jcl , parms ) ; source = "stdin" ; if ( parms . viewAllSpoolContent ) { spoolFilesResponse = apiObj ; } break ; } default : throw new ImperativeError ( { msg : `Internal submit error: Unable to determine the JCL source. ` + `Please contact support.` , additionalDetails : JSON . stringify ( params ) } ) ; } if ( isNullOrUndefined ( spoolFilesResponse ) ) { params . response . format . output ( { fields : [ "jobid" , "retcode" , "jobname" , "status" ] , output : apiObj , format : "object" } ) ; this . data . setObj ( apiObj ) ; } else { for ( const spoolFile of spoolFilesResponse ) { if ( ! isNullOrUndefined ( spoolFile . procName ) && spoolFile . procName . length > 0 ) { this . console . log ( "Spool file: %s (ID #%d, Step: %s, ProcStep: %s)" , spoolFile . ddName , spoolFile . id , spoolFile . stepName , spoolFile . procName ) ; } else { this . console . log ( "Spool file: %s (ID #%d, Step: %s)" , spoolFile . ddName , spoolFile . id , spoolFile . stepName ) ; } this . console . log ( spoolFile . data ) ; } this . data . setObj ( spoolFilesResponse ) ; } if ( ! isNullOrUndefined ( directory ) && isNullOrUndefined ( spoolFilesResponse ) ) { directory = directory . includes ( "./" ) ? directory : `./${ directory }` ; params . response . console . log ( `Successfully downloaded output to ${ directory }/${ apiObj . jobid }` ) ; } params . response . progress . endBar ( ) ; this . data . setMessage ( `Submitted JCL contained in "${ sourceType }": "${ source }"` ) ; } }