2020-07-06 13:20:39 +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 .
* /
2020-06-12 09:51:10 +00:00
package cmd
import (
2020-07-06 13:20:39 +00:00
"fmt"
"net"
2020-06-12 09:51:10 +00:00
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
2020-06-12 10:00:24 +00:00
target string
center string
2020-07-06 13:20:39 +00:00
agentPort AgentPort
2020-06-12 10:00:24 +00:00
debugGoc bool
2020-06-12 09:51:10 +00:00
buildFlags string
2020-06-14 11:37:27 +00:00
goRunExecFlag string
goRunArguments string
2020-06-12 09:51:10 +00:00
)
2020-07-06 13:20:39 +00:00
var coverMode = CoverMode {
mode : "count" ,
}
2020-06-12 09:51:10 +00:00
// addBasicFlags adds a
2020-06-12 10:00:24 +00:00
func addBasicFlags ( cmdset * pflag . FlagSet ) {
2020-06-12 09:51:10 +00:00
cmdset . StringVar ( & center , "center" , "http://127.0.0.1:7777" , "cover profile host center" )
// bind to viper
viper . BindPFlags ( cmdset )
}
2020-06-12 10:00:24 +00:00
func addCommonFlags ( cmdset * pflag . FlagSet ) {
2020-06-12 09:51:10 +00:00
addBasicFlags ( cmdset )
2020-07-06 13:20:39 +00:00
cmdset . Var ( & coverMode , "mode" , "coverage mode: set, count, atomic" )
cmdset . Var ( & agentPort , "agentport" , "a fixed port such as :8100 for registered service communicate with goc server. if not provided, using a random one" )
2020-06-12 10:00:24 +00:00
cmdset . StringVar ( & buildFlags , "buildflags" , "" , "specify the build flags" )
2020-06-12 09:51:10 +00:00
// bind to viper
viper . BindPFlags ( cmdset )
}
2020-06-12 10:00:24 +00:00
func addBuildFlags ( cmdset * pflag . FlagSet ) {
2020-06-12 09:51:10 +00:00
addCommonFlags ( cmdset )
// bind to viper
viper . BindPFlags ( cmdset )
}
func addRunFlags ( cmdset * pflag . FlagSet ) {
addBuildFlags ( cmdset )
2020-06-14 11:37:27 +00:00
cmdset . StringVar ( & goRunExecFlag , "exec" , "" , "same as -exec flag in 'go run' command" )
cmdset . StringVar ( & goRunArguments , "arguments" , "" , "same as 'arguments' in 'go run' command" )
2020-06-12 09:51:10 +00:00
// bind to viper
viper . BindPFlags ( cmdset )
}
2020-07-06 13:20:39 +00:00
// add Cover Mode check
type CoverMode struct {
mode string
}
func ( m * CoverMode ) String ( ) string {
return m . mode
}
func ( m * CoverMode ) Set ( v string ) error {
if v == "" {
m . mode = "count"
return nil
}
if v != "set" && v != "count" && v != "atomic" {
return fmt . Errorf ( "unknown mode" )
}
m . mode = v
return nil
}
func ( m * CoverMode ) Type ( ) string {
return "string"
}
// add agentPort check
type AgentPort struct {
port string
}
func ( agent * AgentPort ) String ( ) string {
return agent . port
}
func ( agent * AgentPort ) Set ( v string ) error {
if v == "" {
agent . port = ""
return nil
}
_ , _ , err := net . SplitHostPort ( v )
if err != nil {
return err
}
agent . port = v
return nil
}
func ( agent * AgentPort ) Type ( ) string {
return "string"
}