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

38
main.go
View File

@ -4,24 +4,43 @@ 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>")
for _, path := range os.Args[1:] { toFile := flag.String("to-file", "", "Save results to <path>")
filepath.Walk(path, func(path string, info os.FileInfo, err error) error { flag.Parse()
filesMap.Add(path, info)
return nil var filesMap filesMap
}) if *fromFile != "" {
fmt.Println(path)
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:] {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
filesMap.Add(path, info)
return nil
})
fmt.Println(path)
}
} }
json, _ := json.MarshalIndent(filesMap.FilesBySize, "", " ") if *toFile != "" && *fromFile == "" {
fmt.Printf("\n\n\n%s\n\n\n", json) json, _ := json.MarshalIndent(filesMap.FilesBySize, "", " ")
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)
} }