-
Notifications
You must be signed in to change notification settings - Fork 85
/
SshBaseHandler.ts
126 lines (110 loc) · 3.66 KB
/
SshBaseHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* 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.
*
*/
import {
// AbstractSession,
ICommandArguments,
ICommandHandler,
IHandlerParameters,
IProfile,
IHandlerResponseConsoleApi,
IHandlerFormatOutputApi,
IHandlerResponseDataApi,
IHandlerProgressApi,
IImperativeError,
ImperativeError,
ConnectionPropsForSessCfg
} from "@zowe/imperative";
import { SshSession } from "./SshSession";
import { ISshSession } from "./doc/ISshSession";
/**
* This class is used by the various handlers in the project as the base class for their implementation.
*/
export abstract class SshBaseHandler implements ICommandHandler {
/**
* The session creating from the command line arguments / profile
*/
protected mSession: SshSession;
/**
* Loaded z/OS SSH profile if needed
*/
protected mSshProfile: IProfile;
/**
* Command line arguments passed
*/
protected mArguments: ICommandArguments;
/**
* Full set of command handler parameters from imperative
*/
protected mHandlerParams: IHandlerParameters;
/**
* This will grab the arguments and create a session before calling the subclass
* {@link SshBaseHandler#processWithSession} method.
*
* @param {IHandlerParameters} commandParameters Command parameters sent by imperative.
*
* @returns {Promise<void>}
*/
public async process(commandParameters: IHandlerParameters) {
this.mHandlerParams = commandParameters;
this.mSshProfile = commandParameters.profiles.get("ssh", false);
const sshSessCfg: ISshSession = SshSession.createSshSessCfgFromArgs(commandParameters.arguments);
const sshSessCfgWithCreds = await ConnectionPropsForSessCfg.addPropsOrPrompt<ISshSession>(
sshSessCfg, commandParameters.arguments
);
this.mSession = new SshSession(sshSessCfgWithCreds);
this.mArguments = commandParameters.arguments;
await this.processCmd(commandParameters);
}
/**
* Fail the command with an imperative error
* @param {IImperativeError} err - the imperative error parameters
*/
public fail(err: IImperativeError) {
throw new ImperativeError(err);
}
/**
* Returns the console interface for the command handler
* @returns {IHandlerResponseConsoleApi}
*/
public get console(): IHandlerResponseConsoleApi {
return this.mHandlerParams.response.console;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerFormatOutputApi}
*/
public get format(): IHandlerFormatOutputApi {
return this.mHandlerParams.response.format;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerResponseDataApi}
*/
public get data(): IHandlerResponseDataApi {
return this.mHandlerParams.response.data;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerProgressApi}
*/
public get progress(): IHandlerProgressApi {
return this.mHandlerParams.response.progress;
}
/**
* This is called by the {@link SshBaseHandler#process} after it creates a session.
*
* @param {IHandlerParameters} commandParameters Command parameters sent to the handler.
*
*/
public abstract async processCmd(
commandParameters: IHandlerParameters
): Promise<void>;
}