Skip to content
Snippets Groups Projects
Commit b119d2ec authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

Added support for reading from Stdin

parent 7069faa2
No related branches found
No related tags found
No related merge requests found
...@@ -9,26 +9,35 @@ import ( ...@@ -9,26 +9,35 @@ import (
func main() { func main() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Println("Usage: gomd5 [file ...]") //If no file names are given, read from stdin
} //Use CRTL-D to signal eof.
files := os.Args[1:] hash, err := processInput(os.Stdin)
for _, file := range files { if err != nil {
if err := processFile(file); err != nil {
fmt.Fprintf(os.Stderr, "gomd5: %v", err) fmt.Fprintf(os.Stderr, "gomd5: %v", err)
} else {
fmt.Printf("%x\n", hash)
}
} else {
files := os.Args[1:]
for _, file := range files {
f, err := os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, "gomd5: %v", err)
}
hash, err := processInput(f)
if err != nil {
fmt.Fprintf(os.Stderr, "gomd5: %v", err)
}
f.Close()
fmt.Printf("%x\n", hash)
} }
} }
} }
func processFile(filename string) error { func processInput(input io.Reader) (string, error) {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
h := md5.New() h := md5.New()
if _, err := io.Copy(h, f); err != nil { if _, err := io.Copy(h, input); err != nil {
return err return "", err
} }
fmt.Printf("%x\n", h.Sum(nil)) return string(h.Sum(nil)), nil
return nil
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment