Skip to content

Commit

Permalink
custom config file path
Browse files Browse the repository at this point in the history
  • Loading branch information
saltfishpr committed Jun 28, 2022
1 parent 451054f commit 57e899d
Show file tree
Hide file tree
Showing 20 changed files with 469 additions and 440 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# Dependency directories (remove the comment below to include it)
# vendor/

dist/

# My Project
examples
output
logs

dist/
6 changes: 0 additions & 6 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ builds:
- darwin
archives:
- format: binary
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: "checksums.txt"
snapshot:
Expand Down
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"console": "integratedTerminal"
}
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2021 SaltFishPr
Copyright © 2022 saltfishpr saltfishpr@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ Download from [Release](https://github.com/saltfishpr/redis-viewer/releases).
| scroll up | detail scroll up |
| scroll down | detail scroll down |

config file directory:

- Windows: `%USERPROFILE%/redis-viewer.yml`
- Linux: `~/.config/redis-viewer/redis-viewer.yml`
default config file directory is `$HOME/.redis-viewer.yaml`

example config file:

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

- [x] Add load animation.
- [ ] Friendly value detail.
- [x] Friendly value detail.
- [ ] Add log view.

Build with [bubbletea](https://github.com/charmbracelet/bubbletea).
87 changes: 60 additions & 27 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
/*
Copyright © 2022 saltfishpr saltfishpr@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"context"
"fmt"
"log"
"os"

"github.com/saltfishpr/redis-viewer/internal/config"
tea "github.com/charmbracelet/bubbletea"
"github.com/saltfishpr/redis-viewer/internal/conf"
"github.com/saltfishpr/redis-viewer/internal/constant"
"github.com/saltfishpr/redis-viewer/internal/tui"

tea "github.com/charmbracelet/bubbletea"
"github.com/go-redis/redis/v8"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "redis-viewer",
Short: "view redis data in terminal.",
Long: `Redis Viewer is a tool to view redis data in terminal.`,
Run: func(cmd *cobra.Command, args []string) {
config.LoadConfig()
cfg := config.GetConfig()

rdb := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: cfg.Addrs,
DB: cfg.DB,
Username: cfg.Username,
Password: cfg.Password,
MaxRetries: constant.MaxRetries,
MaxRedirects: constant.MaxRedirects,
MasterName: cfg.MasterName,
})
_, err := rdb.Ping(context.Background()).Result()
config := conf.Get()

model, err := tui.New(config)
if err != nil {
log.Fatal("connect to redis failed: ", err)
log.Fatal(err)
}

p := tea.NewProgram(tui.New(rdb), tea.WithAltScreen(), tea.WithMouseCellMotion())
p := tea.NewProgram(model, tea.WithAltScreen(), tea.WithMouseCellMotion())
if err := p.Start(); err != nil {
log.Fatal("start failed: ", err)
}
Expand All @@ -54,13 +66,34 @@ func Execute() {
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
cobra.OnInitialize(initConfig)

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.redis-viewer.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.redis-viewer.yaml)")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".redis-viewer" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".redis-viewer")
}

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
viper.AutomaticEnv() // read in environment variables that match

viper.SetDefault("mode", "client")
viper.SetDefault("count", constant.DefaultCount)

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
63 changes: 0 additions & 63 deletions examples/main.go

This file was deleted.

11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module github.com/saltfishpr/redis-viewer
go 1.16

require (
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/bubbles v0.12.0
github.com/charmbracelet/bubbletea v0.22.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redis/v8 v8.11.5
github.com/json-iterator/go v1.1.12
github.com/muesli/reflow v0.3.0
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0
)
Loading

0 comments on commit 57e899d

Please sign in to comment.