提交 0358c174 编写于 作者: E epipho 提交者: Derek Parker

Moving history file to .dlv config directory

上级 471615fb
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
"io" "io"
"os" "os"
"os/signal" "os/signal"
"os/user"
"path"
"strings" "strings"
"github.com/peterh/liner" "github.com/peterh/liner"
...@@ -15,6 +17,7 @@ import ( ...@@ -15,6 +17,7 @@ import (
"github.com/derekparker/delve/service" "github.com/derekparker/delve/service"
) )
const configDir string = ".dlv"
const historyFile string = ".dbg_history" const historyFile string = ".dbg_history"
type Term struct { type Term struct {
...@@ -34,6 +37,11 @@ func New(client service.Client) *Term { ...@@ -34,6 +37,11 @@ func New(client service.Client) *Term {
func (t *Term) Run() (error, int) { func (t *Term) Run() (error, int) {
defer t.line.Close() defer t.line.Close()
err := createConfigPath()
if err != nil {
fmt.Printf("Could not create config directory: %v.")
}
// Send the debugger a halt command on SIGINT // Send the debugger a halt command on SIGINT
ch := make(chan os.Signal) ch := make(chan os.Signal)
signal.Notify(ch, sys.SIGINT) signal.Notify(ch, sys.SIGINT)
...@@ -47,10 +55,19 @@ func (t *Term) Run() (error, int) { ...@@ -47,10 +55,19 @@ func (t *Term) Run() (error, int) {
}() }()
cmds := DebugCommands(t.client) cmds := DebugCommands(t.client)
f, err := os.Open(historyFile) fullHistoryFile, err := getConfigFilePath(historyFile)
if err != nil { if err != nil {
f, _ = os.Create(historyFile) fmt.Printf("Unable to load history file: %v.", err)
} }
f, err := os.Open(fullHistoryFile)
if err != nil {
f, err = os.Create(fullHistoryFile)
if err != nil {
fmt.Printf("Unable to open history file: %v. History will not be saved for this session.", err)
}
}
t.line.ReadHistory(f) t.line.ReadHistory(f)
f.Close() f.Close()
fmt.Println("Type 'help' for list of commands.") fmt.Println("Type 'help' for list of commands.")
...@@ -103,12 +120,17 @@ func (t *Term) promptForInput() (string, error) { ...@@ -103,12 +120,17 @@ func (t *Term) promptForInput() (string, error) {
} }
func handleExit(client service.Client, t *Term) (error, int) { func handleExit(client service.Client, t *Term) (error, int) {
if f, err := os.OpenFile(historyFile, os.O_RDWR, 0666); err == nil { fullHistoryFile, err := getConfigFilePath(historyFile)
_, err := t.line.WriteHistory(f) if err != nil {
if err != nil { fmt.Println("Error saving history file:", err)
fmt.Println("readline history error: ", err) } else {
if f, err := os.OpenFile(fullHistoryFile, os.O_RDWR, 0666); err == nil {
_, err := t.line.WriteHistory(f)
if err != nil {
fmt.Println("readline history error: ", err)
}
f.Close()
} }
f.Close()
} }
answer, err := t.line.Prompt("Would you like to kill the process? [y/n] ") answer, err := t.line.Prompt("Would you like to kill the process? [y/n] ")
...@@ -129,3 +151,19 @@ func parseCommand(cmdstr string) (string, []string) { ...@@ -129,3 +151,19 @@ func parseCommand(cmdstr string) (string, []string) {
vals := strings.Split(cmdstr, " ") vals := strings.Split(cmdstr, " ")
return vals[0], vals[1:] return vals[0], vals[1:]
} }
func createConfigPath() error {
path, err := getConfigFilePath("")
if err != nil {
return err
}
return os.MkdirAll(path, 0700)
}
func getConfigFilePath(file string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return path.Join(usr.HomeDir, configDir, file), nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册