show debug flag

This commit is contained in:
jichangjun 2020-06-14 12:43:25 +08:00
parent 3115bead40
commit 346530150c
6 changed files with 36 additions and 33 deletions

View File

@ -43,14 +43,18 @@ goc build -- -o /to/this/path
goc build -- -ldflags "-extldflags -static" -tags="embed kodo" goc build -- -ldflags "-extldflags -static" -tags="embed kodo"
`, `,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
gocbuild := build.NewBuild(buildFlags, packages, buildOutput) gocBuild := build.NewBuild(buildFlags, packages, buildOutput)
// remove temporary directory if needed // remove temporary directory if needed
defer gocbuild.RemoveTmpDir() defer func() {
if !debugGoc {
gocBuild.Clean()
}
}()
// doCover with original buildFlags, with new GOPATH( tmp:original ) // doCover with original buildFlags, with new GOPATH( tmp:original )
// in the tmp directory // in the tmp directory
doCover(buildFlags, gocbuild.NewGOPATH, gocbuild.TmpDir) doCover(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir)
// do install in the temporary directory // do install in the temporary directory
gocbuild.Build() gocBuild.Build()
return return
}, },
} }

View File

@ -27,7 +27,7 @@ var installCmd = &cobra.Command{
Long: ` 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. 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: ` Example: `
# Install all binaries with cover variables injected. The binary will be installed in $GOPATH/bin or $HOME/go/bin if directory existed. # 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'" goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
`, `,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
gocbuild := build.NewInstall(buildFlags, packages) gocBuild := build.NewInstall(buildFlags, packages)
// remove temporary directory if needed // remove temporary directory if needed
defer gocbuild.RemoveTmpDir() defer func() {
if !debugGoc {
gocBuild.Clean()
}
}()
// doCover with original buildFlags, with new GOPATH( tmp:original ) // doCover with original buildFlags, with new GOPATH( tmp:original )
// in the tmp directory // in the tmp directory
doCover(buildFlags, gocbuild.NewGOPATH, gocbuild.TmpDir) doCover(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir)
// do install in the temporary directory // do install in the temporary directory
gocbuild.Install() gocBuild.Install()
}, },
} }

View File

@ -17,13 +17,14 @@
package cmd package cmd
import ( import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -54,8 +55,7 @@ Find more information at:
} }
func init() { func init() {
rootCmd.PersistentFlags().BoolVar(&debugGoc, "debuggoc", false, "turn goc into debug mode") rootCmd.PersistentFlags().BoolVar(&debugGoc, "debug", false, "run goc in debug mode")
rootCmd.PersistentFlags().MarkHidden("debuggoc")
viper.BindPFlags(rootCmd.PersistentFlags()) viper.BindPFlags(rootCmd.PersistentFlags())
} }

View File

@ -87,7 +87,7 @@ func (b *Build) determineOutputDir(outputDir string) string {
if err != nil { if err != nil {
log.Fatalf("Cannot get current working directory, the err: %v.", err) log.Fatalf("Cannot get current working directory, the err: %v.", err)
} }
// if
if outputDir == "" { if outputDir == "" {
_, last := filepath.Split(curWorkingDir) _, last := filepath.Split(curWorkingDir)
if b.IsMod { if b.IsMod {

View File

@ -19,12 +19,12 @@ package build
import ( import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
) )
@ -57,7 +57,7 @@ func (b *Build) MvProjectsToTmp() {
func (b *Build) mvProjectsToTmp() { func (b *Build) mvProjectsToTmp() {
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { 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)) 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) 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() { if b.checkIfLegacyProject() {
b.cpLegacyProject() b.cpLegacyProject()
} else { } else {
@ -154,11 +154,7 @@ func (b *Build) findWhereToInstall() string {
return filepath.Join(os.Getenv("HOME"), "go", "bin") return filepath.Join(os.Getenv("HOME"), "go", "bin")
} }
func (b *Build) RemoveTmpDir() { // Clean clears up the temporary workspace
debuggoc := viper.GetBool("debuggoc") func (b *Build) Clean() error {
if debuggoc == false { return os.RemoveAll(b.TmpDir)
if b.TmpDir != "" {
os.RemoveAll(b.TmpDir)
}
}
} }

View File

@ -20,11 +20,10 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"time" "time"
"path/filepath"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/qiniu/goc/pkg/build" "github.com/qiniu/goc/pkg/build"
@ -55,7 +54,7 @@ var _ = Describe("E2E", func() {
By("goc build") By("goc build")
testProjDir := filepath.Join(TESTS_ROOT, "samples/simple_project") testProjDir := filepath.Join(TESTS_ROOT, "samples/simple_project")
cmd := exec.Command("goc", "build", "--debuggoc") cmd := exec.Command("goc", "build", "--debug")
cmd.Dir = testProjDir cmd.Dir = testProjDir
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
@ -63,7 +62,7 @@ var _ = Describe("E2E", func() {
By("goc install") By("goc install")
testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_project") testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_project")
cmd = exec.Command("goc", "install", "--debuggoc") cmd = exec.Command("goc", "install", "--debug")
cmd.Dir = testProjDir cmd.Dir = testProjDir
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
@ -114,7 +113,7 @@ var _ = Describe("E2E", func() {
GOPATH = testProjDir GOPATH = testProjDir
By("goc build") By("goc build")
cmd := exec.Command("goc", "build", "--debuggoc") cmd := exec.Command("goc", "build", "--debug")
cmd.Dir = oriWorkingDir cmd.Dir = oriWorkingDir
// use GOPATH mode to compile project // use GOPATH mode to compile project
cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", GOPATH), "GO111MODULE=off") cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", GOPATH), "GO111MODULE=off")
@ -125,7 +124,7 @@ var _ = Describe("E2E", func() {
By("goc install") By("goc install")
testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_gopath_project") 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") cmd.Dir = filepath.Join(testProjDir, "src/qiniu.com/simple_gopath_project")
// use GOPATH mode to compile project // use GOPATH mode to compile project
cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", testProjDir), "GO111MODULE=off") 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") Expect(err).To(BeNil(), "the binary cannot be disassembled")
cnt := strings.Count(string(out), "GoCover") 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") cnt = strings.Count(string(out), "main.registerSelf")
Expect(cnt).To(BeNumerically(">", 0), "main.registerSelf function should be in the binary") Expect(cnt).To(BeNumerically(">", 0), "main.registerSelf function should be in the binary")