terminal: Implements init file and source command

The 'source' command reads the file specified as argument and executes
it as a list of delve commands.
Additionally a flag '--init' can be passed to delve specifying a file
containing a list of commands to execute on startup.

Issue #96
This commit is contained in:
aarzilli
2015-09-29 18:40:12 +02:00
committed by Derek Parker
parent bc9ac0ec78
commit eb2bc2a7ee
6 changed files with 107 additions and 7 deletions

View File

@ -41,9 +41,9 @@ func (c command) match(cmdstr string) bool {
}
type Commands struct {
cmds []command
lastCmd cmdfunc
client service.Client
cmds []command
lastCmd cmdfunc
client service.Client
}
// Returns a Commands struct with default commands defined.
@ -77,6 +77,7 @@ func DebugCommands(client service.Client) *Commands {
{aliases: []string{"list", "ls"}, cmdFn: listCommand, helpMsg: "list <linespec>. Show source around current point or provided linespec."},
{aliases: []string{"stack", "bt"}, cmdFn: stackCommand, helpMsg: "stack [<depth>] [-full]. Prints stack."},
{aliases: []string{"frame"}, cmdFn: frame, helpMsg: "Sets current stack frame (0 is the top of the stack)"},
{aliases: []string{"source"}, cmdFn: c.sourceCommand, helpMsg: "Executes a file containing a list of delve commands"},
}
return c
@ -699,6 +700,14 @@ func listCommand(t *Term, args ...string) error {
return nil
}
func (cmds *Commands) sourceCommand(t *Term, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("wrong number of arguments: source <filename>")
}
return cmds.executeFile(t, args[0])
}
func digits(n int) int {
return int(math.Floor(math.Log10(float64(n)))) + 1
}
@ -826,3 +835,32 @@ func shortenFilePath(fullPath string) string {
workingDir, _ := os.Getwd()
return strings.Replace(fullPath, workingDir, ".", 1)
}
func (cmds *Commands) executeFile(t *Term, name string) error {
fh, err := os.Open(name)
if err != nil {
return err
}
defer fh.Close()
scanner := bufio.NewScanner(fh)
lineno := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
lineno++
if line == "" || line[0] == '#' {
continue
}
cmdstr, args := parseCommand(line)
cmd := cmds.Find(cmdstr)
err := cmd(t, args...)
if err != nil {
fmt.Printf("%s:%d: %v\n", name, lineno, err)
}
}
return scanner.Err()
}