/
env-var.service.ts
69 lines (62 loc) · 2.15 KB
/
env-var.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
import { Injectable } from '@nestjs/common';
import {
EnvFileInterface,
KeyValueEnvFileInterface,
} from './envFile.interface';
import { DataAccessService } from '../data_access/dataAccess.service';
import { Utils } from '../utils/utils';
import { logger } from '../logger/logger.service';
import { EnvVarAccessor } from './env-var.accessor';
@Injectable()
export class EnvVarService {
constructor(
private readonly dataAccessService: DataAccessService,
private readonly envVarAccessor: EnvVarAccessor,
) {}
encryptDatas(envs: KeyValueEnvFileInterface): KeyValueEnvFileInterface {
for (const [key, value] of Object.entries(envs)) {
const encryptedValue = Utils.encryptValue(value);
envs[key] = encryptedValue;
}
return envs;
}
decryptData(envs: KeyValueEnvFileInterface): KeyValueEnvFileInterface {
for (const [key, value] of Object.entries(envs)) {
const decryptedValue = Utils.decryptValue(value);
envs[key] = decryptedValue;
}
return envs;
}
saveEnvs(
userDirectory: string,
projectName: string,
envs: KeyValueEnvFileInterface,
) {
const path: string = `remote_envs_vars/${userDirectory}/${projectName}/env.yml`;
const encryptEnvs = this.encryptDatas(envs);
this.dataAccessService
.writeEnvsVar(path, encryptEnvs)
.catch(err => logger.error(err, { location: 'saveEnvs' }));
}
async setEnvs(repositoryName: string) {
const path: string = `remote_envs_vars/${repositoryName}/env.yml`;
try {
if (await this.dataAccessService.checkIfEnvVarExist(path)) {
const data: KeyValueEnvFileInterface = await this.dataAccessService.readEnvsVar(
path,
);
const decryptedEnv = this.decryptData(data);
this.envVarAccessor.setAllEnvVar(decryptedEnv);
}
} catch (e) {
logger.error(e, { location: 'setEnvs', project: repositoryName });
}
}
async processEnvFile(userDirectory: string, file: string) {
const envFile: EnvFileInterface = await Utils.parseYAMLFile(file);
envFile.projects.forEach(project => {
// no need await
this.saveEnvs(userDirectory, project.name, project.envs);
});
}
}