2020-06-14 11:38:41 +00:00
|
|
|
/*
|
|
|
|
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"
|
2020-06-18 09:03:14 +00:00
|
|
|
"os"
|
2020-06-14 11:38:41 +00:00
|
|
|
|
|
|
|
"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.`,
|
2020-06-27 05:24:57 +00:00
|
|
|
Example: `
|
2020-06-14 11:38:41 +00:00
|
|
|
goc run .
|
2020-06-27 05:24:57 +00:00
|
|
|
goc run . [--buildflags] [--exec] [--arguments]
|
2020-06-14 11:38:41 +00:00
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-06-18 09:03:14 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fail to build: %v", err)
|
|
|
|
}
|
2020-06-18 10:08:48 +00:00
|
|
|
gocBuild, err := build.NewBuild(buildFlags, args, wd, buildOutput)
|
2020-06-18 08:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fail to run: %v", err)
|
|
|
|
}
|
2020-06-14 11:38:41 +00:00
|
|
|
gocBuild.GoRunExecFlag = goRunExecFlag
|
|
|
|
gocBuild.GoRunArguments = goRunArguments
|
2020-06-15 07:29:49 +00:00
|
|
|
defer gocBuild.Clean()
|
2020-06-15 07:54:36 +00:00
|
|
|
|
2020-06-15 09:16:50 +00:00
|
|
|
// only save services in memory
|
|
|
|
cover.DefaultStore = cover.NewMemoryStore()
|
|
|
|
|
2020-06-14 11:38:41 +00:00
|
|
|
// 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)
|
|
|
|
|
2020-06-18 08:20:54 +00:00
|
|
|
if err := gocBuild.Run(); err != nil {
|
2020-06-18 08:26:06 +00:00
|
|
|
log.Fatalf("Fail to run: %v", err)
|
2020-06-18 08:20:54 +00:00
|
|
|
}
|
2020-06-14 11:38:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|