diff --git a/cmd/build.go b/cmd/build.go index 076c2a5..1791313 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -43,14 +43,18 @@ goc build -- -o /to/this/path goc build -- -ldflags "-extldflags -static" -tags="embed kodo" `, Run: func(cmd *cobra.Command, args []string) { - gocbuild := build.NewBuild(buildFlags, packages, buildOutput) + gocBuild := build.NewBuild(buildFlags, packages, buildOutput) // remove temporary directory if needed - defer gocbuild.RemoveTmpDir() + defer func() { + if !debugGoc { + gocBuild.Clean() + } + }() // doCover with original buildFlags, with new GOPATH( tmp:original ) // in the tmp directory - doCover(buildFlags, gocbuild.NewGOPATH, gocbuild.TmpDir) + doCover(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir) // do install in the temporary directory - gocbuild.Build() + gocBuild.Build() return }, } diff --git a/cmd/install.go b/cmd/install.go index d30d0a7..08659e2 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -27,7 +27,7 @@ var installCmd = &cobra.Command{ Long: ` First of all, this install command will copy the project code and its necessary dependencies to a temporary directory, then do cover for the target in this temporary directory, finally go install command will be executed and binaries generated to their original place. -To pass origial go build flags to goc command, place them after "--", see examples below for reference. +To pass original go build flags to goc command, place them after "--", see examples below for reference. `, Example: ` # Install all binaries with cover variables injected. The binary will be installed in $GOPATH/bin or $HOME/go/bin if directory existed. @@ -40,14 +40,18 @@ goc install --center=http://127.0.0.1:7777 goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'" `, Run: func(cmd *cobra.Command, args []string) { - gocbuild := build.NewInstall(buildFlags, packages) + gocBuild := build.NewInstall(buildFlags, packages) // remove temporary directory if needed - defer gocbuild.RemoveTmpDir() + defer func() { + if !debugGoc { + gocBuild.Clean() + } + }() // doCover with original buildFlags, with new GOPATH( tmp:original ) // in the tmp directory - doCover(buildFlags, gocbuild.NewGOPATH, gocbuild.TmpDir) + doCover(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir) // do install in the temporary directory - gocbuild.Install() + gocBuild.Install() }, } diff --git a/cmd/root.go b/cmd/root.go index 3974e4c..b0d50d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,13 +17,14 @@ package cmd import ( - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/viper" "io/ioutil" "path/filepath" "runtime" "strconv" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) var rootCmd = &cobra.Command{ @@ -54,8 +55,7 @@ Find more information at: } func init() { - rootCmd.PersistentFlags().BoolVar(&debugGoc, "debuggoc", false, "turn goc into debug mode") - rootCmd.PersistentFlags().MarkHidden("debuggoc") + rootCmd.PersistentFlags().BoolVar(&debugGoc, "debug", false, "run goc in debug mode") viper.BindPFlags(rootCmd.PersistentFlags()) } diff --git a/pkg/build/build.go b/pkg/build/build.go index d40ea88..854461b 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -87,7 +87,7 @@ func (b *Build) determineOutputDir(outputDir string) string { if err != nil { log.Fatalf("Cannot get current working directory, the err: %v.", err) } - // if + if outputDir == "" { _, last := filepath.Split(curWorkingDir) if b.IsMod { diff --git a/pkg/build/tmpfolder.go b/pkg/build/tmpfolder.go index b4b5a6a..aff30bd 100644 --- a/pkg/build/tmpfolder.go +++ b/pkg/build/tmpfolder.go @@ -19,12 +19,12 @@ package build import ( "crypto/sha256" "fmt" - log "github.com/sirupsen/logrus" - "github.com/spf13/viper" "os" "path/filepath" "strings" + log "github.com/sirupsen/logrus" + "github.com/qiniu/goc/pkg/cover" ) @@ -57,7 +57,7 @@ func (b *Build) MvProjectsToTmp() { func (b *Build) mvProjectsToTmp() { path, err := os.Getwd() if err != nil { - log.Fatalf("Cannot get current working directoy, the error is: %v", err) + log.Fatalf("Cannot get current working directory, the error is: %v", err) } b.TmpDir = filepath.Join(os.TempDir(), TmpFolderName(path)) @@ -70,7 +70,7 @@ func (b *Build) mvProjectsToTmp() { } log.Printf("Tmp project generated in: %v", b.TmpDir) - // set Build.IsMod flag, so we dont have to call checkIfLegacyProject another time + // set Build.IsMod flag, so we don't have to call checkIfLegacyProject another time if b.checkIfLegacyProject() { b.cpLegacyProject() } else { @@ -154,11 +154,7 @@ func (b *Build) findWhereToInstall() string { return filepath.Join(os.Getenv("HOME"), "go", "bin") } -func (b *Build) RemoveTmpDir() { - debuggoc := viper.GetBool("debuggoc") - if debuggoc == false { - if b.TmpDir != "" { - os.RemoveAll(b.TmpDir) - } - } +// Clean clears up the temporary workspace +func (b *Build) Clean() error { + return os.RemoveAll(b.TmpDir) } diff --git a/tests/e2e/simple_project_test.go b/tests/e2e/simple_project_test.go index 370838f..139d05a 100644 --- a/tests/e2e/simple_project_test.go +++ b/tests/e2e/simple_project_test.go @@ -20,11 +20,10 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "strings" "time" - "path/filepath" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/qiniu/goc/pkg/build" @@ -55,7 +54,7 @@ var _ = Describe("E2E", func() { By("goc build") testProjDir := filepath.Join(TESTS_ROOT, "samples/simple_project") - cmd := exec.Command("goc", "build", "--debuggoc") + cmd := exec.Command("goc", "build", "--debug") cmd.Dir = testProjDir out, err := cmd.CombinedOutput() @@ -63,7 +62,7 @@ var _ = Describe("E2E", func() { By("goc install") testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_project") - cmd = exec.Command("goc", "install", "--debuggoc") + cmd = exec.Command("goc", "install", "--debug") cmd.Dir = testProjDir out, err = cmd.CombinedOutput() @@ -114,7 +113,7 @@ var _ = Describe("E2E", func() { GOPATH = testProjDir By("goc build") - cmd := exec.Command("goc", "build", "--debuggoc") + cmd := exec.Command("goc", "build", "--debug") cmd.Dir = oriWorkingDir // use GOPATH mode to compile project cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", GOPATH), "GO111MODULE=off") @@ -125,7 +124,7 @@ var _ = Describe("E2E", func() { By("goc install") testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_gopath_project") - cmd = exec.Command("goc", "install", "--debuggoc") + cmd = exec.Command("goc", "install", "--debug") cmd.Dir = filepath.Join(testProjDir, "src/qiniu.com/simple_gopath_project") // use GOPATH mode to compile project cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", testProjDir), "GO111MODULE=off") @@ -157,7 +156,7 @@ var _ = Describe("E2E", func() { Expect(err).To(BeNil(), "the binary cannot be disassembled") cnt := strings.Count(string(out), "GoCover") - Expect(cnt).To(BeNumerically(">", 0), "GoCover varibale should be in the binary") + Expect(cnt).To(BeNumerically(">", 0), "GoCover variable should be in the binary") cnt = strings.Count(string(out), "main.registerSelf") Expect(cnt).To(BeNumerically(">", 0), "main.registerSelf function should be in the binary")