Skip to content

Commit 57e899d

Browse files
committed
custom config file path
1 parent 451054f commit 57e899d

File tree

20 files changed

+469
-440
lines changed

20 files changed

+469
-440
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616

17+
dist/
18+
1719
# My Project
20+
examples
1821
output
1922
logs
20-
21-
dist/

.goreleaser.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ builds:
1010
- darwin
1111
archives:
1212
- format: binary
13-
replacements:
14-
darwin: Darwin
15-
linux: Linux
16-
windows: Windows
17-
386: i386
18-
amd64: x86_64
1913
checksum:
2014
name_template: "checksums.txt"
2115
snapshot:

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Launch Package",
6+
"type": "go",
7+
"request": "launch",
8+
"mode": "auto",
9+
"program": "${workspaceFolder}/main.go",
10+
"console": "integratedTerminal"
11+
}
12+
]
13+
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2021 SaltFishPr
3+
Copyright © 2022 saltfishpr saltfishpr@gmail.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ Download from [Release](https://github.com/saltfishpr/redis-viewer/releases).
3030
| scroll up | detail scroll up |
3131
| scroll down | detail scroll down |
3232

33-
config file directory:
34-
35-
- Windows: `%USERPROFILE%/redis-viewer.yml`
36-
- Linux: `~/.config/redis-viewer/redis-viewer.yml`
33+
default config file directory is `$HOME/.redis-viewer.yaml`
3734

3835
example config file:
3936

@@ -65,7 +62,7 @@ In Windows, you should change system encoding to `UTF-8` before run this program
6562
## TODOs:
6663

6764
- [x] Add load animation.
68-
- [ ] Friendly value detail.
65+
- [x] Friendly value detail.
6966
- [ ] Add log view.
7067

7168
Build with [bubbletea](https://github.com/charmbracelet/bubbletea).

cmd/root.go

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
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+
*/
122
package cmd
223

324
import (
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
1840
var 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

5668
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)
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
}

examples/main.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

go.mod

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module github.com/saltfishpr/redis-viewer
33
go 1.16
44

55
require (
6-
github.com/charmbracelet/bubbles v0.10.3
7-
github.com/charmbracelet/bubbletea v0.20.0
6+
github.com/charmbracelet/bubbles v0.12.0
7+
github.com/charmbracelet/bubbletea v0.22.0
88
github.com/charmbracelet/lipgloss v0.5.0
9-
github.com/go-redis/redis/v8 v8.11.4
9+
github.com/go-redis/redis/v8 v8.11.5
1010
github.com/json-iterator/go v1.1.12
1111
github.com/muesli/reflow v0.3.0
12-
github.com/spf13/cobra v1.3.0
13-
github.com/spf13/viper v1.10.1
12+
github.com/spf13/cast v1.5.0
13+
github.com/spf13/cobra v1.5.0
14+
github.com/spf13/viper v1.12.0
1415
)

0 commit comments

Comments
 (0)