Skip to content

Commit 80f49d9

Browse files
Added support for escaping Golang special characters (#13)
* Added support for escaping Golang special characters * Helm command readability fixes * Upgrade eslint parser
1 parent 21f4353 commit 80f49d9

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"devDependencies": {
77
"@types/node": "^24.0.1",
88
"@typescript-eslint/eslint-plugin": "^8.34.0",
9-
"@typescript-eslint/parser": "^8.33.1",
9+
"@typescript-eslint/parser": "^8.34.0",
1010
"eslint": "^9.28.0",
1111
"eslint-config-prettier": "^10.1.5",
1212
"eslint-plugin-prettier": "^5.4.1",

src/main.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ const helmSecretVariableName = process.env.HELM_SECRET_VARIABLE_NAME;
1818
const helmEnvVarVariableName = process.env.HELM_ENV_VAR_VARIABLE_NAME;
1919
const tag = process.env.TAG;
2020

21-
let helmDeploymentCommand = `helm --kubeconfig .kubeConfig --kube-context ${environment} upgrade ${name} ${dryRun ? '\\\n ' : ''}--install ${dryRun ? '\\\n ' : ''}--create-namespace ${dryRun ? '\\\n ' : ''}--namespace ${namespace} ${dryRun ? '\\\n ' : ''}--set image.tag=${tag} ${dryRun ? '\\\n ' : ''}`;
21+
let helmDeploymentCommand = `helm upgrade ${name} \\
22+
--install \\
23+
--create-namespace \\
24+
--namespace ${namespace} \\
25+
--set image.tag=${tag} \\
26+
`;
2227

2328
if (dryRun) {
2429
console.log('Executing Dry Run...');
@@ -66,31 +71,38 @@ Object.keys(variables).forEach((variableName) => {
6671
}
6772
});
6873

69-
Object.keys(environmentVariables).forEach((environmentVariableName) => helmEnvVars.push({ name: environmentVariableName, value: environmentVariables[environmentVariableName].replace(',', '\\,') }));
74+
Object.keys(environmentVariables).forEach((environmentVariableName) => helmEnvVars.push({ name: environmentVariableName, value: environmentVariables[environmentVariableName] }));
75+
76+
// We need to escape all the Go sequences.
77+
// Reference - https://stackoverflow.com/a/63636047/4546963
7078

7179
helmSecrets.forEach((dto, idx) => {
72-
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmSecretVariableName}[${idx}].key='${dto.key}' ${dryRun ? '\\\n ' : ''}`);
73-
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmSecretVariableName}[${idx}].value='${dryRun ? '<REDACTED>' : dto.value}' ${dryRun ? '\\\n ' : ''}`);
80+
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmSecretVariableName}[${idx}].key='${dto.key}' \\\n `);
81+
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmSecretVariableName}[${idx}].value='${dryRun ? '<REDACTED>' : escapeGoSpecialChars(dto.value)}' \\\n `);
7482
});
7583

7684
helmConfigMaps.forEach((dto, idx) => {
77-
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmConfigMapVariableName}[${idx}].key='${dto.key}' ${dryRun ? '\\\n ' : ''}`);
78-
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmConfigMapVariableName}[${idx}].value='${dryRun ? '<REDACTED>' : dto.value}' ${dryRun ? '\\\n ' : ''}`);
85+
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmConfigMapVariableName}[${idx}].key='${dto.key}' \\\n `);
86+
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmConfigMapVariableName}[${idx}].value='${dryRun ? '<REDACTED>' : escapeGoSpecialChars(dto.value)}' \\\n `);
7987
});
8088

8189
helmEnvVars.forEach((dto, idx) => {
82-
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmEnvVarVariableName}[${idx}].name='${dto.name}' ${dryRun ? '\\\n ' : ''}`);
83-
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmEnvVarVariableName}[${idx}].value='${dto.value}' ${dryRun ? '\\\n ' : ''}`);
90+
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmEnvVarVariableName}[${idx}].name='${dto.name}' \\\n `);
91+
helmDeploymentCommand = helmDeploymentCommand.concat(`--set ${helmEnvVarVariableName}[${idx}].value='${dryRun ? '<REDACTED>' : escapeGoSpecialChars(dto.value)}' \\\n `);
8492
});
8593

8694
if (helmChartVersion) {
87-
helmDeploymentCommand = helmDeploymentCommand.concat(`--version ${helmChartVersion} ${dryRun ? '\\\n ' : ''}`);
95+
helmDeploymentCommand = helmDeploymentCommand.concat(`--version ${helmChartVersion} \\\n `);
8896
}
8997

9098
helmDeploymentCommand = helmDeploymentCommand.concat(helmChartUrl);
9199

92100
if (dryRun) {
93-
console.log(`This is a Dry Run. Install command to be run: ${helmDeploymentCommand}`);
101+
console.log(`This is a Dry Run. Install command to be run: \n${helmDeploymentCommand}`);
94102
} else {
95-
execSync(helmDeploymentCommand);
103+
console.log(execSync(helmDeploymentCommand).toString('utf-8'));
104+
}
105+
106+
function escapeGoSpecialChars(value: string): string {
107+
return value.split(',').join('\\,').split('.').join('\\.').split('{').join('\\{').split('[').join('\\[').split(']').join('\\]').split('}').join('\\}');
96108
}

0 commit comments

Comments
 (0)