Add flags for loading and saving to json file

This commit is contained in:
Jan Bader 2020-11-21 20:15:10 +01:00
parent 14b7835f72
commit d09c6858b6

24
main.go
View File

@ -4,14 +4,30 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
func main() { func main() {
filesMap := newFilesMap() fromFile := flag.String("from-file", "", "Load results file from <path>")
toFile := flag.String("to-file", "", "Save results to <path>")
flag.Parse()
var filesMap filesMap
if *fromFile != "" {
byteValue, _ := ioutil.ReadFile(*fromFile)
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &filesMap)
} else {
filesMap = newFilesMap()
for _, path := range os.Args[1:] { for _, path := range os.Args[1:] {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error { filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
filesMap.Add(path, info) filesMap.Add(path, info)
@ -19,9 +35,12 @@ func main() {
}) })
fmt.Println(path) fmt.Println(path)
} }
}
if *toFile != "" && *fromFile == "" {
json, _ := json.MarshalIndent(filesMap.FilesBySize, "", " ") json, _ := json.MarshalIndent(filesMap.FilesBySize, "", " ")
fmt.Printf("\n\n\n%s\n\n\n", json) ioutil.WriteFile(*toFile, json, 644)
}
} }
type filesMap struct { type filesMap struct {
@ -64,6 +83,7 @@ func (fm *filesMap) Add(path string, info os.FileInfo) error {
return err2 return err2
} }
// for later files always append by hash
return appendByFileHash(filesByHash, fileInfo) return appendByFileHash(filesByHash, fileInfo)
} }