Jelajahi Sumber

adding ignore file list for reporting

Wilfried Klaas 5 tahun lalu
induk
melakukan
3815083dfa
1 mengubah file dengan 36 tambahan dan 6 penghapusan
  1. 36 6
      GoHash.go

+ 36 - 6
GoHash.go

@@ -13,6 +13,7 @@ import (
 	"path/filepath"
 	"runtime"
 	"sort"
+	"strings"
 	"sync"
 	"time"
 
@@ -30,7 +31,7 @@ type Fdhashes struct {
 }
 
 var hashes map[string]Fdhashes
-
+var ignoreLines []string
 var mu sync.RWMutex
 var driveLetter string
 
@@ -38,19 +39,37 @@ var rewrite bool
 var prune bool
 var outputJson bool
 var report string
+var ignores string
 
 func init() {
 	flag.BoolVarP(&rewrite, "rewrite", "r", false, "rewrite all fhhashes files.")
 	flag.StringVarP(&report, "equals", "e", "", "compare all file hashes and writing a equlatity report.")
 	flag.BoolVarP(&prune, "prune", "p", false, "checking all fdhashes files.")
 	flag.BoolVarP(&outputJson, "json", "j", false, "output as json.")
+	flag.StringVarP(&ignores, "ignores", "i", "", "list of files to ignore in report.")
 }
 
 func main() {
 	log.Println("starting GoHash")
+	ignoreLines = make([]string, 0)
 	hashes = make(map[string]Fdhashes)
 	flag.Parse()
 	myFile := flag.Arg(0)
+	if rewrite {
+		log.Println("rewrite active")
+	}
+	if prune {
+		log.Println("prune active")
+	}
+	if outputJson {
+		log.Println("output json format active")
+	}
+	if report != "" {
+		log.Println("report active, file: ", report)
+	}
+	if ignores != "" {
+		log.Println("ignores file: ", ignores)
+	}
 	file, err := os.Stat(myFile)
 	if os.IsNotExist(err) {
 		log.Fatalln("File does not exists:", myFile)
@@ -269,8 +288,8 @@ func loadHashfile(fileStr string) Fdhashes {
 }
 
 func compareFolder(folder string) {
+	loadIgnoreFile(ignores)
 	loadAllHashFiles(folder)
-
 	// putting all hashes into one big map key = hash, value list of files with that hash
 	size := len(hashes)
 	index := make(map[string][]string)
@@ -286,10 +305,13 @@ func compareFolder(folder string) {
 				values = make([]string, 0)
 			}
 			filepath := fmt.Sprintf("%s/%s", hashFile.Path, filename)
-			_, err := os.Stat(filepath)
-			if err == nil {
-				values = append(values, filepath)
-				index[hash] = values
+			pos := sort.SearchStrings(ignoreLines, filepath)
+			if pos == len(ignoreLines) {
+				_, err := os.Stat(filepath)
+				if err == nil {
+					values = append(values, filepath)
+					index[hash] = values
+				}
 			}
 		}
 	}
@@ -382,6 +404,14 @@ func compareFolder(folder string) {
 	}
 }
 
+func loadIgnoreFile(filename string) {
+	content, err := ioutil.ReadFile(filename)
+	if err == nil {
+		ignoreLines = strings.Split(string(content), "\n")
+		sort.Strings(ignoreLines)
+	}
+}
+
 func search(srcHash string, exFilename string, exFilepath string) (value string, found bool) {
 	for _, hashFile := range hashes {
 		for filename, hash := range hashFile.Hashes {