]> Cypherpunks.ru repositories - netstring.git/blob - cmd/netstring/main.go
Refactoring, io.Reader/Writer friendliness, performance optimization
[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         os.Exit(1)
36 }
37
38 func main() {
39         flag.Usage = usage
40         flag.Parse()
41         if len(os.Args) < 3 {
42                 usage()
43         }
44         switch os.Args[1] {
45         case "list":
46                 fd, err := os.Open(os.Args[2])
47                 if err != nil {
48                         panic(err)
49                 }
50                 r := netstring.NewReader(fd)
51                 for i := 0; ; i++ {
52                         size, err := r.Next()
53                         if err == io.EOF {
54                                 break
55                         }
56                         if err != nil {
57                                 panic(err)
58                         }
59                         fmt.Printf("%d\t%d\n", i, size)
60                         r.Discard()
61                 }
62         case "read":
63                 if len(os.Args) != 4 {
64                         usage()
65                 }
66                 chunk, err := strconv.Atoi(os.Args[3])
67                 if err != nil {
68                         panic(err)
69                 }
70                 fd, err := os.Open(os.Args[2])
71                 if err != nil {
72                         panic(err)
73                 }
74                 r := netstring.NewReader(fd)
75                 for i := 0; i < chunk; i++ {
76                         _, err = r.Next()
77                         if err != nil {
78                                 panic(err)
79                         }
80                         r.Discard()
81                 }
82                 _, err = r.Next()
83                 if err != nil {
84                         panic(err)
85                 }
86                 if _, err = io.Copy(os.Stdout, r); err != nil {
87                         panic(err)
88                 }
89         case "write":
90                 w := netstring.NewWriter(os.Stdout)
91                 for i, fn := range os.Args[2:] {
92                         fd, err := os.Open(fn)
93                         if err != nil {
94                                 panic(err)
95                         }
96                         fi, err := fd.Stat()
97                         if err != nil {
98                                 panic(err)
99                         }
100                         size := uint64(fi.Size())
101                         if _, err = w.WriteSize(size); err != nil {
102                                 panic(err)
103                         }
104                         if _, err = io.Copy(w, fd); err != nil {
105                                 panic(err)
106                         }
107                         fmt.Fprintf(os.Stderr, "%d\t%d\t%s\n", i, size, fn)
108                 }
109         }
110 }