/*
netstring -- netstring format serialization library
-Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
fmt.Fprintf(os.Stderr, "Usage: %s list FILE\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s read FILE CHUNK > data\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s write FILE ... > data\n", os.Args[0])
+ fmt.Fprintf(os.Stderr, "FILE can be \"-\" for reading from stdin\n")
os.Exit(1)
}
+func fileRead(path string) io.Reader {
+ if path == "-" {
+ return os.Stdin
+ }
+ fd, err := os.Open(path)
+ if err != nil {
+ panic(err)
+ }
+ return fd
+}
+
func main() {
flag.Usage = usage
flag.Parse()
}
switch os.Args[1] {
case "list":
- fd, err := os.Open(os.Args[2])
- if err != nil {
- panic(err)
- }
- r := netstring.NewReader(fd)
+ r := netstring.NewReader(fileRead(os.Args[2]))
for i := 0; ; i++ {
size, err := r.Next()
if err == io.EOF {
if err != nil {
panic(err)
}
- fd, err := os.Open(os.Args[2])
- if err != nil {
- panic(err)
- }
- r := netstring.NewReader(fd)
+ r := netstring.NewReader(fileRead(os.Args[2]))
for i := 0; i < chunk; i++ {
_, err = r.Next()
if err != nil {