-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
125 lines (118 loc) · 2.59 KB
/
config.go
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
120
121
122
123
124
125
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"
)
// configDir is where config files will saved.
// It is $HOME/.config/tor
var configDir string
func init() {
u, err := user.Current()
if err != nil {
panic(err)
}
configDir = path.Join(u.HomeDir, ".config", "tor")
if err := os.MkdirAll(configDir, 0755); err != nil && !os.IsExist(err) {
panic(err)
}
}
// saveLastPosition saves a cursor position to
// the 'lastpos' config file.
// Each line is formatted as {filepath}:{line}:{offset}
func saveLastPosition(pth string, l, b int) error {
abspath, err := filepath.Abs(pth)
if err != nil {
return err
}
f := path.Join(configDir, "lastpos")
if _, err = os.Stat(f); os.IsNotExist(err) {
os.Create(f)
}
input, err := ioutil.ReadFile(f)
if err != nil {
return err
}
lines := strings.Split(string(input), "\n")
find := false
for i, ln := range lines {
if strings.Contains(ln, abspath+":") {
find = true
lines[i] = fmt.Sprintf("%v:%v:%v", abspath, l, b)
}
}
if !find {
lines = append(lines, fmt.Sprintf("%v:%v:%v", abspath, l, b))
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(f, []byte(output), 0644)
if err != nil {
return err
}
return nil
}
// loadLastPosition loads a cursor position from
// the 'lastpos' config file.
// If there is no information about the file in 'lastpos',
// it will return 0, 0.
func loadLastPosition(pth string) (int, int) {
abspath, err := filepath.Abs(pth)
if err != nil {
return 0, 0
}
u, err := user.Current()
if err != nil {
return 0, 0
}
f := path.Join(u.HomeDir, ".config", "tor", "lastpos")
input, err := ioutil.ReadFile(f)
if err != nil {
return 0, 0
}
find := false
findline := ""
lines := strings.Split(string(input), "\n")
for _, ln := range lines {
if strings.Contains(ln, abspath+":") {
find = true
findline = ln
}
}
if !find {
return 0, 0
}
tokens := strings.Split(findline, ":")
if len(tokens) != 3 {
return 0, 0
}
l, err := strconv.Atoi(tokens[1])
if err != nil {
return 0, 0
}
b, err := strconv.Atoi(tokens[2])
if err != nil {
return 0, 0
}
return l, b
}
// saveConfig saves a string to ~/.config/tor/{fname} file.
// It will return error if exists.
func saveConfig(fname, s string) error {
f := path.Join(configDir, fname)
return ioutil.WriteFile(f, []byte(s), 0644)
}
// loadConfig loads a string from ~/.config/tor/{fname} file.
// On any error, it will return empty string.
func loadConfig(fname string) string {
f := path.Join(configDir, fname)
b, err := ioutil.ReadFile(f)
if err != nil {
return ""
}
return string(b)
}