]> Cypherpunks.ru repositories - netstring.git/blob - cmd/netstring/main.go
460341e43cff5401eb1980d75f6e2766ebd9dc52
[netstring.git] / cmd / netstring / main.go
1 /*
2 netstring -- netstring format serialization library
3 Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "flag"
22         "fmt"
23         "io"
24         "os"
25         "strconv"
26
27         "go.cypherpunks.ru/netstring/v2"
28 )
29
30 func usage() {
31         fmt.Fprintf(os.Stderr, "ns -- Work with netstring encoded files\n\n")
32         fmt.Fprintf(os.Stderr, "Usage: %s list FILE\n", os.Args[0])
33         fmt.Fprintf(os.Stderr, "       %s read FILE CHUNK > data\n", os.Args[0])
34         fmt.Fprintf(os.Stderr, "       %s write FILE ... > data\n", os.Args[0])
35         fmt.Fprintf(os.Stderr, "FILE can be \"-\" for reading from stdin\n")
36         os.Exit(1)
37 }
38
39 func fileRead(path string) io.Reader {
40         if path == "-" {
41                 return os.Stdin
42         }
43         fd, err := os.Open(path)
44         if err != nil {
45                 panic(err)
46         }
47         return fd
48 }
49
50 func main() {
51         flag.Usage = usage
52         flag.Parse()
53         if len(os.Args) < 3 {
54                 usage()
55         }
56         switch os.Args[1] {
57         case "list":
58                 r := netstring.NewReader(fileRead(os.Args[2]))
59                 for i := 0; ; i++ {
60                         size, err := r.Next()
61                         if err == io.EOF {
62                                 break
63                         }
64                         if err != nil {
65                                 panic(err)
66                         }
67                         fmt.Printf("%d\t%d\n", i, size)
68                         r.Discard()
69                 }
70         case "read":
71                 if len(os.Args) != 4 {
72                         usage()
73                 }
74                 chunk, err := strconv.Atoi(os.Args[3])
75                 if err != nil {
76                         panic(err)
77                 }
78                 r := netstring.NewReader(fileRead(os.Args[2]))
79                 for i := 0; i < chunk; i++ {
80                         _, err = r.Next()
81                         if err != nil {
82                                 panic(err)
83                         }
84                         r.Discard()
85                 }
86                 _, err = r.Next()
87                 if err != nil {
88                         panic(err)
89                 }
90                 if _, err = io.Copy(os.Stdout, r); err != nil {
91                         panic(err)
92                 }
93         case "write":
94                 w := netstring.NewWriter(os.Stdout)
95                 for i, fn := range os.Args[2:] {
96                         fd, err := os.Open(fn)
97                         if err != nil {
98                                 panic(err)
99                         }
100                         fi, err := fd.Stat()
101                         if err != nil {
102                                 panic(err)
103                         }
104                         size := uint64(fi.Size())
105                         if _, err = w.WriteSize(size); err != nil {
106                                 panic(err)
107                         }
108                         if _, err = io.Copy(w, fd); err != nil {
109                                 panic(err)
110                         }
111                         fmt.Fprintf(os.Stderr, "%d\t%d\t%s\n", i, size, fn)
112                 }
113         }
114 }