-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpull-service.ts
157 lines (130 loc) · 3.97 KB
/
pull-service.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import chalk from 'chalk'
import axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
import {vars} from '../vars'
import {existsSync, renameSync, writeFileSync} from 'fs'
import {CliUx} from '@oclif/core'
import {LogService} from '../services/log-service'
import {AbortService} from '../services/abort-service'
import {LoginService} from '../services/login-service'
interface PullServiceAttrs {
cmd;
environment;
filename;
dotenvMe;
yes;
}
class PullService {
public cmd;
public environment;
public filename;
public dotenvMe;
public yes;
public log;
public abort;
public login;
constructor(attrs: PullServiceAttrs = {} as PullServiceAttrs) {
this.cmd = attrs.cmd
this.environment = attrs.environment
this.filename = attrs.filename
this.dotenvMe = attrs.dotenvMe
this.yes = attrs.yes
this.log = new LogService({cmd: attrs.cmd})
this.abort = new AbortService({cmd: attrs.cmd})
this.login = new LoginService({cmd: attrs.cmd, dotenvMe: null, yes: this.yes})
}
async run(): Promise<void> {
if (vars.missingEnvVault) {
this.abort.missingEnvVault()
}
if (vars.emptyEnvVault) {
this.abort.emptyEnvVault()
}
// special case for pulling example - no auth needed
if (this.pullingExample) {
await this.pull()
return
}
if (vars.missingEnvMe(this.dotenvMe)) {
await this.login.login(false)
}
if (vars.emptyEnvMe(this.dotenvMe)) {
await this.login.login(false)
}
let pullingMsg = 'Securely pulling'
if (this.environment) {
pullingMsg = `Securely pulling ${this.environment}`
}
CliUx.ux.action.start(`${chalk.dim(this.log.pretextRemote)}${pullingMsg}`)
await this.pull()
}
async pull(): Promise<void> {
const options: AxiosRequestConfig = {
method: 'POST',
headers: {'content-type': 'application/json'},
data: {
environment: this.environment,
DOTENV_VAULT: vars.vaultValue,
DOTENV_ME: this.meUid,
},
url: this.url,
}
try {
const resp: AxiosResponse = await axios(options)
const environment = resp.data.data.environment
const envName = resp.data.data.envName
const newData = resp.data.data.dotenv
const newVaultData = resp.data.data.dotenvVault
const outputFilename = this.displayFilename(envName)
CliUx.ux.action.stop()
// backup current file to .previous
if (existsSync(outputFilename)) {
renameSync(outputFilename, `${outputFilename}.previous`)
}
// write to new current file
writeFileSync(outputFilename, newData)
this.log.remote(`Securely pulled ${environment} (${outputFilename})`)
// write .env.vault file
if (newVaultData) {
writeFileSync('.env.vault', newVaultData)
this.log.remote('Securely built vault (.env.vault)')
}
} catch (error) {
CliUx.ux.action.stop('aborting')
let errorMessage = null
let errorCode = 'PULL_ERROR'
let suggestions = []
errorMessage = error
if (error.response) {
errorMessage = error.response.data
if (error.response.data && error.response.data.errors && error.response.data.errors[0]) {
const error1 = error.response.data.errors[0]
errorMessage = error1.message
if (error1.code) {
errorCode = error1.code
}
if (error1.suggestions) {
suggestions = error1.suggestions
}
}
}
this.abort.error(errorMessage, {code: errorCode, ref: '', suggestions: suggestions})
}
}
get url(): string {
return vars.apiUrl + '/pull'
}
get meUid(): any {
return this.dotenvMe || vars.meValue
}
get pullingExample(): boolean {
return this.environment === 'example'
}
displayFilename(envName: string): string {
// if user has set a filename for output then use that else use envName
if (this.filename) {
return this.filename
}
return envName
}
}
export {PullService}