Created
July 6, 2016 14:37
-
-
Save jehiah/e565d60fa759a6921c4c3af30f64e922 to your computer and use it in GitHub Desktop.
check ruleset files in HTTPS-Everywhere for duplicate target definitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// This script is designed to check ruleset files in HTTPS-Everywhere for duplicate target definitions | |
import ( | |
"encoding/xml" | |
"flag" | |
"path/filepath" | |
"io/ioutil" | |
"log" | |
) | |
type Target struct { | |
Host string `xml:"host,attr"` | |
} | |
type Ruleset struct { | |
XMLName xml.Name `xml:"ruleset"` | |
Targets []Target `xml:"target"` | |
} | |
func main() { | |
var ( | |
rulesDirectory = flag.String("rules-dir", "src/chrome/content/rules", "path to rules.xml directory") | |
) | |
flag.Parse() | |
existingHosts := make(map[string]string) | |
files, err := filepath.Glob(filepath.Join(*rulesDirectory, "*.xml")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, filename := range files { | |
base := filepath.Base(filename) | |
body, err := ioutil.ReadFile(filename) | |
if err != nil { | |
log.Fatalf("err %s", err) | |
} | |
var r Ruleset | |
err = xml.Unmarshal(body, &r) | |
if err != nil { | |
log.Fatalf("err %s", err) | |
} | |
for _, t := range r.Targets { | |
if existingFile, ok := existingHosts[t.Host]; ok { | |
log.Printf("%s found in %s and %s", t.Host, existingFile, base) | |
} | |
existingHosts[t.Host] = base | |
} | |
} | |
log.Printf("%d hosts checked", len(existingHosts)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment