-
Notifications
You must be signed in to change notification settings - Fork 841
/
fly.js
71 lines (56 loc) · 1.55 KB
/
fly.js
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
'use strict';
const { exec, spawn } = require('child-process-promise');
const { v4: uuidv4 } = require('uuid');
const tmp = require('tmp-promise');
class Fly {
constructor(url, username, password, teamName) {
this.url = url;
this.username = username;
this.password = password;
this.teamName = teamName;
this.target = `wats-target-${uuidv4()}`;
}
static async build(url, username, password, teamName) {
let fly = new Fly(url, username, password, teamName);
await fly.init();
return fly;
}
async setupHome() {
this.home = await tmp.dir({ unsafeCleanup: true });
}
async init() {
await this.setupHome();
await this.loginAs(this.teamName);
}
destroyTeam(teamName) {
return this.run(`destroy-team --team-name ${teamName} --non-interactive`)
}
run(command) {
return this._run(`fly -t ${this.target} ${command}`);
}
spawn(command) {
return this._spawn('fly', ['-t', this.target].concat(command.split(' ')));
}
newTeam(teamName, username) {
return this.run(`set-team --team-name ${teamName} --local-user=${username} --non-interactive`);
}
async cleanup() {
await this.home.cleanup();
}
loginAs(teamName) {
return this.run(`login -c ${this.url} -n ${teamName} -u ${this.username} -p ${this.password}`);
}
_run(command) {
return exec(command, {env: this._env()});
}
_spawn(path, args) {
return spawn(path, args, {env: this._env()});
}
_env() {
return {
"HOME": this.home.path,
"PATH": process.env["PATH"]
}
}
}
module.exports = Fly;