Refactor flag parsing code

This commit is contained in:
Ken Hibino 2022-05-05 06:44:55 -07:00
parent 0527b6c483
commit b7c2ebeff3
2 changed files with 42 additions and 59 deletions

View File

@ -12,7 +12,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/go-redis/redis/v8"
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
"github.com/hibiken/asynq/x/metrics" "github.com/hibiken/asynq/x/metrics"
"github.com/hibiken/asynqmon" "github.com/hibiken/asynqmon"
@ -83,69 +82,52 @@ func parseFlags(progname string, args []string) (cfg *Config, output string, err
return &conf, buf.String(), nil return &conf, buf.String(), nil
} }
// TODO: Refactor this code! func makeTLSConfig(cfg *Config) *tls.Config {
if cfg.RedisTLS == "" && !cfg.RedisInsecureTLS {
return nil
}
return &tls.Config{
ServerName: cfg.RedisTLS,
InsecureSkipVerify: cfg.RedisInsecureTLS,
}
}
func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) { func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) {
var opts redis.UniversalOptions // Connecting to redis-cluster
sentinel := false if len(cfg.RedisClusterNodes) > 0 {
if cfg.RedisClusterNodes != "" {
opts.Addrs = strings.Split(cfg.RedisClusterNodes, ",")
opts.Password = cfg.RedisPassword
} else {
if cfg.RedisURL != "" {
res, err := asynq.ParseRedisURI(cfg.RedisURL)
if err != nil {
return nil, err
}
switch v := res.(type) {
case asynq.RedisClientOpt:
opts.Addrs = append(opts.Addrs, v.Addr)
opts.DB = v.DB
opts.Password = v.Password
case asynq.RedisFailoverClientOpt:
opts.Addrs = append(opts.Addrs, v.SentinelAddrs...)
opts.SentinelPassword = v.SentinelPassword
opts.MasterName = v.MasterName
sentinel = true
}
} else {
opts.Addrs = []string{cfg.RedisAddr}
opts.DB = cfg.RedisDB
opts.Password = cfg.RedisPassword
}
}
if cfg.RedisTLS != "" {
opts.TLSConfig = &tls.Config{ServerName: cfg.RedisTLS}
}
if cfg.RedisInsecureTLS {
if opts.TLSConfig == nil {
opts.TLSConfig = &tls.Config{}
}
opts.TLSConfig.InsecureSkipVerify = true
}
if cfg.RedisClusterNodes != "" {
return asynq.RedisClusterClientOpt{ return asynq.RedisClusterClientOpt{
Addrs: opts.Addrs, Addrs: strings.Split(cfg.RedisClusterNodes, ","),
Password: opts.Password, Password: cfg.RedisPassword,
TLSConfig: opts.TLSConfig, TLSConfig: makeTLSConfig(cfg),
}, nil }, nil
} }
if sentinel {
return asynq.RedisFailoverClientOpt{ // Connecting to redis-sentinels
MasterName: opts.MasterName, if strings.HasPrefix(cfg.RedisURL, "redis-sentinel") {
SentinelAddrs: opts.Addrs, res, err := asynq.ParseRedisURI(cfg.RedisURL)
SentinelPassword: opts.SentinelPassword, if err != nil {
TLSConfig: opts.TLSConfig, return nil, err
}, nil }
connOpt := res.(asynq.RedisFailoverClientOpt) // safe to type-assert
connOpt.TLSConfig = makeTLSConfig(cfg)
return connOpt, nil
} }
return asynq.RedisClientOpt{
Addr: opts.Addrs[0], // Connecting to single redis server
DB: opts.DB, var connOpt asynq.RedisClientOpt
Password: opts.Password, if len(cfg.RedisURL) > 0 {
TLSConfig: opts.TLSConfig, res, err := asynq.ParseRedisURI(cfg.RedisURL)
}, nil if err != nil {
return nil, err
}
connOpt = res.(asynq.RedisClientOpt) // safe to type-assert
} else {
connOpt.Addr = cfg.RedisAddr
connOpt.DB = cfg.RedisDB
connOpt.Password = cfg.RedisPassword
}
connOpt.TLSConfig = makeTLSConfig(cfg)
return connOpt, nil
} }
func main() { func main() {

View File

@ -106,6 +106,7 @@ func TestMakeRedisConnOpt(t *testing.T) {
MasterName: "mymaster", MasterName: "mymaster",
SentinelAddrs: []string{ SentinelAddrs: []string{
"localhost:5000", "localhost:5001", "localhost:5002"}, "localhost:5000", "localhost:5001", "localhost:5002"},
Password: "secretpassword",
}, },
}, },
{ {