1+ /*
2+ Copyright © 2022 saltfishpr [email protected] 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+ */
122package cmd
223
324import (
4- "context "
25+ "fmt "
526 "log"
627 "os"
728
8- "github.com/saltfishpr/redis-viewer/internal/config"
29+ tea "github.com/charmbracelet/bubbletea"
30+ "github.com/saltfishpr/redis-viewer/internal/conf"
931 "github.com/saltfishpr/redis-viewer/internal/constant"
1032 "github.com/saltfishpr/redis-viewer/internal/tui"
11-
12- tea "github.com/charmbracelet/bubbletea"
13- "github.com/go-redis/redis/v8"
1433 "github.com/spf13/cobra"
34+ "github.com/spf13/viper"
1535)
1636
37+ var cfgFile string
38+
1739// rootCmd represents the base command when called without any subcommands
1840var rootCmd = & cobra.Command {
1941 Use : "redis-viewer" ,
2042 Short : "view redis data in terminal." ,
2143 Long : `Redis Viewer is a tool to view redis data in terminal.` ,
2244 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 )
3648 if err != nil {
37- log .Fatal ("connect to redis failed: " , err )
49+ log .Fatal (err )
3850 }
3951
40- p := tea .NewProgram (tui . New ( rdb ) , tea .WithAltScreen (), tea .WithMouseCellMotion ())
52+ p := tea .NewProgram (model , tea .WithAltScreen (), tea .WithMouseCellMotion ())
4153 if err := p .Start (); err != nil {
4254 log .Fatal ("start failed: " , err )
4355 }
@@ -54,13 +66,34 @@ func Execute() {
5466}
5567
5668func 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 )
6070
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+ }
6289
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+ }
6699}
0 commit comments