From 6865b1eab50ac1287510f5b47a1db8e7c35564e6 Mon Sep 17 00:00:00 2001 From: Rene Zbinden Date: Sat, 16 Sep 2017 00:03:29 +0200 Subject: [PATCH] keep trace and profile files when go tool chain is not available (#52) --- cmd.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cmd.go b/cmd.go index da36db3..6290f65 100644 --- a/cmd.go +++ b/cmd.go @@ -65,11 +65,15 @@ func trace(addr net.TCPAddr) error { if err != nil { return err } - defer os.Remove(tmpfile.Name()) if err := ioutil.WriteFile(tmpfile.Name(), out, 0); err != nil { return err } fmt.Printf("Trace dump saved to: %s\n", tmpfile.Name()) + // If go tool chain not found, stopping here and keep trace file. + if _, err := exec.LookPath("go"); err != nil { + return nil + } + defer os.Remove(tmpfile.Name()) cmd := exec.Command("go", "tool", "trace", tmpfile.Name()) cmd.Env = os.Environ() cmd.Stdin = os.Stdin @@ -92,10 +96,15 @@ func pprof(addr net.TCPAddr, p byte) error { if len(out) == 0 { return errors.New("failed to read the profile") } - defer os.Remove(tmpDumpFile.Name()) if err := ioutil.WriteFile(tmpDumpFile.Name(), out, 0); err != nil { return err } + fmt.Printf("Profile dump saved to: %s\n", tmpDumpFile.Name()) + // If go tool chain not found, stopping here and keep dump file. + if _, err := exec.LookPath("go"); err != nil { + return nil + } + defer os.Remove(tmpDumpFile.Name()) } // Download running binary tmpBinFile, err := ioutil.TempFile("", "binary")