Browse Source

Utils improvement ;)

Merging temp and home files structures into a single.
Adding comments and chaning file init function.
admin 10 years ago
parent
commit
ecece57a76
2 changed files with 60 additions and 40 deletions
  1. 8 8
      main.go
  2. 52 32
      utils.go

+ 8 - 8
main.go

@@ -10,19 +10,19 @@ import (
 // map for old history records
 var history = make(map[string]int)
 
-// path ${HOME}/.bash_history
-var h Hfile
+// file 'h': ${HOME}/.bash_history
+// file 't': /tmp/.bash_history******/.bash_history
+var h, t file
 
-// path /tmp/.bash_history******/.bash_history
-var t Tmpfile
+// Set constant file name
+// Why constant? Because it will never be changed due to program purpose.
+const fileName string = ".bash_history"
 
 func init() {
-	h.name = ".bash_history"
-	h.init()
+	h.init(fileName, locHome)
 	h.Open()
 
-	t.name = h.name
-	t.init()
+	t.init(fileName, locTemp)
 	t.Open()
 }
 

+ 52 - 32
utils.go

@@ -10,54 +10,74 @@ import (
 	"path"
 )
 
-type Hfile struct {
-	name   string
-	dir    string
-	path   string
-	handle *os.File
-}
+// File locations enum
+type fileLoc uint8
+
+const (
+	locHome fileLoc = 0 // in user home dir
+	locTemp fileLoc = 1 // in temp dir
+)
 
-type Tmpfile struct {
-	path   string
-	dir    string
-	name   string
-	handle *os.File
+// File stucture
+type file struct {
+	path, dir, name string
+	floc            fileLoc
+	handle          *os.File
 }
 
-func (f *Hfile) init() {
-	u, err := user.Current()
-	if err != nil {
-		fmt.Println("Panicking!")
-		panic(fmt.Sprintf("%v", err))
+func (f *file) init(fname string, loc fileLoc) {
+
+	//	define outside switch scope
+	var dir string
+	var err error
+
+	// Set file name
+	f.name = fname
+
+	// Using switch (in case there's going ever to be more file types)
+	switch loc {
+	case locHome:
+		var u *user.User
+		u, err = user.Current()
+		dir = u.HomeDir
+	case locTemp:
+		dir, err = ioutil.TempDir(os.TempDir(), f.name)
 	}
-	f.dir = u.HomeDir
-	f.path = path.Join(f.dir, f.name)
-}
 
-func (f *Tmpfile) init() {
-	tmpdir, err := ioutil.TempDir(os.TempDir(), f.name)
+	// Check for error
 	if err != nil {
 		fmt.Println("Panicking!")
 		panic(fmt.Sprintf("%v", err))
 	}
-	f.dir = tmpdir
+
+	// Set necessary data
+	f.floc = loc
+	f.dir = dir
 	f.path = path.Join(f.dir, f.name)
+
 }
 
-func (h *Hfile) Open() {
-	fh, err := os.Open(h.path)
-	if err != nil {
-		fmt.Print(err)
+func (f *file) Open() {
+
+	//	define outside switch scope
+	var fh *os.File
+	var err error
+
+	// Different permissions for different locations
+	switch f.floc {
+	case locHome:
+		fh, err = os.Open(h.path)
+	case locTemp:
+		fh, err = os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
 	}
-	h.handle = fh
-}
 
-func (h *Tmpfile) Open() {
-	fh, err := os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
+	// Check for error
 	if err != nil {
-		fmt.Print(err)
+		fmt.Println(err)
 	}
-	h.handle = fh
+
+	// Set handle
+	f.handle = fh
 }
 
 func CopyFile(src, dst string) (int64, error) {