]> Sergey Matveev's repositories - meta4ra.git/commitdiff
Convenient -pipe instead of -stdin v0.8.0
authorSergey Matveev <stargrave@stargrave.org>
Mon, 11 Mar 2024 12:33:04 +0000 (15:33 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 12 Mar 2024 09:38:11 +0000 (12:38 +0300)
cmd/meta4ra/check.go
internal/common.go

index c53ccb9f0e7c7768ada5c1ff2d8f92eeb49a39da..3b3e0a916087635136fd4ff6e9db74145aa63e2b 100644 (file)
@@ -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
                }
        }
index 2ea991ebdc2c32ce4d6e47c24e286a7d60862f96..cc62ce716296eedf892c59d18c08fe98ddfd27b2 100644 (file)
@@ -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