options.go 1.6 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2019 The KubeSphere Authors.

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.
*/

Z
zryfish 已提交
17
package cache
18 19

import (
Z
zryfish 已提交
20
	"fmt"
21 22 23
	"github.com/spf13/pflag"
)

Z
zryfish 已提交
24
type Options struct {
Z
zryfish 已提交
25 26 27 28
	Host     string `json:"host"`
	Port     int    `json:"port"`
	Password string `json:"password"`
	DB       int    `json:"db"`
29 30 31 32
}

// NewRedisOptions returns options points to nowhere,
// because redis is not required for some components
Z
zryfish 已提交
33 34
func NewRedisOptions() *Options {
	return &Options{
Z
zryfish 已提交
35 36 37 38
		Host:     "",
		Port:     0,
		Password: "",
		DB:       0,
39 40 41
	}
}

J
Jeff 已提交
42
// Validate check options
Z
zryfish 已提交
43
func (r *Options) Validate() []error {
44 45
	errors := make([]error, 0)

Z
zryfish 已提交
46 47
	if r.Port == 0 {
		errors = append(errors, fmt.Errorf("invalid service port number"))
J
Jeff 已提交
48 49
	}

50 51 52
	return errors
}

J
Jeff 已提交
53 54
// AddFlags add option flags to command line flags,
// if redis-host left empty, the following options will be ignored.
Z
zryfish 已提交
55
func (r *Options) AddFlags(fs *pflag.FlagSet, s *Options) {
Z
zryfish 已提交
56 57 58 59 60 61
	fs.StringVar(&r.Host, "redis-host", s.Host, "Redis connection URL. If left blank, means redis is unnecessary, "+
		"redis will be disabled.")

	fs.IntVar(&r.Port, "redis-port", s.Port, "")
	fs.StringVar(&r.Password, "redis-password", s.Password, "")
	fs.IntVar(&r.DB, "redis-db", s.DB, "")
62
}