Files
kubesphere/pkg/simple/client/devops/options.go
2020-01-02 20:52:00 +08:00

68 lines
2.0 KiB
Go

package devops
import (
"fmt"
"github.com/spf13/pflag"
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
)
type Options struct {
Host string `json:",omitempty" yaml:"host" description:"Jenkins service host address"`
Username string `json:",omitempty" yaml:"username" description:"Jenkins admin username"`
Password string `json:",omitempty" yaml:"password" description:"Jenkins admin password"`
MaxConnections int `json:"maxConnections,omitempty" yaml:"maxConnections" description:"Maximum connections allowed to connect to Jenkins"`
}
// NewDevopsOptions returns a `zero` instance
func NewDevopsOptions() *Options {
return &Options{
Host: "",
Username: "",
Password: "",
MaxConnections: 100,
}
}
// ApplyTo apply configuration to another options
func (s *Options) ApplyTo(options *Options) {
if s.Host != "" {
reflectutils.Override(options, s)
}
}
// Validate check if there is misconfiguration in options
func (s *Options) Validate() []error {
var errors []error
// devops is not needed, ignore rest options
if s.Host == "" {
return errors
}
if s.Username == "" || s.Password == "" {
errors = append(errors, fmt.Errorf("jenkins's username or password is empty"))
}
if s.MaxConnections <= 0 {
errors = append(errors, fmt.Errorf("jenkins's maximum connections should be greater than 0"))
}
return errors
}
func (s *Options) AddFlags(fs *pflag.FlagSet, c *Options) {
fs.StringVar(&s.Host, "jenkins-host", c.Host, ""+
"Jenkins service host address. If left blank, means Jenkins "+
"is unnecessary.")
fs.StringVar(&s.Username, "jenkins-username", c.Username, ""+
"Username for access to Jenkins service. Leave it blank if there isn't any.")
fs.StringVar(&s.Password, "jenkins-password", c.Password, ""+
"Password for access to Jenkins service, used pair with username.")
fs.IntVar(&s.MaxConnections, "jenkins-max-connections", c.MaxConnections, ""+
"Maximum allowed connections to Jenkins. ")
}