terminal.go 3.3 KB
Newer Older
D
Dan Mace 已提交
1 2 3 4 5 6 7
package terminal

import (
	"fmt"
	"io"
	"os"
	"os/signal"
8 9
	"os/user"
	"path"
D
Dan Mace 已提交
10 11 12 13 14 15 16 17 18
	"strings"

	"github.com/peterh/liner"
	sys "golang.org/x/sys/unix"

	"github.com/derekparker/delve/proctl"
	"github.com/derekparker/delve/service"
)

19
const configDir string = ".dlv"
D
Dan Mace 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
const historyFile string = ".dbg_history"

type Term struct {
	client service.Client
	prompt string
	line   *liner.State
}

func New(client service.Client) *Term {
	return &Term{
		prompt: "(dlv) ",
		line:   liner.NewLiner(),
		client: client,
	}
}

func (t *Term) Run() (error, int) {
	defer t.line.Close()

39 40 41 42 43
	err := createConfigPath()
	if err != nil {
		fmt.Printf("Could not create config directory: %v.")
	}

D
Dan Mace 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
	// Send the debugger a halt command on SIGINT
	ch := make(chan os.Signal)
	signal.Notify(ch, sys.SIGINT)
	go func() {
		for range ch {
			_, err := t.client.Halt()
			if err != nil {
				fmt.Println(err)
			}
		}
	}()

	cmds := DebugCommands(t.client)
57
	fullHistoryFile, err := getConfigFilePath(historyFile)
D
Dan Mace 已提交
58
	if err != nil {
59
		fmt.Printf("Unable to load history file: %v.", err)
D
Dan Mace 已提交
60
	}
61 62 63 64 65 66 67 68 69

	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)
		}
	}

D
Dan Mace 已提交
70 71 72 73 74 75 76 77 78 79 80 81
	t.line.ReadHistory(f)
	f.Close()
	fmt.Println("Type 'help' for list of commands.")

	var status int

	for {
		cmdstr, err := t.promptForInput()
		if err != nil {
			if err == io.EOF {
				err, status = handleExit(t.client, t)
			}
D
Derek Parker 已提交
82
			err, status = fmt.Errorf("Prompt for input failed.\n"), 1
D
Dan Mace 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
			break
		}

		cmdstr, args := parseCommand(cmdstr)
		if cmdstr == "exit" {
			err, status = handleExit(t.client, t)
			break
		}

		cmd := cmds.Find(cmdstr)
		if err := cmd(t.client, args...); err != nil {
			switch err.(type) {
			case proctl.ProcessExitedError:
				pe := err.(proctl.ProcessExitedError)
				fmt.Fprintf(os.Stderr, "Process exited with status %d\n", pe.Status)
			default:
				fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
			}
		}
	}

	return nil, status
}

D
Derek Parker 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
func (t *Term) promptForInput() (string, error) {
	l, err := t.line.Prompt(t.prompt)
	if err != nil {
		return "", err
	}

	l = strings.TrimSuffix(l, "\n")
	if l != "" {
		t.line.AppendHistory(l)
	}

	return l, nil
}

D
Dan Mace 已提交
121
func handleExit(client service.Client, t *Term) (error, int) {
122 123 124 125 126 127 128 129 130 131
	fullHistoryFile, err := getConfigFilePath(historyFile)
	if err != nil {
		fmt.Println("Error saving history file:", 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()
D
Dan Mace 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		}
	}

	answer, err := t.line.Prompt("Would you like to kill the process? [y/n] ")
	if err != nil {
		return io.EOF, 2
	}
	answer = strings.TrimSuffix(answer, "\n")

	kill := (answer == "y")
	err = client.Detach(kill)
	if err != nil {
		return err, 1
	}
	return nil, 0
}

func parseCommand(cmdstr string) (string, []string) {
	vals := strings.Split(cmdstr, " ")
	return vals[0], vals[1:]
}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

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
}