-
Notifications
You must be signed in to change notification settings - Fork 19
/
conf.go
64 lines (52 loc) · 1.07 KB
/
conf.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
package main
import (
"log"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/peterh/liner"
)
func getConfDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
dir := filepath.Join(usr.HomeDir, ".goroutine-inspect")
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
log.Fatal(err)
}
}
return dir
}
func getConfFile() string {
return filepath.Join(getConfDir(), "config")
}
func getHistoryFile() string {
return filepath.Join(getConfDir(), "history")
}
func createLiner() *liner.State {
line := liner.NewLiner()
line.SetCompleter(func(line string) (c []string) {
for n := range commands {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
if f, err := os.Open(getHistoryFile()); err == nil {
line.ReadHistory(f)
f.Close()
}
return line
}
func saveLiner(liner *liner.State) {
f, err := os.Create(getHistoryFile())
if err != nil {
log.Fatal("Error writing history file: ", err)
}
defer f.Close()
liner.WriteHistory(f)
}