Skip to content

Commit

Permalink
handle clean hosts file for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
LouWii committed Sep 19, 2021
1 parent 32b7281 commit c7960fd
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/main/src/hosts-file-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,22 @@ export const saveToFile = function(serializedHosts: string): Promise<boolean> {

let command = '';
if (RESULT_STATE_CLEAN === resultState) {
command = `(echo ${hostsDelimiterStart}
if ('win32' === process.platform) {
command = `(echo ${hostsDelimiterStart}
${assembleLines(hosts, '\n', 'echo ')}
echo ${hostsDelimiterEnd}) >> ${getHostsFilePath()}`;
} else if ('linux' === process.platform) {
// Using printf as it has a more consistent behavior compared to echo
command = `printf '${hostsDelimiterStart}\n${assembleLines(hosts, '\n')}${hostsDelimiterEnd}' >> ${getHostsFilePath()}`;
}
} else {
if ('win32' === process.platform) {
const replacement = '\\"`${1}' + assembleLines(hosts, '`r`n') + '`${3}\\"';
command = `powershell -command "(Get-Content -Raw ${getHostsFilePath()}) | Foreach-Object {$_ -replace '(${hostsDelimiterStart}\\r\\n)([\\s\\S]*)(${hostsDelimiterEnd}\\r\\n)', ${replacement}} | Set-Content ${getHostsFilePath()}`;
} else if ('linux' === process.platform) {
// sed is hard to use with new line char in regex
// awk?
// perl?
}
}

Expand Down

0 comments on commit c7960fd

Please sign in to comment.