feature: add run command
This commit is contained in:
parent
0fb0487dbb
commit
4ad2bc3fd7
@ -19,9 +19,8 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"github.com/qiniu/goc/pkg/cover"
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/viper"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var coverCmd = &cobra.Command{
|
var coverCmd = &cobra.Command{
|
||||||
|
73
cmd/run.go
Normal file
73
cmd/run.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 Qiniu Cloud (qiniu.com)
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/qiniu/goc/pkg/build"
|
||||||
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var runCmd = &cobra.Command{
|
||||||
|
Use: "run",
|
||||||
|
Short: "Run covers and runs the named main Go package",
|
||||||
|
Long: `Run covers and runs the named main Go package,
|
||||||
|
It is exactly behave as 'go run .' in addition of some internal goc features.`,
|
||||||
|
Example: `
|
||||||
|
goc run .
|
||||||
|
`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
gocBuild := build.NewBuild(buildFlags, packages, buildOutput)
|
||||||
|
gocBuild.GoRunExecFlag = goRunExecFlag
|
||||||
|
gocBuild.GoRunArguments = goRunArguments
|
||||||
|
defer func() {
|
||||||
|
if !debugGoc {
|
||||||
|
gocBuild.Clean()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
// start goc server
|
||||||
|
var l = newLocalListener()
|
||||||
|
go cover.GocServer(ioutil.Discard).RunListener(l)
|
||||||
|
gocServer := fmt.Sprintf("http://%s", l.Addr().String())
|
||||||
|
fmt.Printf("[goc] goc server started: %s \n", gocServer)
|
||||||
|
|
||||||
|
// execute covers for the target source with original buildFlags and new GOPATH( tmp:original )
|
||||||
|
cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, gocServer)
|
||||||
|
|
||||||
|
gocBuild.Run()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
addRunFlags(runCmd.Flags())
|
||||||
|
rootCmd.AddCommand(runCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLocalListener() net.Listener {
|
||||||
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
if l, err = net.Listen("tcp6", "[::1]:0"); err != nil {
|
||||||
|
log.Fatalf("failed to listen on a port: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
@ -35,10 +35,17 @@ type Build struct {
|
|||||||
TmpDir string // the temporary directory to build the project
|
TmpDir string // the temporary directory to build the project
|
||||||
TmpWorkingDir string // the working directory in the temporary directory, which is corresponding to the current directory in the project directory
|
TmpWorkingDir string // the working directory in the temporary directory, which is corresponding to the current directory in the project directory
|
||||||
IsMod bool // determine whether it is a Mod project
|
IsMod bool // determine whether it is a Mod project
|
||||||
BuildFlags string // Build flags
|
|
||||||
Packages string // Packages that needs to build
|
|
||||||
Root string // Project Root
|
Root string // Project Root
|
||||||
Target string // the binary name that go build generate
|
Target string // the binary name that go build generate
|
||||||
|
|
||||||
|
// keep compatible with go commands:
|
||||||
|
// go run [build flags] [-exec xprog] package [arguments...]
|
||||||
|
// go build [-o output] [-i] [build flags] [packages]
|
||||||
|
// go install [-i] [build flags] [packages]
|
||||||
|
BuildFlags string // Build flags
|
||||||
|
Packages string // Packages that needs to build
|
||||||
|
GoRunExecFlag string // for the -exec flags in go run command
|
||||||
|
GoRunArguments string // for the '[arguments]' parameters in go run command
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuild creates a Build struct which can build from goc temporary directory,
|
// NewBuild creates a Build struct which can build from goc temporary directory,
|
||||||
@ -108,7 +115,26 @@ func (b *Build) determineOutputDir(outputDir string) string {
|
|||||||
func (b *Build) validatePackageForBuild() bool {
|
func (b *Build) validatePackageForBuild() bool {
|
||||||
if b.Packages == "." {
|
if b.Packages == "." {
|
||||||
return true
|
return true
|
||||||
} else {
|
}
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run excutes the main package in addition with the internal goc features
|
||||||
|
func (b *Build) Run() {
|
||||||
|
cmd := exec.Command("/bin/bash", "-c", "go run "+b.BuildFlags+" "+b.GoRunExecFlag+" "+b.Packages+" "+b.GoRunArguments)
|
||||||
|
cmd.Dir = b.TmpWorkingDir
|
||||||
|
|
||||||
|
if b.NewGOPATH != "" {
|
||||||
|
// Change to temp GOPATH for go install command
|
||||||
|
cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", b.NewGOPATH))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("go build cmd is: %v", cmd.Args)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Fail to execute: %v. The error is: %v, the stdout/stderr is: %v", cmd.Args, err, string(out))
|
||||||
|
}
|
||||||
|
if len(out) > 0 {
|
||||||
|
fmt.Println(string(out))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,8 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Build) cpGoModulesProject() {
|
func (b *Build) cpGoModulesProject() {
|
||||||
|
@ -18,9 +18,10 @@ package build
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewInstall creates a Build struct which can install from goc temporary directory
|
// NewInstall creates a Build struct which can install from goc temporary directory
|
||||||
@ -59,7 +60,6 @@ func (b *Build) Install() {
|
|||||||
func (b *Build) validatePackageForInstall() bool {
|
func (b *Build) validatePackageForInstall() bool {
|
||||||
if b.Packages == "." || b.Packages == "./..." {
|
if b.Packages == "." || b.Packages == "./..." {
|
||||||
return true
|
return true
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,12 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
"github.com/qiniu/goc/pkg/cover"
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Build) cpLegacyProject() {
|
func (b *Build) cpLegacyProject() {
|
||||||
|
@ -23,9 +23,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/qiniu/goc/pkg/cover"
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Build) MvProjectsToTmp() {
|
func (b *Build) MvProjectsToTmp() {
|
||||||
|
@ -19,7 +19,6 @@ package cover
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
@ -27,6 +26,8 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"golang.org/x/tools/cover"
|
"golang.org/x/tools/cover"
|
||||||
"k8s.io/test-infra/gopherage/pkg/cov"
|
"k8s.io/test-infra/gopherage/pkg/cov"
|
||||||
|
Loading…
Reference in New Issue
Block a user