diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ut/lu/lu.go	Sun Jun 17 20:29:45 2018 +0900
@@ -0,0 +1,298 @@
+/*
+    little unix tool library.
+*/
+package lu
+
+import (
+    "archive/tar"
+    "bufio"
+    "bytes"
+    "compress/gzip"
+    "crypto/md5"
+    "crypto/sha256"
+    "fmt"
+    "io"
+    "log"
+    "os"
+    "os/exec"
+    "path/filepath"
+    "strings"
+)
+
+func CmdTime( cmdline string ) {
+    cmd := exec.Command( "ls", "-l" )
+	cmd.Stdin = strings.NewReader( "some input" )
+	var out bytes.Buffer
+	cmd.Stdout = &out
+	err := cmd.Run()
+	if err != nil {
+		log.Fatal( err )
+	}
+	fmt.Printf( "%q\n", out.String() )
+}
+
+/* cut -d, -f: */
+func CutCsv( files []string, cols []string ) {
+}
+
+/* cat -n: done. */
+func Cat_n( files []string ) {
+    if len( files ) == 0 {
+        cnt := 1
+        input := bufio.NewScanner( os.Stdin )
+        for input.Scan() {
+            fmt.Printf( "%5d  %s\n", cnt, input.Text() )
+            cnt++
+        }
+    } else {
+        for _, file := range files {
+            cnt := 1
+            f, _ := os.Open( file )
+            input := bufio.NewScanner( f )
+            for input.Scan() {
+                fmt.Printf( "%5d  %s\n", cnt, input.Text() )
+                cnt++
+            }
+            f.Close()
+        }
+    }
+}
+
+/* wc -l: done. */
+func Wc_l( files []string ) {
+    if len( files ) == 0 {
+        cnt := 0
+        input := bufio.NewScanner( os.Stdin )
+        for input.Scan() {
+            cnt++
+        }
+        fmt.Printf( "%d\n", cnt )
+    } else {
+        for _, file := range files {
+            cnt := 0
+            f, _ := os.Open( file )
+            input := bufio.NewScanner( f )
+            for input.Scan() {
+                cnt++
+            }
+            f.Close()
+            fmt.Printf( "%s:\t%5d\n", file, cnt )
+        }
+    }
+}
+
+/* tar xvfz: done. */
+func UnTgz( files []string ) {
+
+    for _, file := range files {
+
+        f, err := os.Open( file )
+        if err != nil {
+            log.Fatal( err )
+        }
+        zr, err := gzip.NewReader( f )
+        if err != nil {
+            log.Fatal( err )
+        }
+
+        tr := tar.NewReader( zr )
+
+        for {
+            hdr, err := tr.Next()
+            if err == io.EOF {
+                break // End of archive
+            }
+            if err != nil {
+                log.Fatal( err )
+            }
+            fmt.Printf( "%s\n", hdr.Name )
+
+            dir := filepath.Dir( hdr.Name )
+            if ( dir != "" ) {
+                os.MkdirAll( dir, 0744 )
+            }
+            f, err := os.OpenFile( hdr.Name, os.O_RDWR|os.O_CREATE, 0644 )
+            if err != nil {
+                log.Fatal( err )
+            }
+            if _, err := io.Copy( f, tr ); err != nil {
+                log.Fatal( err )
+            }
+
+            if err := f.Close(); err != nil {
+                log.Fatal( err )
+            }
+        }
+
+        if err := zr.Close(); err != nil {
+            log.Fatal( err )
+        }
+    }
+
+}
+
+/* tac: done. */
+func Reverse( files []string ) {
+    var buf []string
+    if len( files ) == 0 {
+        input := bufio.NewScanner( os.Stdin )
+        for input.Scan() {
+            buf = append( buf, input.Text() )
+        }
+    } else {
+        for _, file := range files {
+            f, _ := os.Open( file )
+            input := bufio.NewScanner( f )
+            for input.Scan() {
+                buf = append( buf, input.Text() )
+            }
+            f.Close()
+        }
+    }
+    for i := len( buf ); i > 0; i-- {
+        fmt.Println( buf[i-1] )
+    }
+}
+
+func Grep( word string, inv bool, files []string ) {
+    if len( files ) == 0 {
+        fmt.Println("grep stdin")
+    } else {
+        fmt.Println("grep")
+    }
+}
+
+/* uniq: done. */
+func Uniq( cnt, dup bool, files []string ) {
+    var lines []string
+    lcnt := make( map[string]int )
+    lpnt := make( map[string]bool )
+
+    if len( files ) == 0 {
+        input := bufio.NewScanner( os.Stdin )
+        for input.Scan() {
+            lines = append( lines, input.Text() )
+            lcnt[ input.Text() ]++
+        }
+    } else {
+        for _, file := range files {
+            f, _ := os.Open( file )
+            input := bufio.NewScanner( f )
+            for input.Scan() {
+                lines = append( lines, input.Text() )
+                lcnt[ input.Text() ]++
+            }
+            f.Close()
+        }
+    }
+
+    for _, line := range lines {
+        if !lpnt[ line ] {
+            if dup && lcnt[ line ] == 1 {
+                continue
+            }
+            if cnt {
+                fmt.Printf( "%5d  ,", lcnt[ line ] )
+            }
+            fmt.Printf( "%s\n", line )
+            lpnt[ line ] = true
+        }
+    }
+}
+
+func Head( n int, files []string ) {
+    if n > 0 {
+        if len( files ) == 0 {
+            input := bufio.NewScanner( os.Stdin )
+            for input.Scan() {
+                fmt.Println( input.Text() )
+                n--
+                if n == 0 {
+                    return
+                }
+            }
+        } else {
+            for _, file := range files {
+                if n == 0 {
+                    return
+                }
+                f, _ := os.Open( file )
+                input := bufio.NewScanner( f )
+                for input.Scan() {
+                    fmt.Println( input.Text() )
+                    n--
+                    if n == 0 {
+                        break
+                    }
+                }
+                f.Close()
+            }
+        }
+    } else {
+        if len( files ) == 0 {
+
+        } else {
+
+        }
+    }
+}
+
+/* replace:  */
+func Replace( s, t string, files []string ) {
+    if len( files ) == 0 {
+        input := bufio.NewScanner( os.Stdin )
+        for input.Scan() {
+        }
+    } else {
+        for _, file := range files {
+            f, _ := os.Open( file )
+            input := bufio.NewScanner( f )
+            for input.Scan() {
+            }
+            f.Close()
+        }
+    }
+}
+
+/* touch:  */
+func Touch( files []string ) {
+    for _, file := range files {
+        f, _ := os.Open( file )
+        f.Close()
+    }
+}
+
+/* md5: done. */
+func Md5( files []string ) {
+    for _, file := range files {
+        f, _ := os.Open( file )
+        h := md5.New()
+        if _, err := io.Copy( h, f ); err != nil {
+            log.Fatal( err )
+        }
+        f.Close()
+        if len( files ) == 1 {
+            fmt.Printf( "%x\n", h.Sum( nil ) )
+        } else {
+            fmt.Printf( "%s:\t%x\n", file, h.Sum( nil ) )
+        }
+    }
+}
+
+/* sha256: done.  */
+func Sha256( files []string ) {
+    for _, file := range files {
+        f, _ := os.Open( file )
+        h := sha256.New()
+        if _, err := io.Copy( h, f ); err != nil {
+            log.Fatal( err )
+        }
+        f.Close()
+        if len( files ) == 1 {
+            fmt.Printf( "%x\n", h.Sum( nil ) )
+        } else {
+            fmt.Printf( "%s:\t%x\n", file, h.Sum( nil ) )
+        }
+    }
+}
+