Mercurial > mercurial > hgweb_golang.cgi
comparison src/ut/lu/lu.go @ 16:38b64afbaf79
add ut. ( unix-like tools )
author | pyon@macmini |
---|---|
date | Sun, 17 Jun 2018 20:29:45 +0900 |
parents | |
children | 72ce457fb99d |
comparison
equal
deleted
inserted
replaced
15:3673b244ad94 | 16:38b64afbaf79 |
---|---|
1 /* | |
2 little unix tool library. | |
3 */ | |
4 package lu | |
5 | |
6 import ( | |
7 "archive/tar" | |
8 "bufio" | |
9 "bytes" | |
10 "compress/gzip" | |
11 "crypto/md5" | |
12 "crypto/sha256" | |
13 "fmt" | |
14 "io" | |
15 "log" | |
16 "os" | |
17 "os/exec" | |
18 "path/filepath" | |
19 "strings" | |
20 ) | |
21 | |
22 func CmdTime( cmdline string ) { | |
23 cmd := exec.Command( "ls", "-l" ) | |
24 cmd.Stdin = strings.NewReader( "some input" ) | |
25 var out bytes.Buffer | |
26 cmd.Stdout = &out | |
27 err := cmd.Run() | |
28 if err != nil { | |
29 log.Fatal( err ) | |
30 } | |
31 fmt.Printf( "%q\n", out.String() ) | |
32 } | |
33 | |
34 /* cut -d, -f: */ | |
35 func CutCsv( files []string, cols []string ) { | |
36 } | |
37 | |
38 /* cat -n: done. */ | |
39 func Cat_n( files []string ) { | |
40 if len( files ) == 0 { | |
41 cnt := 1 | |
42 input := bufio.NewScanner( os.Stdin ) | |
43 for input.Scan() { | |
44 fmt.Printf( "%5d %s\n", cnt, input.Text() ) | |
45 cnt++ | |
46 } | |
47 } else { | |
48 for _, file := range files { | |
49 cnt := 1 | |
50 f, _ := os.Open( file ) | |
51 input := bufio.NewScanner( f ) | |
52 for input.Scan() { | |
53 fmt.Printf( "%5d %s\n", cnt, input.Text() ) | |
54 cnt++ | |
55 } | |
56 f.Close() | |
57 } | |
58 } | |
59 } | |
60 | |
61 /* wc -l: done. */ | |
62 func Wc_l( files []string ) { | |
63 if len( files ) == 0 { | |
64 cnt := 0 | |
65 input := bufio.NewScanner( os.Stdin ) | |
66 for input.Scan() { | |
67 cnt++ | |
68 } | |
69 fmt.Printf( "%d\n", cnt ) | |
70 } else { | |
71 for _, file := range files { | |
72 cnt := 0 | |
73 f, _ := os.Open( file ) | |
74 input := bufio.NewScanner( f ) | |
75 for input.Scan() { | |
76 cnt++ | |
77 } | |
78 f.Close() | |
79 fmt.Printf( "%s:\t%5d\n", file, cnt ) | |
80 } | |
81 } | |
82 } | |
83 | |
84 /* tar xvfz: done. */ | |
85 func UnTgz( files []string ) { | |
86 | |
87 for _, file := range files { | |
88 | |
89 f, err := os.Open( file ) | |
90 if err != nil { | |
91 log.Fatal( err ) | |
92 } | |
93 zr, err := gzip.NewReader( f ) | |
94 if err != nil { | |
95 log.Fatal( err ) | |
96 } | |
97 | |
98 tr := tar.NewReader( zr ) | |
99 | |
100 for { | |
101 hdr, err := tr.Next() | |
102 if err == io.EOF { | |
103 break // End of archive | |
104 } | |
105 if err != nil { | |
106 log.Fatal( err ) | |
107 } | |
108 fmt.Printf( "%s\n", hdr.Name ) | |
109 | |
110 dir := filepath.Dir( hdr.Name ) | |
111 if ( dir != "" ) { | |
112 os.MkdirAll( dir, 0744 ) | |
113 } | |
114 f, err := os.OpenFile( hdr.Name, os.O_RDWR|os.O_CREATE, 0644 ) | |
115 if err != nil { | |
116 log.Fatal( err ) | |
117 } | |
118 if _, err := io.Copy( f, tr ); err != nil { | |
119 log.Fatal( err ) | |
120 } | |
121 | |
122 if err := f.Close(); err != nil { | |
123 log.Fatal( err ) | |
124 } | |
125 } | |
126 | |
127 if err := zr.Close(); err != nil { | |
128 log.Fatal( err ) | |
129 } | |
130 } | |
131 | |
132 } | |
133 | |
134 /* tac: done. */ | |
135 func Reverse( files []string ) { | |
136 var buf []string | |
137 if len( files ) == 0 { | |
138 input := bufio.NewScanner( os.Stdin ) | |
139 for input.Scan() { | |
140 buf = append( buf, input.Text() ) | |
141 } | |
142 } else { | |
143 for _, file := range files { | |
144 f, _ := os.Open( file ) | |
145 input := bufio.NewScanner( f ) | |
146 for input.Scan() { | |
147 buf = append( buf, input.Text() ) | |
148 } | |
149 f.Close() | |
150 } | |
151 } | |
152 for i := len( buf ); i > 0; i-- { | |
153 fmt.Println( buf[i-1] ) | |
154 } | |
155 } | |
156 | |
157 func Grep( word string, inv bool, files []string ) { | |
158 if len( files ) == 0 { | |
159 fmt.Println("grep stdin") | |
160 } else { | |
161 fmt.Println("grep") | |
162 } | |
163 } | |
164 | |
165 /* uniq: done. */ | |
166 func Uniq( cnt, dup bool, files []string ) { | |
167 var lines []string | |
168 lcnt := make( map[string]int ) | |
169 lpnt := make( map[string]bool ) | |
170 | |
171 if len( files ) == 0 { | |
172 input := bufio.NewScanner( os.Stdin ) | |
173 for input.Scan() { | |
174 lines = append( lines, input.Text() ) | |
175 lcnt[ input.Text() ]++ | |
176 } | |
177 } else { | |
178 for _, file := range files { | |
179 f, _ := os.Open( file ) | |
180 input := bufio.NewScanner( f ) | |
181 for input.Scan() { | |
182 lines = append( lines, input.Text() ) | |
183 lcnt[ input.Text() ]++ | |
184 } | |
185 f.Close() | |
186 } | |
187 } | |
188 | |
189 for _, line := range lines { | |
190 if !lpnt[ line ] { | |
191 if dup && lcnt[ line ] == 1 { | |
192 continue | |
193 } | |
194 if cnt { | |
195 fmt.Printf( "%5d ,", lcnt[ line ] ) | |
196 } | |
197 fmt.Printf( "%s\n", line ) | |
198 lpnt[ line ] = true | |
199 } | |
200 } | |
201 } | |
202 | |
203 func Head( n int, files []string ) { | |
204 if n > 0 { | |
205 if len( files ) == 0 { | |
206 input := bufio.NewScanner( os.Stdin ) | |
207 for input.Scan() { | |
208 fmt.Println( input.Text() ) | |
209 n-- | |
210 if n == 0 { | |
211 return | |
212 } | |
213 } | |
214 } else { | |
215 for _, file := range files { | |
216 if n == 0 { | |
217 return | |
218 } | |
219 f, _ := os.Open( file ) | |
220 input := bufio.NewScanner( f ) | |
221 for input.Scan() { | |
222 fmt.Println( input.Text() ) | |
223 n-- | |
224 if n == 0 { | |
225 break | |
226 } | |
227 } | |
228 f.Close() | |
229 } | |
230 } | |
231 } else { | |
232 if len( files ) == 0 { | |
233 | |
234 } else { | |
235 | |
236 } | |
237 } | |
238 } | |
239 | |
240 /* replace: */ | |
241 func Replace( s, t string, files []string ) { | |
242 if len( files ) == 0 { | |
243 input := bufio.NewScanner( os.Stdin ) | |
244 for input.Scan() { | |
245 } | |
246 } else { | |
247 for _, file := range files { | |
248 f, _ := os.Open( file ) | |
249 input := bufio.NewScanner( f ) | |
250 for input.Scan() { | |
251 } | |
252 f.Close() | |
253 } | |
254 } | |
255 } | |
256 | |
257 /* touch: */ | |
258 func Touch( files []string ) { | |
259 for _, file := range files { | |
260 f, _ := os.Open( file ) | |
261 f.Close() | |
262 } | |
263 } | |
264 | |
265 /* md5: done. */ | |
266 func Md5( files []string ) { | |
267 for _, file := range files { | |
268 f, _ := os.Open( file ) | |
269 h := md5.New() | |
270 if _, err := io.Copy( h, f ); err != nil { | |
271 log.Fatal( err ) | |
272 } | |
273 f.Close() | |
274 if len( files ) == 1 { | |
275 fmt.Printf( "%x\n", h.Sum( nil ) ) | |
276 } else { | |
277 fmt.Printf( "%s:\t%x\n", file, h.Sum( nil ) ) | |
278 } | |
279 } | |
280 } | |
281 | |
282 /* sha256: done. */ | |
283 func Sha256( files []string ) { | |
284 for _, file := range files { | |
285 f, _ := os.Open( file ) | |
286 h := sha256.New() | |
287 if _, err := io.Copy( h, f ); err != nil { | |
288 log.Fatal( err ) | |
289 } | |
290 f.Close() | |
291 if len( files ) == 1 { | |
292 fmt.Printf( "%x\n", h.Sum( nil ) ) | |
293 } else { | |
294 fmt.Printf( "%s:\t%x\n", file, h.Sum( nil ) ) | |
295 } | |
296 } | |
297 } | |
298 |