16
|
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"
|
18
|
19 "regexp"
|
16
|
20 "strings"
|
|
21 )
|
|
22
|
|
23 func CmdTime( cmdline string ) {
|
|
24 cmd := exec.Command( "ls", "-l" )
|
|
25 cmd.Stdin = strings.NewReader( "some input" )
|
|
26 var out bytes.Buffer
|
|
27 cmd.Stdout = &out
|
|
28 err := cmd.Run()
|
|
29 if err != nil {
|
|
30 log.Fatal( err )
|
|
31 }
|
|
32 fmt.Printf( "%q\n", out.String() )
|
|
33 }
|
|
34
|
|
35 /* cut -d, -f: */
|
|
36 func CutCsv( files []string, cols []string ) {
|
|
37 }
|
|
38
|
|
39 /* cat -n: done. */
|
|
40 func Cat_n( files []string ) {
|
|
41 if len( files ) == 0 {
|
|
42 cnt := 1
|
|
43 input := bufio.NewScanner( os.Stdin )
|
|
44 for input.Scan() {
|
|
45 fmt.Printf( "%5d %s\n", cnt, input.Text() )
|
|
46 cnt++
|
|
47 }
|
|
48 } else {
|
|
49 for _, file := range files {
|
|
50 cnt := 1
|
|
51 f, _ := os.Open( file )
|
|
52 input := bufio.NewScanner( f )
|
|
53 for input.Scan() {
|
|
54 fmt.Printf( "%5d %s\n", cnt, input.Text() )
|
|
55 cnt++
|
|
56 }
|
|
57 f.Close()
|
|
58 }
|
|
59 }
|
|
60 }
|
|
61
|
|
62 /* wc -l: done. */
|
|
63 func Wc_l( files []string ) {
|
|
64 if len( files ) == 0 {
|
|
65 cnt := 0
|
|
66 input := bufio.NewScanner( os.Stdin )
|
|
67 for input.Scan() {
|
|
68 cnt++
|
|
69 }
|
|
70 fmt.Printf( "%d\n", cnt )
|
|
71 } else {
|
|
72 for _, file := range files {
|
|
73 cnt := 0
|
|
74 f, _ := os.Open( file )
|
|
75 input := bufio.NewScanner( f )
|
|
76 for input.Scan() {
|
|
77 cnt++
|
|
78 }
|
|
79 f.Close()
|
|
80 fmt.Printf( "%s:\t%5d\n", file, cnt )
|
|
81 }
|
|
82 }
|
|
83 }
|
|
84
|
17
|
85 /* tar cvfz: done. */
|
|
86 func Tgz( tgzfile string, files []string ) {
|
|
87
|
|
88 tgz, err := os.Create( tgzfile )
|
|
89 if err != nil {
|
|
90 log.Fatal( err )
|
|
91 }
|
|
92 defer tgz.Close()
|
|
93
|
|
94 gw := gzip.NewWriter( tgz )
|
|
95 defer gw.Close()
|
|
96 tw := tar.NewWriter( gw )
|
|
97 defer tw.Close()
|
|
98
|
|
99 for _, file := range fullpath( files ) {
|
|
100 f, err := os.Open( file )
|
|
101 if err != nil {
|
|
102 log.Fatal( err )
|
|
103 }
|
|
104
|
|
105 stat, _ := f.Stat()
|
|
106 header := new( tar.Header )
|
|
107 header.Name = file
|
|
108 header.Size = stat.Size()
|
|
109 header.Mode = int64( stat.Mode() )
|
|
110 header.ModTime = stat.ModTime()
|
|
111
|
|
112 if err := tw.WriteHeader( header ); err != nil {
|
|
113 log.Fatal( err )
|
|
114 }
|
|
115 if _, err := io.Copy( tw, f ); err != nil {
|
|
116 log.Fatal( err )
|
|
117 }
|
|
118 f.Close()
|
|
119 }
|
|
120 }
|
|
121
|
16
|
122 /* tar xvfz: done. */
|
|
123 func UnTgz( files []string ) {
|
|
124
|
|
125 for _, file := range files {
|
|
126
|
|
127 f, err := os.Open( file )
|
|
128 if err != nil {
|
|
129 log.Fatal( err )
|
|
130 }
|
|
131 zr, err := gzip.NewReader( f )
|
|
132 if err != nil {
|
|
133 log.Fatal( err )
|
|
134 }
|
|
135
|
|
136 tr := tar.NewReader( zr )
|
|
137
|
|
138 for {
|
|
139 hdr, err := tr.Next()
|
|
140 if err == io.EOF {
|
|
141 break // End of archive
|
|
142 }
|
|
143 if err != nil {
|
|
144 log.Fatal( err )
|
|
145 }
|
|
146 fmt.Printf( "%s\n", hdr.Name )
|
|
147
|
|
148 dir := filepath.Dir( hdr.Name )
|
|
149 if ( dir != "" ) {
|
|
150 os.MkdirAll( dir, 0744 )
|
|
151 }
|
|
152 f, err := os.OpenFile( hdr.Name, os.O_RDWR|os.O_CREATE, 0644 )
|
|
153 if err != nil {
|
|
154 log.Fatal( err )
|
|
155 }
|
|
156 if _, err := io.Copy( f, tr ); err != nil {
|
|
157 log.Fatal( err )
|
|
158 }
|
|
159
|
|
160 if err := f.Close(); err != nil {
|
|
161 log.Fatal( err )
|
|
162 }
|
|
163 }
|
|
164
|
|
165 if err := zr.Close(); err != nil {
|
|
166 log.Fatal( err )
|
|
167 }
|
|
168 }
|
17
|
169 }
|
16
|
170
|
17
|
171 func fullpath( files []string ) []string {
|
|
172 var buf []string
|
|
173 for _, f := range files {
|
|
174 fi, _ := os.Stat( f )
|
|
175 if fi.IsDir() {
|
|
176 filepath.Walk( f, func( path string, info os.FileInfo, err error ) error {
|
|
177 if !info.IsDir() {
|
|
178 buf = append( buf, path )
|
|
179 }
|
|
180 return nil
|
|
181 } )
|
|
182 } else {
|
|
183 buf = append( buf, f )
|
|
184 }
|
|
185 }
|
|
186 return buf
|
16
|
187 }
|
|
188
|
|
189 /* tac: done. */
|
|
190 func Reverse( files []string ) {
|
|
191 var buf []string
|
|
192 if len( files ) == 0 {
|
|
193 input := bufio.NewScanner( os.Stdin )
|
|
194 for input.Scan() {
|
|
195 buf = append( buf, input.Text() )
|
|
196 }
|
|
197 } else {
|
|
198 for _, file := range files {
|
|
199 f, _ := os.Open( file )
|
|
200 input := bufio.NewScanner( f )
|
|
201 for input.Scan() {
|
|
202 buf = append( buf, input.Text() )
|
|
203 }
|
|
204 f.Close()
|
|
205 }
|
|
206 }
|
|
207 for i := len( buf ); i > 0; i-- {
|
|
208 fmt.Println( buf[i-1] )
|
|
209 }
|
|
210 }
|
|
211
|
18
|
212 /* grep: done. */
|
16
|
213 func Grep( word string, inv bool, files []string ) {
|
18
|
214 regex, err := regexp.Compile( word )
|
|
215 if err != nil {
|
|
216 log.Fatal( err )
|
|
217 }
|
16
|
218 if len( files ) == 0 {
|
18
|
219 grep( regex, inv, os.Stdin )
|
16
|
220 } else {
|
18
|
221 for _, file := range files {
|
|
222 f, _ := os.Open( file )
|
|
223 grep( regex, inv, f )
|
|
224 f.Close()
|
|
225 }
|
|
226 }
|
|
227 }
|
|
228
|
|
229 func grep( regex *regexp.Regexp, inv bool, r io.Reader ) {
|
|
230 input := bufio.NewScanner( r )
|
|
231 for input.Scan() {
|
|
232 if regex.MatchString( input.Text() ) && !inv {
|
|
233 fmt.Println( input.Text() )
|
|
234 }
|
|
235 if !regex.MatchString( input.Text() ) && inv {
|
|
236 fmt.Println( input.Text() )
|
|
237 }
|
|
238 }
|
|
239 }
|
|
240
|
|
241 /* orgrep: done. */
|
|
242 func OrGrep( lfile string, inv bool, files []string ) {
|
|
243 var keywords []regexp.Regexp
|
|
244 f, _ := os.Open( lfile )
|
|
245 input := bufio.NewScanner( f )
|
|
246 for input.Scan() {
|
|
247 regex, err := regexp.Compile( input.Text() )
|
|
248 if err != nil {
|
|
249 log.Fatal( err )
|
|
250 }
|
|
251 keywords = append( keywords, *regex )
|
|
252 }
|
|
253 f.Close()
|
|
254
|
|
255 if len( files ) == 0 {
|
|
256 orgrep( keywords, inv, os.Stdin )
|
|
257 } else {
|
|
258 for _, file := range files {
|
|
259 f, _ := os.Open( file )
|
|
260 orgrep( keywords, inv, f )
|
|
261 f.Close()
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265
|
|
266 func orgrep( keywords []regexp.Regexp, inv bool, r io.Reader ) {
|
|
267 input := bufio.NewScanner( r )
|
|
268 seen := make( map[string]bool )
|
|
269 for input.Scan() {
|
|
270 for _, r := range keywords {
|
|
271 if r.MatchString( input.Text() ) {
|
|
272 seen[ input.Text() ] = true
|
|
273 }
|
|
274 }
|
|
275 if !inv && seen[ input.Text() ] {
|
|
276 fmt.Println( input.Text() )
|
|
277 }
|
|
278 if inv && !seen[ input.Text() ] {
|
|
279 fmt.Println( input.Text() )
|
|
280 }
|
|
281 seen[ input.Text() ] = false
|
16
|
282 }
|
|
283 }
|
|
284
|
|
285 /* uniq: done. */
|
|
286 func Uniq( cnt, dup bool, files []string ) {
|
|
287 var lines []string
|
|
288 lcnt := make( map[string]int )
|
|
289 lpnt := make( map[string]bool )
|
|
290
|
|
291 if len( files ) == 0 {
|
|
292 input := bufio.NewScanner( os.Stdin )
|
|
293 for input.Scan() {
|
|
294 lines = append( lines, input.Text() )
|
|
295 lcnt[ input.Text() ]++
|
|
296 }
|
|
297 } else {
|
|
298 for _, file := range files {
|
|
299 f, _ := os.Open( file )
|
|
300 input := bufio.NewScanner( f )
|
|
301 for input.Scan() {
|
|
302 lines = append( lines, input.Text() )
|
|
303 lcnt[ input.Text() ]++
|
|
304 }
|
|
305 f.Close()
|
|
306 }
|
|
307 }
|
|
308
|
|
309 for _, line := range lines {
|
|
310 if !lpnt[ line ] {
|
|
311 if dup && lcnt[ line ] == 1 {
|
|
312 continue
|
|
313 }
|
|
314 if cnt {
|
|
315 fmt.Printf( "%5d ,", lcnt[ line ] )
|
|
316 }
|
|
317 fmt.Printf( "%s\n", line )
|
|
318 lpnt[ line ] = true
|
|
319 }
|
|
320 }
|
|
321 }
|
|
322
|
|
323 func Head( n int, files []string ) {
|
|
324 if n > 0 {
|
|
325 if len( files ) == 0 {
|
|
326 input := bufio.NewScanner( os.Stdin )
|
|
327 for input.Scan() {
|
|
328 fmt.Println( input.Text() )
|
|
329 n--
|
|
330 if n == 0 {
|
|
331 return
|
|
332 }
|
|
333 }
|
|
334 } else {
|
|
335 for _, file := range files {
|
|
336 if n == 0 {
|
|
337 return
|
|
338 }
|
|
339 f, _ := os.Open( file )
|
|
340 input := bufio.NewScanner( f )
|
|
341 for input.Scan() {
|
|
342 fmt.Println( input.Text() )
|
|
343 n--
|
|
344 if n == 0 {
|
|
345 break
|
|
346 }
|
|
347 }
|
|
348 f.Close()
|
|
349 }
|
|
350 }
|
|
351 } else {
|
|
352 if len( files ) == 0 {
|
|
353
|
|
354 } else {
|
|
355
|
|
356 }
|
|
357 }
|
|
358 }
|
|
359
|
|
360 /* replace: */
|
|
361 func Replace( s, t string, files []string ) {
|
|
362 if len( files ) == 0 {
|
|
363 input := bufio.NewScanner( os.Stdin )
|
|
364 for input.Scan() {
|
|
365 }
|
|
366 } else {
|
|
367 for _, file := range files {
|
|
368 f, _ := os.Open( file )
|
|
369 input := bufio.NewScanner( f )
|
|
370 for input.Scan() {
|
|
371 }
|
|
372 f.Close()
|
|
373 }
|
|
374 }
|
|
375 }
|
|
376
|
|
377 /* touch: */
|
|
378 func Touch( files []string ) {
|
|
379 for _, file := range files {
|
|
380 f, _ := os.Open( file )
|
|
381 f.Close()
|
|
382 }
|
|
383 }
|
|
384
|
|
385 /* md5: done. */
|
|
386 func Md5( files []string ) {
|
|
387 for _, file := range files {
|
|
388 f, _ := os.Open( file )
|
|
389 h := md5.New()
|
|
390 if _, err := io.Copy( h, f ); err != nil {
|
|
391 log.Fatal( err )
|
|
392 }
|
|
393 f.Close()
|
|
394 if len( files ) == 1 {
|
|
395 fmt.Printf( "%x\n", h.Sum( nil ) )
|
|
396 } else {
|
|
397 fmt.Printf( "%s:\t%x\n", file, h.Sum( nil ) )
|
|
398 }
|
|
399 }
|
|
400 }
|
|
401
|
|
402 /* sha256: done. */
|
|
403 func Sha256( files []string ) {
|
|
404 for _, file := range files {
|
|
405 f, _ := os.Open( file )
|
|
406 h := sha256.New()
|
|
407 if _, err := io.Copy( h, f ); err != nil {
|
|
408 log.Fatal( err )
|
|
409 }
|
|
410 f.Close()
|
|
411 if len( files ) == 1 {
|
|
412 fmt.Printf( "%x\n", h.Sum( nil ) )
|
|
413 } else {
|
|
414 fmt.Printf( "%s:\t%x\n", file, h.Sum( nil ) )
|
|
415 }
|
|
416 }
|
|
417 }
|
|
418
|