Edvinas Valatka hace 10 años
padre
commit
63fff7d0fb
Se han modificado 2 ficheros con 84 adiciones y 25 borrados
  1. 37 16
      main.go
  2. 47 9
      utils.go

+ 37 - 16
main.go

@@ -2,39 +2,60 @@ package main
 
 import (
 	"bufio"
+	"fmt"
 	"os"
 	"strings"
 )
 
 var history = make(map[string]int)
 
+var h Hfile
+
+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)
+	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++
 	}
-
-	defer histfile.Close()
+	delete(history, "")
+	h.handle.Close()
 
 	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)
+	i = 0
 	for _, key := range ah {
-		newhistfile.WriteString(key + "\n")
+		if len(key) > 0 {
+			t.handle.WriteString(key + "\n")
+			i++
+		}
+	}
+	t.handle.Close()
+
+	_, err := CopyFile(t.path, h.path)
+	if err != nil {
+		fmt.Println(err)
+	}
+
+	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)
 }

+ 47 - 9
utils.go

@@ -2,25 +2,62 @@
 package main
 
 import (
+	"fmt"
 	"io"
+	"io/ioutil"
 	"os"
 	"os/user"
 	"path"
 )
 
-func gethomedir() string {
-	u, _ := user.Current()
-	return u.HomeDir
+type Hfile struct {
+	name   string
+	dir    string
+	path   string
+	handle *os.File
 }
 
-func gethistfile() string {
-	const fname = ".bash_history"
-	return path.Join(gethomedir(), fname)
+type Tmpfile struct {
+	path   string
+	dir    string
+	name   string
+	handle *os.File
 }
 
-func mktmpfile() string {
-	const fname = ".bash_history"
-	return path.Join("/tmp", fname)
+func (f *Hfile) init() {
+	u, err := user.Current()
+	if err != nil {
+		fmt.Println("Panicking!")
+		panic(fmt.Sprintf("%v", err))
+	}
+	f.dir = u.HomeDir
+	f.path = path.Join(f.dir, f.name)
+}
+
+func (f *Tmpfile) init() {
+	tmpdir, err := ioutil.TempDir(os.TempDir(), f.name)
+	if err != nil {
+		fmt.Println("Panicking!")
+		panic(fmt.Sprintf("%v", err))
+	}
+	f.dir = tmpdir
+	f.path = path.Join(f.dir, f.name)
+}
+
+func (h *Hfile) Open() {
+	fh, err := os.Open(h.path)
+	if err != nil {
+		fmt.Print(err)
+	}
+	h.handle = fh
+}
+
+func (h *Tmpfile) Open() {
+	fh, err := os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
+	if err != nil {
+		fmt.Print(err)
+	}
+	h.handle = fh
 }
 
 func CopyFile(src, dst string) (int64, error) {
@@ -33,6 +70,7 @@ func CopyFile(src, dst string) (int64, error) {
 	if err != nil {
 		return 0, err
 	}
+	df.Chmod(0600)
 	defer df.Close()
 	return io.Copy(df, sf)
 }