Skip to content

Commit

Permalink
cmds/core/head: improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Siarhiej Siemianczuk <[email protected]>
  • Loading branch information
binjip978 committed Jul 24, 2024
1 parent f0c8d7d commit ffb49ef
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cmds/core/head/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bufio"
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -33,11 +34,12 @@ func run(stdout io.Writer, stderr io.Writer, bytes, count int, files ...string)
}

var newLineHeader bool
var errs error

for _, file := range files {
f, err := os.Open(file)
if err != nil {
fmt.Fprintf(stderr, "head: %v", err)
errs = errors.Join(errs, fmt.Errorf("head: %w", err))
continue
}
if len(files) > 1 {
Expand All @@ -53,7 +55,7 @@ func run(stdout io.Writer, stderr io.Writer, bytes, count int, files ...string)
if err == io.ErrUnexpectedEOF {
// ignore if user request more bytes than file has
} else if err != nil {
fmt.Fprintf(stderr, "head: %v", err)
errs = errors.Join(errs, fmt.Errorf("head: %w", err))
continue
}
stdout.Write(buffer[:n])
Expand All @@ -70,6 +72,9 @@ func run(stdout io.Writer, stderr io.Writer, bytes, count int, files ...string)
}
}

if errs != nil {
fmt.Fprintf(stderr, "\n%v\n", errs)
}
return nil
}

Expand Down

0 comments on commit ffb49ef

Please sign in to comment.