Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

Commit

Permalink
Better command-line documentation (closes #2).
Browse files Browse the repository at this point in the history
  • Loading branch information
lextoumbourou committed Jun 8, 2015
1 parent 0a784ca commit f9508a5
Showing 1 changed file with 79 additions and 76 deletions.
155 changes: 79 additions & 76 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"flag"
"fmt"
"github.com/docopt/docopt-go"
"github.com/lextoumbourou/goodhosts"
"os"
)
Expand All @@ -14,105 +14,108 @@ func check(err error) {
}

func main() {
showComments := flag.Bool("all", false, "Show comments when listing.")
usage := `Goodhosts - simple hosts file management.
flag.Parse()
Usage:
goodhosts check <ip> <host>...
goodhosts add <ip> <host>...
goodhosts (rm|remove) <ip> <host>...
goodhosts list [--all]
goodhosts -h | --help
goodhosts --version
args := flag.Args()
Options:
--all Display comments when listing.
-h --help Show this screen.
--version Show the version.`

if len(args) > 0 {
command := args[0]
hosts, err := goodhosts.NewHosts()
check(err)
args, _ := docopt.Parse(usage, nil, true, "Goodhosts 2.0.0", false)

switch command {
case "list":
total := 0
for _, line := range hosts.Lines {
var lineOutput string
hosts, err := goodhosts.NewHosts()
check(err)

if line.IsComment() && !*showComments {
continue
}
if args["list"].(bool) {
total := 0
for _, line := range hosts.Lines {
var lineOutput string

lineOutput = fmt.Sprintf("%s", line.Raw)
if line.Err != nil {
lineOutput = fmt.Sprintf("%s # <<< Malformated!", lineOutput)
}
total += 1
if line.IsComment() && !args["--all"].(bool) {
continue
}

fmt.Println(lineOutput)
lineOutput = fmt.Sprintf("%s", line.Raw)
if line.Err != nil {
lineOutput = fmt.Sprintf("%s # <<< Malformated!", lineOutput)
}
total += 1

fmt.Print("\nTotal: %d\n", total)
fmt.Println(lineOutput)
}

return
case "check":
if len(os.Args) < 3 {
fmt.Println("usage: goodhosts check 127.0.0.1 facebook.com")
os.Exit(1)
}
fmt.Printf("\nTotal: %d\n", total)

ip := os.Args[2]
host := os.Args[3]
return
}

if !hosts.Has(ip, host) {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s %s is not in the hosts file", ip, host))
os.Exit(1)
}
if args["check"].(bool) {
hasErr := false

return
case "add":
if len(os.Args) < 3 {
fmt.Println("usage: goodhosts add 127.0.0.1 facebook.com")
os.Exit(1)
ip := args["<ip>"].(string)
hostEntries := args["<host>"].([]string)

for _, hostEntry := range hostEntries {
if !hosts.Has(ip, hostEntry) {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s %s is not in the hosts file", ip, hostEntry))
hasErr = true
}
}

ip := os.Args[2]
inputHosts := os.Args[3:]
if hasErr {
os.Exit(1)
}

if !hosts.IsWritable() {
fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
os.Exit(1)
}
return
}

err = hosts.Add(ip, inputHosts...)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err.Error()))
os.Exit(2)
}
if args["add"].(bool) {
ip := args["<ip>"].(string)
hostEntries := args["<host>"].([]string)

err = hosts.Flush()
check(err)
if !hosts.IsWritable() {
fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
os.Exit(1)
}

return
case "rm", "remove":
if len(os.Args) < 3 {
fmt.Println("usage: goodhost remove 127.0.0.1 facebook.com")
os.Exit(1)
}
err = hosts.Add(ip, hostEntries...)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err.Error()))
os.Exit(2)
}

ip := os.Args[2]
inputHosts := os.Args[3:]
err = hosts.Flush()
check(err)

if !hosts.IsWritable() {
fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
os.Exit(1)
}
return
}

err = hosts.Remove(ip, inputHosts...)
if err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err.Error()))
os.Exit(2)
}
if args["rm"].(bool) || args["remove"].(bool) {
ip := args["<ip>"].(string)
hostEntries := args["<host>"].([]string)

err = hosts.Flush()
check(err)
if !hosts.IsWritable() {
fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
os.Exit(1)
}

return
err = hosts.Remove(ip, hostEntries...)
if err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err.Error()))
os.Exit(2)
}
}

fmt.Println("Add --help for usage.")
os.Exit(2)
err = hosts.Flush()
check(err)

return
}
}

0 comments on commit f9508a5

Please sign in to comment.