Skip to content

Commit

Permalink
Use clearer arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaught committed Sep 15, 2019
1 parent 40febb4 commit 369c3ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ $ unzip /tmp/hosts-override.zip -d /usr/local/bin
```
$client = new-object System.Net.WebClient
$client.DownloadFile("https://github.com/rcaught/hosts-override/releases/latest/download/windows.zip","C:\tmp\hosts-override.zip")
# Then unzip the file and run the bin
# Then unzip the file and run the bin as Administrator
```

## Usage (Mac / Linux)
```
$ sudo hosts-override -0 myhost.com -1 127.0.0.1 # Override myhost.com to point to localhost
$ sudo hosts-override -0 myhost.com -1 google.com # google.com will be resolved into an IP / set of IPs
$ sudo hosts-override -0 myhost.com -1 127.0.0.1 -0 anotherhost.com -1 127.0.0.1 # Multiple hosts and values are supported
$ # Override myhost.com to resolve to 127.0.0.1
$ sudo hosts-override myhost.com,127.0.0.1
$ # google.com will be resolved into an IP / set of IPs
$ sudo hosts-override myhost.com,google.com
$ # Multiple hosts and values are supported
$ sudo hosts-override myhost.com,127.0.0.1 anotherhost.com,127.0.0.1
```

### Notes
- `sudo` is required as the hosts file is owned by `root`
- On exiting the program with an interupt (CTRL-c), the hosts file is cleaned of appended records
- In the case of an unclean shutdown, the next invocation of `hosts-override` will clear the previous sessions records
- IPv4 and IPv6 addresses supported
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,49 @@ import (
"os/signal"
"regexp"
"runtime"
"strings"
"syscall"

"github.com/spf13/cobra"
)

func overrideCmd() *cobra.Command {
var hosts []string
var values []string

rootCmd := &cobra.Command{
Use: "hosts-override",
Short: "Override hosts file entries for the life of the process",
Run: func(cmd *cobra.Command, args []string) {
hosts, values := parseArgs(&args)
hostsFileLocation := hostsFileLocation()
createHostsBackup(hostsFileLocation)
removeOverrides(hostsFileLocation) // Fixes unclean shutdown
parsedOverrides := parsedOverrides(&hosts, &values)
parsedOverrides := parsedOverrides(hosts, values)
parsedOverridesAsHosts := parsedOverridesAsHosts(parsedOverrides)
appendOverrides(hostsFileLocation, parsedOverridesAsHosts)
waitUntilExit()
removeOverrides(hostsFileLocation)
},
}

rootCmd.Flags().StringSliceVarP(&hosts, "host", "0", []string{}, "Host name to override (can be multiple)")
rootCmd.Flags().StringSliceVarP(&values, "value", "1", []string{}, "IP or unresolved host name (can be multiple)")

return rootCmd
}

func main() {
overrideCmd().Execute()
}

func parseArgs(args *[]string) (*[]string, *[]string) {
var hosts []string
var values []string

for _, pair := range *args {
hv := strings.Split(pair, ",")
hosts = append(hosts, hv[0])
values = append(values, hv[1])
}

return &hosts, &values
}

func hostsFileLocation() *string {
var hostsFile string

Expand Down

0 comments on commit 369c3ad

Please sign in to comment.