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 (
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gomd5 [file ...]")
}
files := os.Args[1:]
for _, file := range files {
if err := processFile(file); err != nil {
//If no file names are given, read from stdin
//Use CRTL-D to signal eof.
hash, err := processInput(os.Stdin)
if err != nil {
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 {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
func processInput(input io.Reader) (string, error) {
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return err
if _, err := io.Copy(h, input); err != nil {
return "", err
}
fmt.Printf("%x\n", h.Sum(nil))
return nil
return string(h.Sum(nil)), 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