Add head
This commit is contained in:
1 parent
d094be6014
commit
b8b468f0b8
5 files changed
+61
-3
No files matched your search
+34
-1
@@ -1,10 +1,43 @@
|
||||
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() {
|
||||
fmt.Println(os.Args)
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user