Added JSON output format

This commit is contained in:
DataHoarder 2021-12-07 16:10:46 +01:00
parent d679f9a541
commit b847a14bce

View file

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/hex" "encoding/hex"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/minio/md5-simd" "github.com/minio/md5-simd"
@ -15,18 +16,18 @@ import (
) )
type HashFileResult struct { type HashFileResult struct {
error error Error error
path string Path string
sha256 string SHA256 string
md5 string MD5 string
} }
func HashFile(results chan<- HashFileResult, md5hasher *md5simd.Hasher, sha256hasher *hash.Hash, path string) { func HashFile(results chan<- HashFileResult, md5hasher *md5simd.Hasher, sha256hasher *hash.Hash, path string) {
fh, err := os.Open(path) fh, err := os.Open(path)
if err != nil { if err != nil {
results <- HashFileResult{ results <- HashFileResult{
error: err, Error: err,
path: path, Path: path,
} }
return return
} }
@ -35,18 +36,35 @@ func HashFile(results chan<- HashFileResult, md5hasher *md5simd.Hasher, sha256ha
io.Copy(io.MultiWriter(*sha256hasher, *md5hasher), fh) io.Copy(io.MultiWriter(*sha256hasher, *md5hasher), fh)
results <- HashFileResult{ results <- HashFileResult{
error: nil, Error: nil,
path: path, Path: path,
sha256: hex.EncodeToString((*sha256hasher).Sum(nil)), SHA256: hex.EncodeToString((*sha256hasher).Sum(nil)),
md5: hex.EncodeToString((*md5hasher).Sum(nil)), MD5: hex.EncodeToString((*md5hasher).Sum(nil)),
} }
} }
func PrintHashFileResult(result *HashFileResult) { func PrintHashFileResult(result *HashFileResult, format string) {
if result.error != nil { switch {
fmt.Fprintln(os.Stderr, result.path, "error: ", result.error) case format == "json":
} else { var jsonData []byte
fmt.Println(result.sha256, result.md5, result.path) jsonData, err := json.Marshal(*result)
if err != nil {
fmt.Fprintln(os.Stderr, result.Path, "Error: ", result.Error)
} else {
if result.Error != nil {
fmt.Fprintln(os.Stderr, string(jsonData))
} else {
fmt.Println(string(jsonData))
}
}
case format == "text":
if result.Error != nil {
fmt.Fprintln(os.Stderr, result.Path, "Error: ", result.Error)
} else {
fmt.Println(result.SHA256, result.MD5, result.Path)
}
} }
} }
@ -59,6 +77,8 @@ func main() {
return result return result
}(), "Maximum number of concurrent hashing tasks. Change to avoid fdlimit issues. Defaults to number of min(128, CPU cores * 16)") }(), "Maximum number of concurrent hashing tasks. Change to avoid fdlimit issues. Defaults to number of min(128, CPU cores * 16)")
outputFormat := flag.String("format", "text", "Output formats. Allowed: text, json")
flag.Parse() flag.Parse()
var taskCount int64 var taskCount int64
@ -108,13 +128,13 @@ func main() {
//Already print before finishing, use atomic ints instead of a WaitGroup //Already print before finishing, use atomic ints instead of a WaitGroup
for atomic.LoadInt64(&taskCount) > 0 { for atomic.LoadInt64(&taskCount) > 0 {
result := <-resultChannel result := <-resultChannel
PrintHashFileResult(&result) PrintHashFileResult(&result, *outputFormat)
} }
close(resultChannel) close(resultChannel)
for result := range resultChannel { for result := range resultChannel {
PrintHashFileResult(&result) PrintHashFileResult(&result, *outputFormat)
} }
close(md5hashers) close(md5hashers)