-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathstart.ts
72 lines (65 loc) · 2.48 KB
/
start.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
import { LeappCommand } from "../../leapp-command";
import { Config } from "@oclif/core/lib/config/config";
import { Session } from "@noovolari/leapp-core/models/session";
import { SessionStatus } from "@noovolari/leapp-core/models/session-status";
import { sessionId } from "../../flags";
export default class StartSession extends LeappCommand {
static description = "Start a session";
static examples = [`$leapp session start`, `$leapp session start --sessionId SESSIONID`];
static flags = {
sessionId,
};
constructor(argv: string[], config: Config) {
super(argv, config);
}
async run(): Promise<void> {
try {
const { flags } = await this.parse(StartSession);
if (flags.sessionId && flags.sessionId !== "") {
const selectedSession = this.cliProviderService.sessionManagementService.getSessionById(flags.sessionId);
if (!selectedSession) {
throw new Error("No session with id " + flags.sessionId + " found");
}
await this.startSession(selectedSession);
} else {
const selectedSession = await this.selectSession();
await this.startSession(selectedSession);
}
} catch (error) {
this.error(error instanceof Error ? error.message : `Unknown error: ${error}`);
}
}
async startSession(session: Session): Promise<void> {
if (session.status === SessionStatus.active) {
throw new Error("session already started");
}
const sessionService = this.cliProviderService.sessionFactory.getSessionService(session.type);
process.on("SIGINT", () => {
sessionService.sessionDeactivated(session.sessionId);
process.exit(0);
});
try {
await sessionService.start(session.sessionId);
this.log("session started");
} finally {
await this.cliProviderService.remoteProceduresClient.refreshSessions();
}
}
async selectSession(): Promise<Session> {
const availableSessions = this.cliProviderService.sessionManagementService
.getSessions()
.filter((session: Session) => session.status === SessionStatus.inactive);
if (availableSessions.length === 0) {
throw new Error("no sessions available");
}
const answer: any = await this.cliProviderService.inquirer.prompt([
{
name: "selectedSession",
message: "select a session",
type: "list",
choices: availableSessions.map((session: any) => ({ name: session.sessionName, value: session })),
},
]);
return answer.selectedSession;
}
}