Simple hosts file management in Go (golang).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

148 lines
2.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/ChrisWiegman/goodhosts/v3"
  6. "github.com/docopt/docopt-go"
  7. )
  8. func check(err error) {
  9. if err != nil {
  10. panic(err)
  11. }
  12. }
  13. func main() {
  14. usage := `Goodhosts - simple hosts file management.
  15. Usage:
  16. goodhosts check <ip> <host>...
  17. goodhosts add <ip> <host>...
  18. goodhosts (rm|remove) <ip> <host>...
  19. goodhosts list [--all]
  20. goodhosts removesection <section>
  21. goodhosts -h | --help
  22. goodhosts --version
  23. Options:
  24. --all Display comments when listing.
  25. -h --help Show this screen.
  26. --version Show the version.`
  27. args, _ := docopt.Parse(usage, nil, true, "Goodhosts 3.2.0", false)
  28. hosts, err := goodhosts.NewHosts("")
  29. check(err)
  30. if args["list"].(bool) {
  31. total := 0
  32. for _, line := range hosts.FileLines {
  33. var lineOutput string
  34. if goodhosts.IsComment(line.Raw) && !args["--all"].(bool) {
  35. continue
  36. }
  37. lineOutput = fmt.Sprintf("%s", line.Raw)
  38. if line.Err != nil {
  39. lineOutput = fmt.Sprintf("%s # <<< Malformated!", lineOutput)
  40. }
  41. total++
  42. fmt.Println(lineOutput)
  43. }
  44. fmt.Printf("\nTotal: %d\n", total)
  45. return
  46. }
  47. if args["check"].(bool) {
  48. hasErr := false
  49. ip := args["<ip>"].(string)
  50. hostEntries := args["<host>"].([]string)
  51. for _, hostEntry := range hostEntries {
  52. if !hosts.Has(ip, hostEntry, false) {
  53. fmt.Fprintln(os.Stderr, fmt.Sprintf("%s %s is not in the hosts file", ip, hostEntry))
  54. hasErr = true
  55. }
  56. }
  57. if hasErr {
  58. os.Exit(1)
  59. }
  60. return
  61. }
  62. if args["add"].(bool) {
  63. ip := args["<ip>"].(string)
  64. hostEntries := args["<host>"].([]string)
  65. if !hosts.IsWritable() {
  66. fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
  67. os.Exit(1)
  68. }
  69. err = hosts.Add(ip, "Test Comment", hostEntries...)
  70. if err != nil {
  71. fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err.Error()))
  72. os.Exit(2)
  73. }
  74. err = hosts.Flush()
  75. check(err)
  76. return
  77. }
  78. if args["rm"].(bool) || args["remove"].(bool) {
  79. ip := args["<ip>"].(string)
  80. hostEntries := args["<host>"].([]string)
  81. if !hosts.IsWritable() {
  82. fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
  83. os.Exit(1)
  84. }
  85. err = hosts.Remove(ip, hostEntries...)
  86. if err != nil {
  87. fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err.Error()))
  88. os.Exit(2)
  89. }
  90. err = hosts.Flush()
  91. check(err)
  92. return
  93. }
  94. if args["removesection"].(bool) {
  95. section := args["<section>"].(string)
  96. hosts, err := goodhosts.NewHosts(section)
  97. check(err)
  98. if !hosts.IsWritable() {
  99. fmt.Fprintln(os.Stderr, "Host file not writable. Try running with elevated privileges.")
  100. os.Exit(1)
  101. }
  102. err = hosts.RemoveSection()
  103. if err != nil {
  104. fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err.Error()))
  105. os.Exit(2)
  106. }
  107. err = hosts.Flush()
  108. check(err)
  109. return
  110. }
  111. }