Skip to content

Commit e260519

Browse files
committed
Simplified envsubst quite a big and remove many errornous behaviors
1 parent 10b0677 commit e260519

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/job.ts

+16-20
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,25 @@ export class Job {
148148
};
149149
}
150150

151+
private static escape (key: any, val: any) {
152+
if (typeof(val) !== "string") return val;
153+
return val
154+
.replace(/[\\]/g, '\\\\')
155+
.replace(/[\/]/g, '\\/')
156+
.replace(/[\b]/g, '\\b')
157+
.replace(/[\f]/g, '\\f')
158+
.replace(/[\n]/g, '\\n')
159+
.replace(/[\r]/g, '\\r')
160+
.replace(/[\t]/g, '\\t')
161+
.replace(/["]/g, '\\"')
162+
.replace(/\\'/g, "\\'");
163+
}
164+
151165
public async init() {
152166
const allEnvs: { [key: string]: string } = {...this.globals.variables || {}, ...this.variables, ...process.env, ...this.predefinedVariables};
153-
const envs: { [key: string]: string } = {}
154-
155-
let command = 'printenv'
156-
for (let i = 0; i < this.extendsDepth; i++) {
157-
command += ` | envsubst`
158-
}
167+
const command = `echo '${JSON.stringify(allEnvs, Job.escape)}' | envsubst`;
159168
const res = await exec(command, { env: allEnvs });
160-
161-
const keysToSubst = Object.keys({...this.globals.variables || {}, ...this.variables});
162-
for (const line of res.stdout.split("\n")) {
163-
const split = line.split('=');
164-
if (!split[1]) {
165-
continue;
166-
}
167-
if (!keysToSubst.includes(split[0])) {
168-
continue;
169-
}
170-
envs[split[0]] = split[1];
171-
}
172-
173-
this.envs = ({...envs, ...process.env, ...this.predefinedVariables} as any);
169+
this.envs = JSON.parse(res.stdout);
174170

175171
if (!this.rules) {
176172
return;

0 commit comments

Comments
 (0)