From: Sergey Matveev <stargrave@stargrave.org>
Date: Mon, 11 Mar 2024 12:33:04 +0000 (+0300)
Subject: Convenient -pipe instead of -stdin
X-Git-Tag: v0.8.0
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=c501ffd4328ebafe3d8739cbb21bb78f8b154443;p=meta4ra.git

Convenient -pipe instead of -stdin
---

diff --git a/cmd/meta4ra/check.go b/cmd/meta4ra/check.go
index c53ccb9..3b3e0a9 100644
--- a/cmd/meta4ra/check.go
+++ b/cmd/meta4ra/check.go
@@ -31,7 +31,7 @@ import (
 )
 
 func runCheck() {
-	stdin := flag.Bool("stdin", false, "Compare data of single file taken from stdin")
+	pipe := flag.Bool("pipe", false, "Verify file data from stdin, copy to stdout")
 	allHashes := flag.Bool("all-hashes", false, "Check all hashes, not the first common one")
 	hashes := flag.String("hashes", meta4ra.HashesDefault,
 		"hash-name:commandline[,...]")
@@ -73,8 +73,8 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 	for _, fn := range flag.Args() {
 		toCheck[path.Base(fn)] = fn
 	}
-	if *stdin && len(toCheck) != 1 {
-		log.Fatalln("exactly single FILE must be specified when using -stdin")
+	if *pipe && len(toCheck) != 1 {
+		log.Fatalln("exactly single FILE must be specified when using -pipe")
 	}
 
 	bad := false
@@ -98,7 +98,7 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 				[]byte(strings.TrimPrefix(sig.Signature, "\n")),
 				fs.FileMode(0666),
 			); err != nil {
-				fmt.Println("Error:", f.Name, "can not save signature:", err)
+				log.Println("Error:", f.Name, "can not save signature:", err)
 				bad = true
 			}
 		}
@@ -111,15 +111,15 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 		if fullPath == "" {
 			fullPath = f.Name
 		}
-		if !*stdin {
+		if !*pipe {
 			s, err := os.Stat(fullPath)
 			if err != nil {
-				fmt.Println(err)
+				log.Println(err)
 				bad = true
 				continue
 			}
 			if uint64(s.Size()) != f.Size {
-				fmt.Println("size mismatch",
+				log.Println("size mismatch",
 					f.Name, "our:", s.Size(), "their:", f.Size)
 				bad = true
 				continue
@@ -128,7 +128,7 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 
 		hasher, err := meta4ra.NewHasher(*hashes)
 		if err != nil {
-			fmt.Println(f.Name, err)
+			log.Println(f.Name, err)
 			bad = true
 			continue
 		}
@@ -150,44 +150,50 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 				}
 			}
 		}
-		fmt.Println("no common hashes found for:", f.Name)
+		log.Println("no common hashes found for:", f.Name)
 		bad = true
 		continue
 	HashFound:
 
 		fd := os.Stdin
-		if !*stdin {
+		if !*pipe {
 			fd, err = os.Open(fullPath)
 			if err != nil {
-				fmt.Println("Error:", f.Name, err)
+				log.Println("Error:", f.Name, err)
 				bad = true
 				continue
 			}
 		}
 		err = hasher.Start()
 		if err != nil {
-			if !*stdin {
+			if !*pipe {
 				fd.Close()
 			}
 			hasher.Stop()
-			fmt.Println("Error:", f.Name, err)
+			log.Println("Error:", f.Name, err)
 			bad = true
 			continue
 		}
-		_, err = io.Copy(hasher, bufio.NewReaderSize(fd, meta4ra.BufLen))
-		if !*stdin {
+		var w io.Writer
+		if *pipe {
+			w = io.MultiWriter(os.Stdout, hasher)
+		} else {
+			w = hasher
+		}
+		_, err = io.Copy(w, bufio.NewReaderSize(fd, meta4ra.BufLen))
+		if !*pipe {
 			fd.Close()
 		}
 		if err != nil {
 			hasher.Stop()
-			fmt.Println("Error:", f.Name, err)
+			log.Println("Error:", f.Name, err)
 			bad = true
 			continue
 		}
 		sums, err := hasher.Sums()
 		if err != nil {
 			hasher.Stop()
-			fmt.Println("Error:", f.Name, err)
+			log.Println("Error:", f.Name, err)
 			bad = true
 			continue
 		}
@@ -199,9 +205,11 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 			for _, h := range f.Hashes {
 				hashOur := hashesOur[h.Type]
 				if h.Hash == hashOur {
-					fmt.Println(f.Name, h.Type, "good")
+					if !*pipe {
+						fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
+					}
 				} else {
-					fmt.Println(
+					log.Println(
 						"hash mismatch:", f.Name, h.Type,
 						"our:", hashOur,
 						"their:", h.Hash,
@@ -212,9 +220,11 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 		} else {
 			hashOur := sums[0].Hash
 			if hashOur == hashTheir {
-				fmt.Println(f.Name, hashName, "good")
+				if !*pipe {
+					fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
+				}
 			} else {
-				fmt.Println(
+				log.Println(
 					"hash mismatch:", f.Name, hashName,
 					"our:", hashOur,
 					"their:", hashTheir,
@@ -226,7 +236,7 @@ and -extract-sig, then you can just specify an empty ("") FILE.
 	}
 	if len(toCheck) != 0 {
 		if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
-			fmt.Println("not all FILEs met")
+			log.Println("not all FILEs met")
 			bad = true
 		}
 	}
diff --git a/internal/common.go b/internal/common.go
index 2ea991e..cc62ce7 100644
--- a/internal/common.go
+++ b/internal/common.go
@@ -21,7 +21,7 @@ import (
 )
 
 const (
-	Generator       = "meta4ra/0.7.1"
+	Generator       = "meta4ra/0.8.0"
 	SigMediaTypePGP = "application/pgp-signature"
 	SigMediaTypeSSH = "application/ssh-signature"
 	BufLen          = 1 << 20