forked from mborho/rem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
119 lines (106 loc) · 2.6 KB
/
Copy pathfile.go
File metadata and controls
119 lines (106 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// rem - A tool to remember things on the command line.
// Copyright (C) 2015 Martin Borho (martin@borho.net)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
_ "fmt"
"os"
"os/user"
"path"
)
type File struct {
filepath string
filename string
file *os.File
global bool
}
func (f *File) clearFile() error {
return os.Remove(f.filepath)
}
func (f *File) Close() {
f.file.Close()
}
func (f *File) setFile(appendTo bool) error {
// which mode to use to open file
var openFlags int
if appendTo {
openFlags = os.O_CREATE | os.O_APPEND | os.O_WRONLY
} else {
openFlags = os.O_CREATE | os.O_RDONLY
}
// open history file
file, err := os.OpenFile(f.filepath, openFlags, 0600)
if err == nil {
f.file = file
}
return nil
}
func (f *File) createLocalFile() error {
// Create history file in the current directory.
dir, err := os.Getwd()
if err != nil {
return err
}
localFile := path.Join(dir, f.filename)
f.file, err = os.OpenFile(localFile, os.O_CREATE, 0600)
defer f.file.Close()
if err != nil {
return err
}
f.filepath = localFile
return nil
}
func (f *File) checkPath(dir string) bool {
// Checks if rem file exists in given directory.
checkFile := path.Join(dir, f.filename)
if _, err := os.Stat(checkFile); err == nil {
return true
}
return false
}
func (f *File) setPath() error {
// ignore current dir if global .rem file is wanted
if f.global == false {
// Set path to history file in current dir if one exists
dir, err := os.Getwd()
if err != nil {
return err
}
for {
// traverse through dir path
localFile := path.Join(dir, f.filename)
if f.checkPath(dir) {
f.filepath = localFile
return nil
}
if dir == "/" {
break
}
dir = path.Dir(dir)
}
}
// Set default path to rem's history file
usr, err := user.Current()
if err == nil {
f.filepath = path.Join(usr.HomeDir, f.filename)
}
return err
}
func (f *File) write(line string) error {
if _, err := f.file.WriteString(line); err != nil {
panic(err)
}
return nil
}