You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
720 B

package main
import (
"bufio"
"fmt"
"io"
"os"
"github.com/alexflint/go-arg"
common "source.heropunch.io/tomo/go-coreutils"
)
func head(num uint, reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() && num > 0 {
fmt.Println(scanner.Text())
num = num - 1
}
common.ExitIfError(scanner.Err())
}
func main() {
var args struct {
Lines uint `arg:"-n"`
Files []string `arg:"positional"`
}
args.Lines = 10
arg.MustParse(&args)
if len(args.Files) == 0 {
head(args.Lines, os.Stdin)
os.Exit(0)
}
for _, filename := range args.Files {
file, err := os.Open(filename)
common.ExitIfError(err)
head(args.Lines, file)
err = file.Close()
common.ExitIfError(err)
}
}