parent
d094be6014
commit
b8b468f0b8
5 changed files with 61 additions and 3 deletions
@ -0,0 +1,13 @@ |
|||||||
|
package common |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"os" |
||||||
|
) |
||||||
|
|
||||||
|
func ExitIfError(err error) { |
||||||
|
if err != nil { |
||||||
|
fmt.Fprintf(os.Stderr, "%v: %v\n", os.Args[0], err) |
||||||
|
os.Exit(1) |
||||||
|
} |
||||||
|
} |
@ -1,3 +1,6 @@ |
|||||||
module source.heropunch.io/tomo/go-coreutils |
module source.heropunch.io/tomo/go-coreutils |
||||||
|
|
||||||
require github.com/stretchr/testify v1.3.0 |
require ( |
||||||
|
github.com/alexflint/go-arg v1.0.0 |
||||||
|
github.com/stretchr/testify v1.3.0 |
||||||
|
) |
||||||
|
@ -1,7 +1,16 @@ |
|||||||
|
github.com/alexflint/go-arg v1.0.0 h1:VWNnY3DyBHiq5lcwY2FlCE5t5qyHNV0o5i1bkCIHprU= |
||||||
|
github.com/alexflint/go-arg v1.0.0/go.mod h1:Cto8k5VtkP4pp0EXiWD4ZJMFOOinZ38ggVcQ/6CGuRI= |
||||||
|
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70= |
||||||
|
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= |
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= |
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= |
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= |
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= |
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= |
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= |
||||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= |
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= |
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
||||||
|
@ -1,10 +1,43 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
|
"bufio" |
||||||
"fmt" |
"fmt" |
||||||
|
"io" |
||||||
"os" |
"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() { |
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) |
||||||
|
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue