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