-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate ps1 code to update hosts file
- Loading branch information
Showing
17 changed files
with
271 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/jianglibo/vaadin/dashboard/util/ProcessRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.jianglibo.vaadin.dashboard.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.ProcessBuilder.Redirect; | ||
import java.util.Map; | ||
|
||
public class ProcessRunner { | ||
|
||
public static Process build() throws IOException { | ||
ProcessBuilder pb = | ||
new ProcessBuilder("myCommand", "myArg1", "myArg2"); | ||
Map<String, String> env = pb.environment(); | ||
env.put("VAR1", "myValue"); | ||
env.remove("OTHERVAR"); | ||
env.put("VAR2", env.get("VAR1") + "suffix"); | ||
pb.directory(new File("myDir")); | ||
File log = new File("log"); | ||
pb.redirectErrorStream(true); | ||
pb.redirectOutput(Redirect.appendTo(log)); | ||
Process p = pb.start(); | ||
return p; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
Param( | ||
[string]$hostfile, | ||
[switch]$delete, | ||
[string]$items, | ||
[bool]$writeback=$true | ||
) | ||
|
||
# ---insert-items-here--- | ||
|
||
function Update-HostsItems { | ||
Param( | ||
[string]$hostfile, | ||
[switch]$delete, | ||
[string]$items, | ||
[bool]$writeback=$true) | ||
if (!$hostfile) { | ||
$hostfile = $env:windir | Join-Path -ChildPath "System32\drivers\etc\hosts" | ||
} | ||
|
||
[array]$lines = Get-Content $hostfile | ForEach-Object {$_.ToString().trim()} | ||
|
||
[array]$items = $items -split "," | ForEach-Object {$_.ToString().Trim()} | ||
|
||
foreach ($item in $items) { | ||
[array]$pair = $item -split "\s+" | ||
if ($delete) { | ||
$lines = $lines | Where-Object {$_ -notmatch $pair[0].ToString().Trim()} | ||
} else { | ||
if ($pair.Count -eq 2) { | ||
$lines = $lines | Where-Object {$_ -notmatch $pair[0].ToString().Trim()} | ||
} | ||
} | ||
} | ||
if ($writeback) { | ||
if ($delete) { | ||
$lines | Out-File $hostfile -Encoding ascii | ||
} else { | ||
$lines + $items | Out-File $hostfile -Encoding ascii | ||
} | ||
} else { | ||
if ($delete) { | ||
$lines | ||
} else { | ||
$lines + $items | ||
} | ||
} | ||
} | ||
if ($items) { | ||
if ($delete) { | ||
Update-HostsItems -hostfile $hostfile -items $items -writeback $writeback -delete | ||
} else { | ||
Update-HostsItems -hostfile $hostfile -items $items -writeback $writeback | ||
} | ||
} |
Oops, something went wrong.