goc/cmd/commonflags.go

127 lines
2.9 KiB
Go
Raw Normal View History

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 (
target string
center string
agentPort AgentPort
debugGoc bool
debugInCISyncFile string
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
2020-07-26 09:03:47 +00:00
// CoverMode represents the covermode when doing cover for source code
2020-07-06 13:20:39 +00:00
type CoverMode struct {
mode string
}
func (m *CoverMode) String() string {
return m.mode
}
2020-07-26 09:03:47 +00:00
// Set sets the value to the CoverMode struct, use 'count' as default if v is empty
2020-07-06 13:20:39 +00:00
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
}
2020-07-26 09:03:47 +00:00
// Type returns the type of CoverMode
2020-07-06 13:20:39 +00:00
func (m *CoverMode) Type() string {
return "string"
}
2020-07-26 09:03:47 +00:00
// AgentPort is the struct to do agentPort check
2020-07-06 13:20:39 +00:00
type AgentPort struct {
port string
}
func (agent *AgentPort) String() string {
return agent.port
}
2020-07-26 09:03:47 +00:00
// Set sets the value to the AgentPort struct
2020-07-06 13:20:39 +00:00
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
}
2020-07-26 09:03:47 +00:00
// Type returns the type of AgentPort
2020-07-06 13:20:39 +00:00
func (agent *AgentPort) Type() string {
return "string"
}