Added JSON output format
This commit is contained in:
parent
d679f9a541
commit
b847a14bce
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/minio/md5-simd"
|
||||
|
@ -15,18 +16,18 @@ import (
|
|||
)
|
||||
|
||||
type HashFileResult struct {
|
||||
error error
|
||||
path string
|
||||
sha256 string
|
||||
md5 string
|
||||
Error error
|
||||
Path string
|
||||
SHA256 string
|
||||
MD5 string
|
||||
}
|
||||
|
||||
func HashFile(results chan<- HashFileResult, md5hasher *md5simd.Hasher, sha256hasher *hash.Hash, path string) {
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
results <- HashFileResult{
|
||||
error: err,
|
||||
path: path,
|
||||
Error: err,
|
||||
Path: path,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -35,18 +36,35 @@ func HashFile(results chan<- HashFileResult, md5hasher *md5simd.Hasher, sha256ha
|
|||
io.Copy(io.MultiWriter(*sha256hasher, *md5hasher), fh)
|
||||
|
||||
results <- HashFileResult{
|
||||
error: nil,
|
||||
path: path,
|
||||
sha256: hex.EncodeToString((*sha256hasher).Sum(nil)),
|
||||
md5: hex.EncodeToString((*md5hasher).Sum(nil)),
|
||||
Error: nil,
|
||||
Path: path,
|
||||
SHA256: hex.EncodeToString((*sha256hasher).Sum(nil)),
|
||||
MD5: hex.EncodeToString((*md5hasher).Sum(nil)),
|
||||
}
|
||||
}
|
||||
|
||||
func PrintHashFileResult(result *HashFileResult) {
|
||||
if result.error != nil {
|
||||
fmt.Fprintln(os.Stderr, result.path, "error: ", result.error)
|
||||
} else {
|
||||
fmt.Println(result.sha256, result.md5, result.path)
|
||||
func PrintHashFileResult(result *HashFileResult, format string) {
|
||||
switch {
|
||||
case format == "json":
|
||||
var jsonData []byte
|
||||
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
|
||||
}(), "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()
|
||||
|
||||
var taskCount int64
|
||||
|
@ -108,13 +128,13 @@ func main() {
|
|||
//Already print before finishing, use atomic ints instead of a WaitGroup
|
||||
for atomic.LoadInt64(&taskCount) > 0 {
|
||||
result := <-resultChannel
|
||||
PrintHashFileResult(&result)
|
||||
PrintHashFileResult(&result, *outputFormat)
|
||||
}
|
||||
|
||||
close(resultChannel)
|
||||
|
||||
for result := range resultChannel {
|
||||
PrintHashFileResult(&result)
|
||||
PrintHashFileResult(&result, *outputFormat)
|
||||
}
|
||||
|
||||
close(md5hashers)
|
||||
|
|
Loading…
Reference in a new issue