-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathplugin-environment.ts
90 lines (77 loc) · 3.93 KB
/
plugin-environment.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
import { LoggedEntry, LogLevel } from "../services/log-service";
import { INativeService } from "../interfaces/i-native-service";
import { IOpenExternalUrlService } from "../interfaces/i-open-external-url-service";
import { AwsSessionService } from "../services/session/aws/aws-session-service";
import { Session } from "../models/session";
import { CredentialsInfo } from "../models/credentials-info";
import { IPluginEnvironment } from "./interfaces/i-plugin-environment";
import { PluginLogLevel } from "./plugin-log-level";
import { SessionData } from "./interfaces/session-data";
import { constants } from "../models/constants";
export enum EnvironmentType {
desktopApp = "desktop-app",
cli = "cli",
}
export class PluginEnvironment implements IPluginEnvironment {
private nativeService: INativeService;
private openExternalUrlService: IOpenExternalUrlService;
constructor(private readonly environmentType: EnvironmentType, private readonly providerService: any) {
if (environmentType === EnvironmentType.desktopApp) {
this.nativeService = providerService.appNativeService;
this.openExternalUrlService = providerService.windowService;
} else {
this.nativeService = providerService.cliNativeService;
this.openExternalUrlService = providerService.cliOpenWebConsoleService;
}
}
log(message: string, level: PluginLogLevel, display: boolean): void {
this.providerService.logService.log(new LoggedEntry(message, this, level as unknown as LogLevel, display));
}
fetch(url: string): any {
return this.nativeService.fetch(url);
}
openExternalUrl(loginUrl: string): void {
this.openExternalUrlService.openExternalUrl(loginUrl);
}
getProfileIdByName(profileName: string): string {
return this.providerService.namedProfileService.getProfileIdByName(profileName);
}
getIdpUrlIdByUrl(url: string): string {
return this.providerService.idpUrlService.getIdpUrlIdByUrl(url);
}
async openTerminal(command: string, env?: any): Promise<void> {
let exportedEnvVars = "";
for (const [key, value] of Object.entries(env)) {
exportedEnvVars = exportedEnvVars.concat(`export ${key}=${value};\n`);
}
this.providerService.fileService.writeFileSync(this.nativeService.os.homedir() + "/" + constants.pluginEnvFileDestination, exportedEnvVars);
this.providerService.executeService
.openTerminalFromPlugin(command, env)
.then(() => {
if (this.nativeService.process.platform === "darwin")
this.nativeService.rimraf(this.nativeService.os.homedir() + "/" + constants.pluginEnvFileDestination, {}, () => {});
})
.catch((_) => {
if (this.nativeService.process.platform === "darwin") {
this.nativeService.rimraf(this.nativeService.os.homedir() + "/" + constants.pluginEnvFileDestination, {}, () => {});
}
});
}
async createSession(createSessionData: SessionData): Promise<string> {
const sessionService = this.providerService.sessionFactory.getSessionService(createSessionData.sessionType);
return await sessionService.create(createSessionData.getCreationRequest());
}
async cloneSession(session: Session): Promise<string> {
const sessionService = this.providerService.sessionFactory.getSessionService(session.type);
const createSessionData = await sessionService.getCloneRequest(session);
return await sessionService.create(createSessionData);
}
async updateSession(updateSessionData: SessionData, session: Session): Promise<void> {
const sessionService = this.providerService.sessionFactory.getSessionService(session.type);
return await sessionService.update(session.sessionId, updateSessionData.getCreationRequest());
}
private async generateCredentials(session: Session): Promise<CredentialsInfo> {
const sessionService = this.providerService.sessionFactory.getSessionService(session.type) as unknown as AwsSessionService;
return await sessionService.generateCredentials(session.sessionId);
}
}