add new command 'goc init'
This commit is contained in:
parent
9ce7b168ea
commit
f07d0a1312
39
cmd/init.go
Normal file
39
cmd/init.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
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 (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var initCmd = &cobra.Command{
|
||||||
|
Use: "init",
|
||||||
|
Short: "Clear the register information in order to start a new round of tests",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if res, err := cover.NewWorker().InitSystem(center); err != nil {
|
||||||
|
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
initCmd.Flags().StringVarP(¢er, "center", "", "http://127.0.0.1:7777", "cover profile host center")
|
||||||
|
rootCmd.AddCommand(initCmd)
|
||||||
|
}
|
@ -26,9 +26,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func cpLegacyProject(tmpBuildDir string, pkgs map[string]*cover.Package) {
|
func cpLegacyProject(tmpBuildDir string, pkgs map[string]*cover.Package) {
|
||||||
|
|
||||||
visited := make(map[string]bool)
|
visited := make(map[string]bool)
|
||||||
|
|
||||||
for k, v := range pkgs {
|
for k, v := range pkgs {
|
||||||
dst := filepath.Join(tmpBuildDir, "src", k)
|
dst := filepath.Join(tmpBuildDir, "src", k)
|
||||||
src := v.Dir
|
src := v.Dir
|
||||||
|
@ -27,7 +27,7 @@ import (
|
|||||||
"github.com/qiniu/goc/pkg/cover"
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MvProjectsToTmp(target string, args []string) (newgopath string, newWorkingDir string, tmpBuildDir string, pkgs map[string]*cover.Package) {
|
func MvProjectsToTmp(target string, args []string) (newGopath string, newWorkingDir string, tmpBuildDir string, pkgs map[string]*cover.Package) {
|
||||||
listArgs := []string{"list", "-json"}
|
listArgs := []string{"list", "-json"}
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
listArgs = append(listArgs, args...)
|
listArgs = append(listArgs, args...)
|
||||||
@ -36,15 +36,15 @@ func MvProjectsToTmp(target string, args []string) (newgopath string, newWorking
|
|||||||
pkgs = cover.ListPackages(target, listArgs, "")
|
pkgs = cover.ListPackages(target, listArgs, "")
|
||||||
|
|
||||||
tmpBuildDir, newWorkingDir, isMod := mvProjectsToTmp(pkgs)
|
tmpBuildDir, newWorkingDir, isMod := mvProjectsToTmp(pkgs)
|
||||||
origopath := os.Getenv("GOPATH")
|
oriGopath := os.Getenv("GOPATH")
|
||||||
if isMod == true {
|
if isMod == true {
|
||||||
newgopath = ""
|
newGopath = ""
|
||||||
} else if origopath == "" {
|
} else if oriGopath == "" {
|
||||||
newgopath = tmpBuildDir
|
newGopath = tmpBuildDir
|
||||||
} else {
|
} else {
|
||||||
newgopath = fmt.Sprintf("%v:%v", tmpBuildDir, origopath)
|
newGopath = fmt.Sprintf("%v:%v", tmpBuildDir, oriGopath)
|
||||||
}
|
}
|
||||||
log.Printf("New GOPATH: %v", newgopath)
|
log.Printf("New GOPATH: %v", newGopath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,10 +88,9 @@ func TmpFolderName(path string) string {
|
|||||||
|
|
||||||
// Check if it is go module project
|
// Check if it is go module project
|
||||||
// true legacy
|
// true legacy
|
||||||
// flase go mod
|
// false go mod
|
||||||
func checkIfLegacyProject(pkgs map[string]*cover.Package) bool {
|
func checkIfLegacyProject(pkgs map[string]*cover.Package) bool {
|
||||||
for _, v := range pkgs {
|
for _, v := range pkgs {
|
||||||
|
|
||||||
if v.Module == nil {
|
if v.Module == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -105,7 +104,7 @@ func getTmpwd(tmpBuildDir string, pkgs map[string]*cover.Package, isMod bool) st
|
|||||||
for _, pkg := range pkgs {
|
for _, pkg := range pkgs {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
index := -1
|
index := -1
|
||||||
|
@ -30,13 +30,13 @@ type Store interface {
|
|||||||
// Add adds the given service to store
|
// Add adds the given service to store
|
||||||
Add(s Service) error
|
Add(s Service) error
|
||||||
|
|
||||||
// Get returns the registered service informations with the given service's name
|
// Get returns the registered service information with the given service's name
|
||||||
Get(name string) []string
|
Get(name string) []string
|
||||||
|
|
||||||
// Get returns all the registered service informations as a map
|
// Get returns all the registered service information as a map
|
||||||
GetAll() map[string][]string
|
GetAll() map[string][]string
|
||||||
|
|
||||||
// Init cleanup all the registered service informations
|
// Init cleanup all the registered service information
|
||||||
Init() error
|
Init() error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,25 +68,25 @@ func (l *localStore) Add(s Service) error {
|
|||||||
l.servicesMap[s.Name] = []string{s.Address}
|
l.servicesMap[s.Name] = []string{s.Address}
|
||||||
}
|
}
|
||||||
|
|
||||||
// persistent to local sotre
|
// persistent to local store
|
||||||
return l.appendToFile(s)
|
return l.appendToFile(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the registered service informations with the given name
|
// Get returns the registered service information with the given name
|
||||||
func (l *localStore) Get(name string) []string {
|
func (l *localStore) Get(name string) []string {
|
||||||
l.mu.RLock()
|
l.mu.RLock()
|
||||||
defer l.mu.RUnlock()
|
defer l.mu.RUnlock()
|
||||||
return l.servicesMap[name]
|
return l.servicesMap[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns all the registered service informations
|
// Get returns all the registered service information
|
||||||
func (l *localStore) GetAll() map[string][]string {
|
func (l *localStore) GetAll() map[string][]string {
|
||||||
l.mu.RLock()
|
l.mu.RLock()
|
||||||
defer l.mu.RUnlock()
|
defer l.mu.RUnlock()
|
||||||
return l.servicesMap
|
return l.servicesMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init cleanup all the registered service informations
|
// Init cleanup all the registered service information
|
||||||
// and the local persistent file
|
// and the local persistent file
|
||||||
func (l *localStore) Init() error {
|
func (l *localStore) Init() error {
|
||||||
l.mu.Lock()
|
l.mu.Lock()
|
||||||
@ -99,7 +99,7 @@ func (l *localStore) Init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// load all registered servcie from file to memory
|
// load all registered service from file to memory
|
||||||
func (l *localStore) load() (map[string][]string, error) {
|
func (l *localStore) load() (map[string][]string, error) {
|
||||||
var svrsMap = make(map[string][]string, 0)
|
var svrsMap = make(map[string][]string, 0)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user