Skip to content

Instantly share code, notes, and snippets.

@brettcannon
Created July 11, 2014 17:34
Show Gist options
  • Save brettcannon/a2a37cc5aadbc91c02ad to your computer and use it in GitHub Desktop.
Save brettcannon/a2a37cc5aadbc91c02ad to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"flag"
"log"
"os"
"sort"
"strings"
)
func readFile(path string, values map[string]struct{}) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
values[strings.ToLower(scanner.Text())] = struct{}{}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func writeFile(values map[string]struct{}, path string) error {
domains := make([]string, 0)
for d, _ := range values {
domains = append(domains, d)
}
sort.Strings(domains)
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
if _, err := file.WriteString(strings.Join(domains, "\n")); err != nil {
return err
}
return nil
}
func main() {
flag.Parse()
values := make(map[string]struct{})
for _, path := range flag.Args() {
if err := readFile(path, values); err != nil {
log.Fatal(err)
}
}
if err := writeFile(values, "whitelist.txt"); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment