mirror of
https://github.com/beego/bee.git
synced 2024-11-22 10:10:53 +00:00
Start both Delve's RPC server and terminal (as client)
This commit is contained in:
parent
36436c0f53
commit
427541fcd9
@ -18,16 +18,18 @@ package dlv
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/beego/bee/cmd/commands"
|
"github.com/beego/bee/cmd/commands"
|
||||||
"github.com/beego/bee/cmd/commands/version"
|
"github.com/beego/bee/cmd/commands/version"
|
||||||
beeLogger "github.com/beego/bee/logger"
|
beeLogger "github.com/beego/bee/logger"
|
||||||
|
"github.com/beego/bee/utils"
|
||||||
"github.com/derekparker/delve/pkg/terminal"
|
"github.com/derekparker/delve/pkg/terminal"
|
||||||
"github.com/derekparker/delve/service"
|
"github.com/derekparker/delve/service"
|
||||||
"github.com/derekparker/delve/service/rpc2"
|
"github.com/derekparker/delve/service/rpc2"
|
||||||
|
"github.com/derekparker/delve/service/rpccommon"
|
||||||
dlvConfig "github.com/derekparker/delve/pkg/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdDlv = &commands.Command{
|
var cmdDlv = &commands.Command{
|
||||||
@ -46,12 +48,12 @@ var cmdDlv = &commands.Command{
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
packageName string
|
packageName string
|
||||||
port string
|
port int
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
fs := flag.NewFlagSet("dlv", flag.ContinueOnError)
|
fs := flag.NewFlagSet("dlv", flag.ContinueOnError)
|
||||||
fs.StringVar(&port, "port", "8181", "Port to listen to for clients")
|
fs.IntVar(&port, "port", 8181, "Port to listen to for clients")
|
||||||
fs.StringVar(&packageName, "package", "", "The package to debug (Must have a main package)")
|
fs.StringVar(&packageName, "package", "", "The package to debug (Must have a main package)")
|
||||||
cmdDlv.Flag = *fs
|
cmdDlv.Flag = *fs
|
||||||
commands.AvailableCommands = append(commands.AvailableCommands, cmdDlv)
|
commands.AvailableCommands = append(commands.AvailableCommands, cmdDlv)
|
||||||
@ -61,49 +63,69 @@ func runDlv(cmd *commands.Command, args []string) int {
|
|||||||
if err := cmd.Flag.Parse(args); err != nil {
|
if err := cmd.Flag.Parse(args); err != nil {
|
||||||
beeLogger.Log.Fatalf("Error parsing flags: %v", err.Error())
|
beeLogger.Log.Fatalf("Error parsing flags: %v", err.Error())
|
||||||
}
|
}
|
||||||
addr := fmt.Sprintf("127.0.0.1:%s", port)
|
|
||||||
|
|
||||||
startChan := make(chan bool)
|
debugname := "debug"
|
||||||
defer close(startChan)
|
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
||||||
|
return runDelve(addr, debugname)
|
||||||
|
}
|
||||||
|
|
||||||
go runDelve(addr, startChan)
|
// runDelve runs the Delve debugger server
|
||||||
|
func runDelve(addr, debugname string) int {
|
||||||
if started := <-startChan; started {
|
|
||||||
beeLogger.Log.Info("Starting Delve Debugger...")
|
beeLogger.Log.Info("Starting Delve Debugger...")
|
||||||
status, err := startRepl(addr)
|
|
||||||
|
err := utils.GoBuild(debugname, packageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beeLogger.Log.Fatal(err.Error())
|
beeLogger.Log.Fatalf("%v", err)
|
||||||
}
|
|
||||||
return status
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// runDelve runs the Delve debugger in API mode
|
fp, err := filepath.Abs("./" + debugname)
|
||||||
func runDelve(addr string, c chan bool) {
|
if err != nil {
|
||||||
args := []string{
|
beeLogger.Log.Fatalf("%v", err)
|
||||||
"debug",
|
|
||||||
"--headless",
|
|
||||||
"--accept-multiclient=true",
|
|
||||||
"--api-version=2",
|
|
||||||
fmt.Sprintf("--listen=%s", addr),
|
|
||||||
}
|
|
||||||
if err := exec.Command("dlv", args...).Start(); err == nil {
|
|
||||||
c <- true
|
|
||||||
}
|
}
|
||||||
|
defer os.Remove(fp)
|
||||||
|
|
||||||
|
abs, err := filepath.Abs(debugname)
|
||||||
|
if err != nil {
|
||||||
|
beeLogger.Log.Fatalf("%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// startRepl starts the Delve REPL
|
//
|
||||||
func startRepl(addr string) (int, error) {
|
// Create and start the debugger server
|
||||||
var client service.Client
|
//
|
||||||
client = rpc2.NewClient(addr)
|
listener, err := net.Listen("tcp", addr)
|
||||||
term := terminal.New(client, dlvConfig.LoadConfig())
|
if err != nil {
|
||||||
|
beeLogger.Log.Fatalf("Could not start listener: %s", err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
server := rpccommon.NewServer(&service.Config{
|
||||||
|
Listener: listener,
|
||||||
|
AcceptMulti: true,
|
||||||
|
AttachPid: 0,
|
||||||
|
APIVersion: 2,
|
||||||
|
WorkingDir: "./",
|
||||||
|
ProcessArgs: []string{abs},
|
||||||
|
}, false)
|
||||||
|
if err := server.Run(); err != nil {
|
||||||
|
beeLogger.Log.Fatalf("Could not start debugger server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start the Delve client REPL
|
||||||
|
//
|
||||||
|
client := rpc2.NewClient(addr)
|
||||||
|
term := terminal.New(client, nil)
|
||||||
|
|
||||||
status, err := term.Run()
|
status, err := term.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return status, err
|
beeLogger.Log.Fatalf("Could not start Delve REPL: %v", err)
|
||||||
}
|
}
|
||||||
defer term.Close()
|
defer term.Close()
|
||||||
|
|
||||||
return 0, nil
|
// Stop and kill the debugger server once
|
||||||
|
// user quits the REPL
|
||||||
|
if err := server.Stop(true); err != nil {
|
||||||
|
beeLogger.Log.Fatalf("Could not stop Delve server: %v", err)
|
||||||
|
}
|
||||||
|
return status
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user