Skip to content

Commit

Permalink
IPv6 support.
Browse files Browse the repository at this point in the history
Preserve order of localhost aliases.
  • Loading branch information
hiteshjasani committed Nov 17, 2014
1 parent 00d2139 commit 05afda6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Will result in

```bash
127.0.0.1 localhost foo
::1 localhost foo
```

Any existing aliases will be preserved, so running the command again
Expand All @@ -53,18 +54,20 @@ with trying to alias `bar` will result in
```bash
% sudo mungehosts -l bar
% cat /etc/hosts
127.0.0.1 localhost bar foo
127.0.0.1 localhost foo bar
::1 localhost foo bar
```

### Add host mapping

To add a mapping for host `foo` at `192.168.0.1`
To add a mapping for host `baz` at `192.168.0.1`

```bash
% sudo mungehosts -a "192.168.0.1 foo"
% sudo mungehosts -a "192.168.0.1 baz"
% cat /etc/hosts
127.0.0.1 localhost
192.168.0.1 foo
::1 localhost
192.168.0.1 baz
```

### Help
Expand Down
30 changes: 16 additions & 14 deletions mungehosts.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import parseopt2, pegs, strutils

proc writeVersion*() = echo "0.1.1"

proc writeHelp() = echo """
Alter /etc/hosts file with a new localhost alias or host mapping.
Expand All @@ -27,13 +29,15 @@ Sample Usage:
Add host alias for localhost
% mungehosts -l kafka
Will change this line in /etc/hosts
Will change these lines in /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
to this line
to this
127.0.0.1 localhost kafka
127.0.0.1 localhost kafka
::1 localhost ip6-localhost ip6-loopback kafka
Add a host mapping
Expand All @@ -44,8 +48,6 @@ Add a host mapping
192.168.0.1 router
"""

proc writeVersion*() = echo "0.1.0"

proc writeHosts*() = echo(readFile("/etc/hosts"))

proc updateFile(lines: openArray[string], addedLines: bool) =
Expand All @@ -59,16 +61,15 @@ proc readHosts(): seq[string] =
result = split(raw, '\L')

proc doAddAlias(alias: string) =
var
lines = readHosts()
index = -1
extra = ""
let
ip4 = peg"{'127.0.0.1' \s+ 'localhost'} {.*}"
ip6 = peg"{'::1' \s+ 'localhost'} {.*}"
var lines = readHosts()
for i in 0.. <lines.len:
if lines[i] =~ peg"'127.0.0.1' \s+ 'localhost' {.*}":
index = i
extra = matches[0]
if index >= 0:
lines[index] = "127.0.0.1 localhost " & alias & extra
if lines[i] =~ ip4:
lines[i] = matches[0] & matches[1] & " " & alias
if lines[i] =~ ip6:
lines[i] = matches[0] & matches[1] & " " & alias
updateFile(lines, false)

proc doAddHost(hostmap: string) =
Expand Down Expand Up @@ -96,6 +97,7 @@ when isMainModule:
of "add-alias", "l": action = addAlias
of "add-host", "a": action = addHost
of "print", "p": writeHosts(); quit()
else: writeHelp(); quit()
of cmdEnd: assert(false) # cannot happen

case action:
Expand Down

0 comments on commit 05afda6

Please sign in to comment.