Prefer anonymous functions with return status for defers

This commit is contained in:
Derek Parker 2015-07-11 14:51:54 -05:00
parent f848eb0d14
commit c6ca18ff07

@ -59,22 +59,24 @@ The goal of this tool is to provide a simple yet powerful interface for debuggin
Long: `Compiles your program with optimizations disabled, Long: `Compiles your program with optimizations disabled,
starts and attaches to it, and enables you to immediately begin debugging your program.`, starts and attaches to it, and enables you to immediately begin debugging your program.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
status := func() int {
const debugname = "debug" const debugname = "debug"
goBuild := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l") goBuild := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l")
goBuild.Stderr = os.Stderr goBuild.Stderr = os.Stderr
err := goBuild.Run() err := goBuild.Run()
if err != nil { if err != nil {
os.Exit(1) return 1
} }
fp, err := filepath.Abs("./" + debugname) fp, err := filepath.Abs("./" + debugname)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) return 1
} }
defer os.Remove(fp)
processArgs := append([]string{"./" + debugname}, args...) processArgs := append([]string{"./" + debugname}, args...)
status := execute(0, processArgs) return execute(0, processArgs)
os.Remove(fp) }()
os.Exit(status) os.Exit(status)
}, },
} }
@ -87,23 +89,25 @@ starts and attaches to it, and enables you to immediately begin debugging your p
Long: `Compiles a test binary with optimizations disabled, Long: `Compiles a test binary with optimizations disabled,
starts and attaches to it, and enable you to immediately begin debugging your program.`, starts and attaches to it, and enable you to immediately begin debugging your program.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
status := func() int {
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) return 1
} }
base := filepath.Base(wd) base := filepath.Base(wd)
goTest := exec.Command("go", "test", "-c", "-gcflags", "-N -l") goTest := exec.Command("go", "test", "-c", "-gcflags", "-N -l")
goTest.Stderr = os.Stderr goTest.Stderr = os.Stderr
err = goTest.Run() err = goTest.Run()
if err != nil { if err != nil {
os.Exit(1) return 1
} }
debugname := "./" + base + ".test" debugname := "./" + base + ".test"
defer os.Remove(debugname)
processArgs := append([]string{debugname}, args...) processArgs := append([]string{debugname}, args...)
status := execute(0, processArgs) return execute(0, processArgs)
os.Remove(debugname) }()
os.Exit(status) os.Exit(status)
}, },
} }
@ -138,8 +142,7 @@ func execute(attachPid int, processArgs []string) int {
defer listener.Close() defer listener.Close()
// Create and start a debugger server // Create and start a debugger server
var server service.Server server := rpc.NewServer(&service.Config{
server = rpc.NewServer(&service.Config{
Listener: listener, Listener: listener,
ProcessArgs: processArgs, ProcessArgs: processArgs,
AttachPid: attachPid, AttachPid: attachPid,