fix: remove testing code, fix version print, change watch conf place

This commit is contained in:
Muyao CHEN 2024-10-02 23:26:42 +02:00
parent 792ee4ebe0
commit e45f4d992f
3 changed files with 8 additions and 16 deletions

View File

@ -28,6 +28,7 @@ import (
"strings" "strings"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -68,6 +69,12 @@ func initConfig() {
return return
} }
// watching reloading conf
viper.OnConfigChange(func(e fsnotify.Event) {
log.InfoLog("Config file changed:", e.Name)
})
viper.WatchConfig()
log.DebugLog("Using config file", "file", viper.ConfigFileUsed()) log.DebugLog("Using config file", "file", viper.ConfigFileUsed())
} }

View File

@ -23,13 +23,11 @@
package howmuch package howmuch
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag" "git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag"
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -89,19 +87,6 @@ to share their expense of an event or a trip`,
} }
func run() error { func run() error {
log.DebugLog("How much do I owe you?")
settings, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
// watching reloading conf
viper.OnConfigChange(func(e fsnotify.Event) {
log.InfoLog("Config file changed:", e.Name)
})
viper.WatchConfig()
log.InfoLog(string(settings))
log.InfoLog(viper.GetString("db.username"))
isDev := viper.GetBool("dev-mode") isDev := viper.GetBool("dev-mode")
if isDev { if isDev {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)

View File

@ -41,8 +41,8 @@ func AddFlags(fs *pflag.FlagSet) {
} }
func PrintVersion() { func PrintVersion() {
fmt.Printf("%s\n", version.Get().String())
if *doPrint { if *doPrint {
fmt.Printf("%s\n", version.Get().String())
os.Exit(0) os.Exit(0)
} }
} }