|
|
@@ -2,39 +2,69 @@ package main
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
+ "fmt"
|
|
|
"os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
+// map for old history records
|
|
|
var history = make(map[string]int)
|
|
|
|
|
|
+// path ${HOME}/.bash_history
|
|
|
+var h Hfile
|
|
|
+
|
|
|
+// path /tmp/.bash_history******/.bash_history
|
|
|
+var t Tmpfile
|
|
|
+
|
|
|
+func init() {
|
|
|
+ h.name = ".bash_history"
|
|
|
+ h.init()
|
|
|
+ h.Open()
|
|
|
+
|
|
|
+ t.name = h.name
|
|
|
+ t.init()
|
|
|
+ t.Open()
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
- histfile, _ := os.Open(gethistfile())
|
|
|
- scanner := bufio.NewScanner(histfile)
|
|
|
+ //read old history
|
|
|
+ scanner := bufio.NewScanner(h.handle)
|
|
|
var i = 0
|
|
|
for scanner.Scan() {
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
- if len(line) > 0 {
|
|
|
- if history[line] == 0 {
|
|
|
- history[line] = i
|
|
|
- i++
|
|
|
- }
|
|
|
- }
|
|
|
+ history[line] = i
|
|
|
+ i++
|
|
|
}
|
|
|
+ delete(history, "")
|
|
|
+ h.handle.Close()
|
|
|
|
|
|
- defer histfile.Close()
|
|
|
-
|
|
|
+ // push history to temp slice
|
|
|
+ // and sort by last entry position number
|
|
|
var ah = make([]string, i)
|
|
|
for key, val := range history {
|
|
|
ah[val] = key
|
|
|
}
|
|
|
|
|
|
- newhistfile, _ := os.OpenFile(mktmpfile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
- newhistfile.Truncate(0)
|
|
|
+ // write not empty lines to temp file
|
|
|
+ i = 0
|
|
|
for _, key := range ah {
|
|
|
- newhistfile.WriteString(key + "\n")
|
|
|
+ if len(key) > 0 {
|
|
|
+ t.handle.WriteString(key + "\n")
|
|
|
+ i++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ t.handle.Close()
|
|
|
+
|
|
|
+ //copy from temp file to home
|
|
|
+ _, err := CopyFile(t.path, h.path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ //finnaly remove temp folder
|
|
|
+ err = os.RemoveAll(t.dir)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Print(err)
|
|
|
}
|
|
|
- newhistfile.Sync()
|
|
|
- CopyFile(mktmpfile(), gethistfile())
|
|
|
- defer newhistfile.Close()
|
|
|
+ fmt.Printf("wrote %d lines to %s\n", i, h.path)
|
|
|
}
|