Repository: qiniu/goc
Pull request #54 (2020-07-03 06:54:33)

Add particular port for registered service when goc build
#52 

qiniu-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/qiniu/goc/pull/54#" title="Author self-approved">memoryliu</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=qiniu%2Fgoc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/qiniu/goc/blob/master/OWNERS)~~ [memoryliu]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=h1) Report
> Merging [#54](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=desc) into [master](https://codecov.io/gh/qiniu/goc/commit/f8b8fdb01430aeda8ac9033a699cd006e625b718&el=desc) will **increase** coverage by `0.06%`.
> The diff coverage is `71.42%`.

[![Impacted file tree graph](https://codecov.io/gh/qiniu/goc/pull/54/graphs/tree.svg?width=650&height=150&src=pr&token=7AHPDDLXQ3)](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #54      +/-   ##
==========================================
+ Coverage   51.01%   51.07%   +0.06%     
==========================================
  Files          31       31              
  Lines        1578     1580       +2     
==========================================
+ Hits          805      807       +2     
  Misses        654      654              
  Partials      119      119              
```


| [Impacted Files](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [cmd/cover.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-Y21kL2NvdmVyLmdv) | `33.33% <0.00%> (ø)` | |
| [cmd/run.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-Y21kL3J1bi5nbw==) | `11.11% <0.00%> (ø)` | |
| [pkg/cover/instrument.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-cGtnL2NvdmVyL2luc3RydW1lbnQuZ28=) | `16.21% <ø> (ø)` | |
| [cmd/build.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-Y21kL2J1aWxkLmdv) | `52.63% <100.00%> (ø)` | |
| [cmd/commonflags.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-Y21kL2NvbW1vbmZsYWdzLmdv) | `100.00% <100.00%> (ø)` | |
| [cmd/install.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-Y21kL2luc3RhbGwuZ28=) | `50.00% <100.00%> (ø)` | |
| [pkg/cover/cover.go](https://codecov.io/gh/qiniu/goc/pull/54/diff?src=pr&el=tree#diff-cGtnL2NvdmVyL2NvdmVyLmdv) | `56.02% <100.00%> (+0.15%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=footer). Last update [f8b8fdb...23f9dba](https://codecov.io/gh/qiniu/goc/pull/54?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index b4d5dfe..82433f3 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,6 @@
 ![](https://github.com/qiniu/goc/workflows/ut-check/badge.svg)
 ![](https://github.com/qiniu/goc/workflows/style-check/badge.svg)
 ![](https://github.com/qiniu/goc/workflows/e2e%20test/badge.svg)
-![Build Release](https://github.com/qiniu/goc/workflows/Build%20Release/badge.svg)
 [![codecov](https://codecov.io/gh/qiniu/goc/branch/master/graph/badge.svg)](https://codecov.io/gh/qiniu/goc)
 [![GoDoc](https://godoc.org/github.com/qiniu/goc?status.svg)](https://godoc.org/github.com/qiniu/goc)
 
diff --git a/ci-build.sh b/ci-build.sh
index d888a5d..f9fc2d9 100755
--- a/ci-build.sh
+++ b/ci-build.sh
@@ -6,11 +6,11 @@ EVENT_DATA=$(cat $GITHUB_EVENT_PATH)
 echo $EVENT_DATA | jq .
 UPLOAD_URL=$(echo $EVENT_DATA | jq -r .release.upload_url)
 UPLOAD_URL=${UPLOAD_URL/\{?name,label\}/}
-RELEASE_VERSION=$(echo $EVENT_DATA | jq -r .release.tag_name)
+RELEASE_NAME=$(echo $EVENT_DATA | jq -r .release.tag_name)
 PROJECT_NAME=$(basename $GITHUB_REPOSITORY)
-NAME="${NAME:-${PROJECT_NAME}-${RELEASE_VERSION}}-${GOOS}-${GOARCH}"
+NAME="${NAME:-${PROJECT_NAME}-${RELEASE_NAME}}-${GOOS}-${GOARCH}"
 
-CGO_ENABLED=0 go build -ldflags "-X 'github.com/qiniu/goc/cmd.version=${RELEASE_VERSION}'" .
+go build .
 
 ARCHIVE=tmp.tar.gz
 FILE_LIST=goc
diff --git a/cmd/build.go b/cmd/build.go
index 099d472..4617b86 100644
--- a/cmd/build.go
+++ b/cmd/build.go
@@ -71,7 +71,7 @@ func runBuild(args []string, wd string) {
 	defer gocBuild.Clean()
 	// doCover with original buildFlags, with new GOPATH( tmp:original )
 	// in the tmp directory
-	cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, center)
+	cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, agentPort, center)
 	// do install in the temporary directory
 	err = gocBuild.Build()
 	if err != nil {
diff --git a/cmd/commonflags.go b/cmd/commonflags.go
index e36647b..e48f7c9 100644
--- a/cmd/commonflags.go
+++ b/cmd/commonflags.go
@@ -9,6 +9,7 @@ var (
 	target     string
 	center     string
 	mode       string
+	agentPort  string
 	debugGoc   bool
 	buildFlags string
 
@@ -26,6 +27,7 @@ func addBasicFlags(cmdset *pflag.FlagSet) {
 func addCommonFlags(cmdset *pflag.FlagSet) {
 	addBasicFlags(cmdset)
 	cmdset.StringVar(&mode, "mode", "count", "coverage mode: set, count, atomic")
+	cmdset.StringVar(&agentPort, "agentport", "", "specify fixed port for registered service communicate with goc server. if not provided, using a random port")
 	cmdset.StringVar(&buildFlags, "buildflags", "", "specify the build flags")
 	// bind to viper
 	viper.BindPFlags(cmdset)
diff --git a/cmd/cover.go b/cmd/cover.go
index a9ae61c..d394980 100644
--- a/cmd/cover.go
+++ b/cmd/cover.go
@@ -48,7 +48,7 @@ goc cover --center=http://127.0.0.1:7777 --target=/path/to/target --mode=atomic
 			log.Fatalf("unknown -mode %v", mode)
 		}
 
-		cover.Execute(buildFlags, "", target, mode, center)
+		cover.Execute(buildFlags, "", target, mode, agentPort, center)
 	},
 }
 
diff --git a/cmd/install.go b/cmd/install.go
index 4dc48b9..dbe1f7a 100644
--- a/cmd/install.go
+++ b/cmd/install.go
@@ -64,7 +64,7 @@ func runInstall(args []string, wd string) {
 	defer gocBuild.Clean()
 	// doCover with original buildFlags, with new GOPATH( tmp:original )
 	// in the tmp directory
-	cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, center)
+	cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, agentPort, center)
 	// do install in the temporary directory
 	err = gocBuild.Install()
 	if err != nil {
diff --git a/cmd/run.go b/cmd/run.go
index f3ac9fb..99a8368 100644
--- a/cmd/run.go
+++ b/cmd/run.go
@@ -60,7 +60,7 @@ goc run . [--buildflags] [--exec] [--arguments]
 		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)
+		cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, "", gocServer)
 
 		if err := gocBuild.Run(); err != nil {
 			log.Fatalf("Fail to run: %v", err)
diff --git a/cmd/version.go b/cmd/version.go
deleted file mode 100644
index 833f772..0000000
--- a/cmd/version.go
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- 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"
-
-	"github.com/spf13/cobra"
-)
-
-// the version value will be injected when publishing
-var version = "Unstable"
-
-var versionCmd = &cobra.Command{
-	Use:   "version",
-	Short: "Print the goc version information",
-	Example: `
-# Print the client and server versions for the current context
-goc version
-	`,
-	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Println(version)
-	},
-}
-
-func init() {
-	rootCmd.AddCommand(versionCmd)
-}
diff --git a/go.mod b/go.mod
index 9e9fbaa..b6fec6a 100644
--- a/go.mod
+++ b/go.mod
@@ -7,6 +7,7 @@ require (
 	github.com/google/go-github v17.0.0+incompatible
 	github.com/hashicorp/go-retryablehttp v0.6.6
 	github.com/julienschmidt/httprouter v1.2.0
+	github.com/magiconair/properties v1.8.1
 	github.com/mattn/go-runewidth v0.0.9 // indirect
 	github.com/olekukonko/tablewriter v0.0.4
 	github.com/onsi/ginkgo v1.11.0
diff --git a/pkg/cover/cover.go b/pkg/cover/cover.go
index 688095f..3c97de5 100644
--- a/pkg/cover/cover.go
+++ b/pkg/cover/cover.go
@@ -46,6 +46,7 @@ var (
 // TestCover is a collection of all counters
 type TestCover struct {
 	Mode         string
+	AgentPort    string
 	Center       string // cover profile host center
 	MainPkgCover *PackageCover
 	DepsCover    []*PackageCover
@@ -120,7 +121,7 @@ type PackageError struct {
 }
 
 //Execute execute go tool cover for all the .go files in the target folder
-func Execute(args, newGopath, target, mode, center string) error {
+func Execute(args, newGopath, target, mode, agentPort, center string) error {
 	if !isDirExist(target) {
 		log.Errorf("Target directory %s not exist", target)
 		return ErrCoverPkgFailed
@@ -152,6 +153,7 @@ func Execute(args, newGopath, target, mode, center string) error {
 			// new a testcover for this service
 			tc := TestCover{
 				Mode:         mode,
+				AgentPort:    agentPort,
 				Center:       center,
 				MainPkgCover: mainCover,
 			}
diff --git a/pkg/cover/instrument.go b/pkg/cover/instrument.go
index ab4d396..4248d85 100644
--- a/pkg/cover/instrument.go
+++ b/pkg/cover/instrument.go
@@ -260,8 +260,12 @@ func listen() (ln net.Listener, host string, err error) {
 			return
 		}
 	}
-
-	ln, err = net.Listen("tcp4", ":0")
+	agentPort := "{{.AgentPort }}"
+	if agentPort != "" {
+		ln, err = net.Listen("tcp4", agentPort)
+	} else {
+		ln, err = net.Listen("tcp4", ":0")
+	}
 	if err != nil {
 		return
 	}


Test patch:
diff --git a/pkg/cover/cover_test.go b/pkg/cover/cover_test.go
index 098cc67..7bf1549 100644
--- a/pkg/cover/cover_test.go
+++ b/pkg/cover/cover_test.go
@@ -316,7 +316,7 @@ func TestExecuteForSimpleModProject(t *testing.T) {
 	testDir := filepath.Join(os.TempDir(), "goc-build-test")
 	copy.Copy(workingDir, testDir)
 
-	Execute("", gopath, testDir, "count", "http://127.0.0.1:7777")
+	Execute("", gopath, testDir, "count", "", "http://127.0.0.1:7777")
 
 	_, err := os.Lstat(filepath.Join(testDir, "http_cover_apis_auto_generated.go"))
 	if !assert.Equal(t, err, nil) {