Edvinas Valatka 10 yıl önce
işleme
a8897d8f5f
4 değiştirilmiş dosya ile 71 ekleme ve 0 silme
  1. 0 0
      .gitignore
  2. 6 0
      doc.go
  3. 27 0
      main.go
  4. 38 0
      utils.go

+ 0 - 0
.gitignore


+ 6 - 0
doc.go

@@ -0,0 +1,6 @@
+// hipa project doc.go
+
+/*
+hipa document
+*/
+package main

+ 27 - 0
main.go

@@ -0,0 +1,27 @@
+package main
+
+import (
+    "bufio"
+    "os"
+    "strings"
+)
+
+var history = make(map[string]int)
+
+func main() {
+    histfile, _ := os.Open(gethistfile())
+    scanner := bufio.NewScanner(histfile)
+    for scanner.Scan() {
+        line := scanner.Text()
+        history[strings.TrimSpace(line)] = 0
+    }
+    defer histfile.Close()
+    newhistfile, _ := os.OpenFile(mktmpfile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
+    newhistfile.Truncate(0)
+    for key, _ := range history {
+        newhistfile.WriteString(key + "\n")
+    }
+    newhistfile.Sync()
+    CopyFile(mktmpfile(), gethistfile())
+    defer newhistfile.Close()
+}

+ 38 - 0
utils.go

@@ -0,0 +1,38 @@
+// utils
+package main
+
+import (
+    "io"
+    "os"
+    "os/user"
+    "path"
+)
+
+func gethomedir() string {
+    u, _ := user.Current()
+    return u.HomeDir
+}
+
+func gethistfile() string {
+    const fname = ".bash_history"
+    return path.Join(gethomedir(), fname)
+}
+
+func mktmpfile() string {
+    const fname = ".bash_history"
+    return path.Join("/tmp", fname)
+}
+
+func CopyFile(src, dst string) (int64, error) {
+    sf, err := os.Open(src)
+    if err != nil {
+        return 0, err
+    }
+    defer sf.Close()
+    df, err := os.Create(dst)
+    if err != nil {
+        return 0, err
+    }
+    defer df.Close()
+    return io.Copy(df, sf)
+}