add new command 'goc init'

This commit is contained in:
jichangjun 2020-06-07 14:03:47 +08:00
parent 9ce7b168ea
commit f07d0a1312
4 changed files with 56 additions and 20 deletions

39
cmd/init.go Normal file
View 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(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center")
rootCmd.AddCommand(initCmd)
}

View File

@ -26,9 +26,7 @@ import (
)
func cpLegacyProject(tmpBuildDir string, pkgs map[string]*cover.Package) {
visited := make(map[string]bool)
for k, v := range pkgs {
dst := filepath.Join(tmpBuildDir, "src", k)
src := v.Dir

View File

@ -27,7 +27,7 @@ import (
"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"}
if len(args) != 0 {
listArgs = append(listArgs, args...)
@ -36,15 +36,15 @@ func MvProjectsToTmp(target string, args []string) (newgopath string, newWorking
pkgs = cover.ListPackages(target, listArgs, "")
tmpBuildDir, newWorkingDir, isMod := mvProjectsToTmp(pkgs)
origopath := os.Getenv("GOPATH")
oriGopath := os.Getenv("GOPATH")
if isMod == true {
newgopath = ""
} else if origopath == "" {
newgopath = tmpBuildDir
newGopath = ""
} else if oriGopath == "" {
newGopath = tmpBuildDir
} 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
}
@ -88,10 +88,9 @@ func TmpFolderName(path string) string {
// Check if it is go module project
// true legacy
// flase go mod
// false go mod
func checkIfLegacyProject(pkgs map[string]*cover.Package) bool {
for _, v := range pkgs {
if v.Module == nil {
return true
}
@ -105,7 +104,7 @@ func getTmpwd(tmpBuildDir string, pkgs map[string]*cover.Package, isMod bool) st
for _, pkg := range pkgs {
path, err := os.Getwd()
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

View File

@ -30,13 +30,13 @@ type Store interface {
// Add adds the given service to store
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 returns all the registered service informations as a map
// Get returns all the registered service information as a map
GetAll() map[string][]string
// Init cleanup all the registered service informations
// Init cleanup all the registered service information
Init() error
}
@ -68,25 +68,25 @@ func (l *localStore) Add(s Service) error {
l.servicesMap[s.Name] = []string{s.Address}
}
// persistent to local sotre
// persistent to local store
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 {
l.mu.RLock()
defer l.mu.RUnlock()
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 {
l.mu.RLock()
defer l.mu.RUnlock()
return l.servicesMap
}
// Init cleanup all the registered service informations
// Init cleanup all the registered service information
// and the local persistent file
func (l *localStore) Init() error {
l.mu.Lock()
@ -99,7 +99,7 @@ func (l *localStore) Init() error {
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) {
var svrsMap = make(map[string][]string, 0)