options.go 1.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package openpitrix

import (
	"fmt"
	"github.com/spf13/pflag"
	"kubesphere.io/kubesphere/pkg/utils/reflectutils"
)

type OpenPitrixOptions struct {
	APIServer string `json:"apiServer,omitempty" yaml:"apiServer,omitempty"`
	Token     string `json:"token,omitempty" yaml:"token,omitempty"`
}

func NewOpenPitrixOptions() *OpenPitrixOptions {
	return &OpenPitrixOptions{
		APIServer: "",
		Token:     "",
	}
}

func (s *OpenPitrixOptions) ApplyTo(options *OpenPitrixOptions) {
J
Jeff 已提交
22 23 24 25 26
	if options == nil {
		options = s
		return
	}

J
Jeff 已提交
27 28 29
	if s.APIServer != "" {
		reflectutils.Override(options, s)
	}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
}

func (s *OpenPitrixOptions) Validate() []error {
	errs := []error{}

	if s.APIServer != "" {
		if s.Token == "" {
			errs = append(errs, fmt.Errorf("OpenPitrix access token cannot be empty"))
		}
	}

	return errs
}

func (s *OpenPitrixOptions) AddFlags(fs *pflag.FlagSet) {
	fs.StringVar(&s.APIServer, "openpitrix-apiserver", s.APIServer, ""+
		"OpenPitrix api gateway endpoint, if left blank, following options will be ignored.")

	fs.StringVar(&s.Token, "openpitrix-token", s.Token, ""+
		"OpenPitrix api access token.")
}