1
+ /*
2
+ Copyright © 2022 saltfishpr saltfishpr@gmail.com
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
1
22
package cmd
2
23
3
24
import (
4
- "context "
25
+ "fmt "
5
26
"log"
6
27
"os"
7
28
8
- "github.com/saltfishpr/redis-viewer/internal/config"
29
+ tea "github.com/charmbracelet/bubbletea"
30
+ "github.com/saltfishpr/redis-viewer/internal/conf"
9
31
"github.com/saltfishpr/redis-viewer/internal/constant"
10
32
"github.com/saltfishpr/redis-viewer/internal/tui"
11
-
12
- tea "github.com/charmbracelet/bubbletea"
13
- "github.com/go-redis/redis/v8"
14
33
"github.com/spf13/cobra"
34
+ "github.com/spf13/viper"
15
35
)
16
36
37
+ var cfgFile string
38
+
17
39
// rootCmd represents the base command when called without any subcommands
18
40
var rootCmd = & cobra.Command {
19
41
Use : "redis-viewer" ,
20
42
Short : "view redis data in terminal." ,
21
43
Long : `Redis Viewer is a tool to view redis data in terminal.` ,
22
44
Run : func (cmd * cobra.Command , args []string ) {
23
- config .LoadConfig ()
24
- cfg := config .GetConfig ()
25
-
26
- rdb := redis .NewUniversalClient (& redis.UniversalOptions {
27
- Addrs : cfg .Addrs ,
28
- DB : cfg .DB ,
29
- Username : cfg .Username ,
30
- Password : cfg .Password ,
31
- MaxRetries : constant .MaxRetries ,
32
- MaxRedirects : constant .MaxRedirects ,
33
- MasterName : cfg .MasterName ,
34
- })
35
- _ , err := rdb .Ping (context .Background ()).Result ()
45
+ config := conf .Get ()
46
+
47
+ model , err := tui .New (config )
36
48
if err != nil {
37
- log .Fatal ("connect to redis failed: " , err )
49
+ log .Fatal (err )
38
50
}
39
51
40
- p := tea .NewProgram (tui . New ( rdb ) , tea .WithAltScreen (), tea .WithMouseCellMotion ())
52
+ p := tea .NewProgram (model , tea .WithAltScreen (), tea .WithMouseCellMotion ())
41
53
if err := p .Start (); err != nil {
42
54
log .Fatal ("start failed: " , err )
43
55
}
@@ -54,13 +66,34 @@ func Execute() {
54
66
}
55
67
56
68
func init () {
57
- // Here you will define your flags and configuration settings.
58
- // Cobra supports persistent flags, which, if defined here,
59
- // will be global for your application.
69
+ cobra .OnInitialize (initConfig )
60
70
61
- // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.redis-viewer.yaml)")
71
+ rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is $HOME/.redis-viewer.yaml)" )
72
+ }
73
+
74
+ // initConfig reads in config file and ENV variables if set.
75
+ func initConfig () {
76
+ if cfgFile != "" {
77
+ // Use config file from the flag.
78
+ viper .SetConfigFile (cfgFile )
79
+ } else {
80
+ // Find home directory.
81
+ home , err := os .UserHomeDir ()
82
+ cobra .CheckErr (err )
83
+
84
+ // Search config in home directory with name ".redis-viewer" (without extension).
85
+ viper .AddConfigPath (home )
86
+ viper .SetConfigType ("yaml" )
87
+ viper .SetConfigName (".redis-viewer" )
88
+ }
62
89
63
- // Cobra also supports local flags, which will only run
64
- // when this action is called directly.
65
- rootCmd .Flags ().BoolP ("toggle" , "t" , false , "Help message for toggle" )
90
+ viper .AutomaticEnv () // read in environment variables that match
91
+
92
+ viper .SetDefault ("mode" , "client" )
93
+ viper .SetDefault ("count" , constant .DefaultCount )
94
+
95
+ // If a config file is found, read it in.
96
+ if err := viper .ReadInConfig (); err == nil {
97
+ fmt .Fprintln (os .Stderr , "Using config file:" , viper .ConfigFileUsed ())
98
+ }
66
99
}
0 commit comments