Skip to content

Commit 74aab11

Browse files
committed
bugfix: exit when connect failed.
1 parent fa7ebc2 commit 74aab11

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

cmd/root.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"log"
56
"os"
67

@@ -26,14 +27,25 @@ to quickly create a Cobra application.`,
2627
config.LoadConfig()
2728
cfg := config.GetConfig()
2829

29-
rdb := redis.NewClient(&redis.Options{
30-
Addr: cfg.Addr,
31-
Password: cfg.Password,
32-
DB: cfg.DB,
33-
})
30+
var rdb *redis.Client
31+
switch cfg.Mode {
32+
case "sentinel":
33+
rdb = redis.NewFailoverClient(&redis.FailoverOptions{
34+
MasterName: cfg.MasterName,
35+
SentinelAddrs: cfg.SentinelAddrs,
36+
Password: cfg.SentinelPassword,
37+
})
38+
default:
39+
rdb = redis.NewClient(&redis.Options{
40+
Addr: cfg.Addr,
41+
Password: cfg.Password,
42+
DB: cfg.DB,
43+
})
44+
}
3445

35-
if rdb == nil {
36-
log.Fatal("start failed: cannot connect to redis")
46+
_, err := rdb.Ping(context.Background()).Result()
47+
if err != nil {
48+
log.Fatal("connect to redis failed: ", err)
3749
}
3850

3951
p := tea.NewProgram(tui.New(rdb), tea.WithAltScreen())

internal/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ import (
1818

1919
// Config represents the main config for the application.
2020
type Config struct {
21+
Mode string `mapstructure:"mode"`
22+
23+
// client
2124
Addr string `mapstructure:"addr"`
2225
Password string `mapstructure:"password"`
2326
DB int `mapstructure:"db"`
27+
28+
// sentinel
29+
MasterName string `mapstructure:"master_name"`
30+
SentinelAddrs []string `mapstructure:"sentinel_addrs"`
31+
SentinelPassword string `mapstructure:"sentinel_password"`
2432
}
2533

2634
// LoadConfig loads a users config and creates the config if it does not exist.

0 commit comments

Comments
 (0)