This is a huge commit, it does following things:
1. refactor kubesphere dependency service client creation, we can disable dependency by config 2. dependencies can be configured by configuration file 3. refactor cmd package using cobra.Command, so we can use hypersphere to invoke command sepearately. Later we only need to build one image to contains all kubesphere core components. One command to rule them all! 4. live reloading configuration currently not implemented
This commit is contained in:
49
pkg/simple/client/sonarqube/options.go
Normal file
49
pkg/simple/client/sonarqube/options.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package sonarqube
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type SonarQubeOptions struct {
|
||||
Host string `json:",omitempty" yaml:",omitempty" description:"SonarQube service host address"`
|
||||
Token string `json:",omitempty" yaml:",omitempty" description:"SonarQube service token"`
|
||||
}
|
||||
|
||||
func NewSonarQubeOptions() *SonarQubeOptions {
|
||||
return &SonarQubeOptions{
|
||||
Host: "",
|
||||
Token: "",
|
||||
}
|
||||
}
|
||||
|
||||
func NewDefaultSonarQubeOptions() *SonarQubeOptions {
|
||||
return NewSonarQubeOptions()
|
||||
}
|
||||
|
||||
func (s *SonarQubeOptions) Validate() []error {
|
||||
errors := []error{}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
func (s *SonarQubeOptions) ApplyTo(options *SonarQubeOptions) {
|
||||
if options == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s.Host != "" {
|
||||
options.Host = s.Host
|
||||
}
|
||||
|
||||
if s.Token != "" {
|
||||
options.Token = s.Token
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SonarQubeOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&s.Host, "sonarqube-host", s.Host, ""+
|
||||
"Sonarqube service address if enabled.")
|
||||
|
||||
fs.StringVar(&s.Token, "sonarqube-token", s.Token, ""+
|
||||
"Sonarqube service access token.")
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package sonarqube
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/golang/glog"
|
||||
"github.com/kubesphere/sonargo/sonar"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
sonarAddress string
|
||||
sonarToken string
|
||||
sonarOnce sync.Once
|
||||
sonarClient *sonargo.Client
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&sonarAddress, "sonar-address", "", "sonar server host")
|
||||
flag.StringVar(&sonarToken, "sonar-token", "", "sonar token")
|
||||
}
|
||||
|
||||
func Client() *sonargo.Client {
|
||||
|
||||
sonarOnce.Do(func() {
|
||||
if sonarAddress == "" {
|
||||
sonarClient = nil
|
||||
glog.Info("skip sonar init")
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(sonarAddress, "/") {
|
||||
sonarAddress += "/"
|
||||
}
|
||||
client, err := sonargo.NewClientWithToken(sonarAddress+"api/", sonarToken)
|
||||
if err != nil {
|
||||
glog.Error("failed to connect to sonar")
|
||||
return
|
||||
}
|
||||
_, _, err = client.Projects.Search(nil)
|
||||
if err != nil {
|
||||
glog.Errorf("failed to search sonar projects [%+v]", err)
|
||||
return
|
||||
}
|
||||
glog.Info("init sonar client success")
|
||||
sonarClient = client
|
||||
})
|
||||
|
||||
return sonarClient
|
||||
}
|
||||
54
pkg/simple/client/sonarqube/sonarqube.go
Normal file
54
pkg/simple/client/sonarqube/sonarqube.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package sonarqube
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kubesphere/sonargo/sonar"
|
||||
"k8s.io/klog"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SonarQubeClient struct {
|
||||
client *sonargo.Client
|
||||
}
|
||||
|
||||
func NewSonarQubeClient(options *SonarQubeOptions) (*SonarQubeClient, error) {
|
||||
var endpoint string
|
||||
|
||||
if strings.HasSuffix(options.Host, "/") {
|
||||
endpoint = fmt.Sprintf("%sapi/", options.Host)
|
||||
} else {
|
||||
endpoint = fmt.Sprintf("%s/api/", options.Host)
|
||||
}
|
||||
|
||||
sonar, err := sonargo.NewClientWithToken(endpoint, options.Token)
|
||||
if err != nil {
|
||||
klog.Errorf("failed to connect to sonarqube service, %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &SonarQubeClient{client: sonar}, err
|
||||
}
|
||||
|
||||
func NewSonarQubeClientOrDie(options *SonarQubeOptions) *SonarQubeClient {
|
||||
var endpoint string
|
||||
|
||||
if strings.HasSuffix(options.Host, "/") {
|
||||
endpoint = fmt.Sprintf("%sapi/", options.Host)
|
||||
} else {
|
||||
endpoint = fmt.Sprintf("%s/api/", options.Host)
|
||||
}
|
||||
|
||||
sonar, err := sonargo.NewClientWithToken(endpoint, options.Token)
|
||||
if err != nil {
|
||||
klog.Errorf("failed to connect to sonarqube service, %+v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &SonarQubeClient{client: sonar}
|
||||
}
|
||||
|
||||
// return sonarqube client
|
||||
// Also we can wrap some methods to avoid direct use sonar client
|
||||
func (s *SonarQubeClient) SonarQube() *sonargo.Client {
|
||||
return s.client
|
||||
}
|
||||
Reference in New Issue
Block a user