-
Notifications
You must be signed in to change notification settings - Fork 6
/
linehistory.go
55 lines (44 loc) · 1.06 KB
/
linehistory.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
package pcsliner
import (
"fmt"
"os"
)
// LineHistory 命令行历史
type LineHistory struct {
historyFilePath string
historyFile *os.File
}
// NewLineHistory 设置历史
func NewLineHistory(filePath string) (lh *LineHistory, err error) {
lh = &LineHistory{
historyFilePath: filePath,
}
lh.historyFile, err = os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
return lh, nil
}
// DoWriteHistory 执行写入历史
func (pl *PCSLiner) DoWriteHistory() (err error) {
if pl.History == nil {
return fmt.Errorf("history not set")
}
pl.History.historyFile, err = os.Create(pl.History.historyFilePath)
if err != nil {
return fmt.Errorf("写入历史错误, %s", err)
}
_, err = pl.State.WriteHistory(pl.History.historyFile)
if err != nil {
return fmt.Errorf("写入历史错误: %s", err)
}
return nil
}
// ReadHistory 读取历史
func (pl *PCSLiner) ReadHistory() (err error) {
if pl.History == nil {
return fmt.Errorf("history not set")
}
_, err = pl.State.ReadHistory(pl.History.historyFile)
return err
}