From 47d58d3784c6c16d1cc66f1d79ca0e1368aeac9a Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Sun, 18 Feb 2024 23:08:19 +0800 Subject: [PATCH] Opt: preload env file --- cmd/admin/add.go | 2 +- cmd/admin/delete.go | 2 +- cmd/admin/show.go | 2 +- cmd/conf.go | 2 +- cmd/root.go | 24 +++++++++++++++++++++ cmd/root/add.go | 2 +- cmd/root/delete.go | 2 +- cmd/root/show.go | 2 +- cmd/self-update.go | 2 +- cmd/server.go | 2 +- cmd/setting/set.go | 2 +- cmd/setting/show.go | 2 +- cmd/user/ban.go | 2 +- cmd/user/delete.go | 2 +- cmd/user/search.go | 2 +- cmd/user/unban.go | 2 +- internal/bootstrap/config.go | 42 ------------------------------------ utils/utils.go | 17 +++++++++++++++ 18 files changed, 56 insertions(+), 57 deletions(-) diff --git a/cmd/admin/add.go b/cmd/admin/add.go index e4f0e7d..cb5b282 100644 --- a/cmd/admin/add.go +++ b/cmd/admin/add.go @@ -13,7 +13,7 @@ var AddCmd = &cobra.Command{ Use: "add", Short: "add admin by user id", Long: `add admin by user id`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/admin/delete.go b/cmd/admin/delete.go index d26dd30..8c88992 100644 --- a/cmd/admin/delete.go +++ b/cmd/admin/delete.go @@ -13,7 +13,7 @@ var RemoveCmd = &cobra.Command{ Use: "remove", Short: "remove", Long: `remove admin`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/admin/show.go b/cmd/admin/show.go index 2f34fe6..ac0e58e 100644 --- a/cmd/admin/show.go +++ b/cmd/admin/show.go @@ -12,7 +12,7 @@ var ShowCmd = &cobra.Command{ Use: "show", Short: "show admin", Long: `show admin`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/conf.go b/cmd/conf.go index 7955caa..35c4e86 100644 --- a/cmd/conf.go +++ b/cmd/conf.go @@ -10,7 +10,7 @@ var ConfCmd = &cobra.Command{ Use: "conf", Short: "init or check", Long: `Init or check config file for correctness`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitConfig, ).Run() diff --git a/cmd/root.go b/cmd/root.go index 170f277..361483e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,7 +5,10 @@ import ( "os" "path/filepath" + "github.com/go-kratos/kratos/v2/log" + "github.com/joho/godotenv" "github.com/mitchellh/go-homedir" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/synctv-org/synctv/cmd/admin" "github.com/synctv-org/synctv/cmd/flags" @@ -13,12 +16,33 @@ import ( "github.com/synctv-org/synctv/cmd/setting" "github.com/synctv-org/synctv/cmd/user" "github.com/synctv-org/synctv/internal/version" + "github.com/synctv-org/synctv/utils" ) var RootCmd = &cobra.Command{ Use: "synctv", Short: "synctv", Long: `synctv https://github.com/synctv-org/synctv`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + s, err := utils.GetEnvFiles(flags.DataDir) + if err != nil { + logrus.Fatalf("get env files error: %v", err) + } + if flags.Dev { + ss, err := utils.GetEnvFiles(".") + if err != nil { + logrus.Fatalf("get env files error: %v", err) + } + s = append(s, ss...) + } + if len(s) != 0 { + log.Infof("Overload env from: %v", s) + err = godotenv.Overload(s...) + if err != nil { + logrus.Fatalf("load env error: %v", err) + } + } + }, } func Execute() { diff --git a/cmd/root/add.go b/cmd/root/add.go index 542ac95..9d6146a 100644 --- a/cmd/root/add.go +++ b/cmd/root/add.go @@ -13,7 +13,7 @@ var AddCmd = &cobra.Command{ Use: "add", Short: "add root by user id", Long: `add root by user id`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/root/delete.go b/cmd/root/delete.go index 54fc241..a97f121 100644 --- a/cmd/root/delete.go +++ b/cmd/root/delete.go @@ -13,7 +13,7 @@ var RemoveCmd = &cobra.Command{ Use: "remove", Short: "remove", Long: `remove root`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/root/show.go b/cmd/root/show.go index 1649914..91a603a 100644 --- a/cmd/root/show.go +++ b/cmd/root/show.go @@ -12,7 +12,7 @@ var ShowCmd = &cobra.Command{ Use: "show", Short: "show root", Long: `show root`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/self-update.go b/cmd/self-update.go index d096a7b..38b5dc2 100644 --- a/cmd/self-update.go +++ b/cmd/self-update.go @@ -18,7 +18,7 @@ var SelfUpdateCmd = &cobra.Command{ Use: "self-update", Short: "self-update", Long: SelfUpdateLong, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitStdLog, ).Run() diff --git a/cmd/server.go b/cmd/server.go index 1b9c319..cbbd771 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -22,7 +22,7 @@ var ServerCmd = &cobra.Command{ Use: "server", Short: "Start synctv-server", Long: `Start synctv-server`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { boot := bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitSysNotify, bootstrap.InitConfig, diff --git a/cmd/setting/set.go b/cmd/setting/set.go index 456b323..4912932 100644 --- a/cmd/setting/set.go +++ b/cmd/setting/set.go @@ -13,7 +13,7 @@ var SetCmd = &cobra.Command{ Use: "set", Short: "set setting", Long: `set setting`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/setting/show.go b/cmd/setting/show.go index e128ef0..bb0bfe1 100644 --- a/cmd/setting/show.go +++ b/cmd/setting/show.go @@ -14,7 +14,7 @@ var ShowCmd = &cobra.Command{ Use: "show", Short: "show setting", Long: `show setting`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/user/ban.go b/cmd/user/ban.go index f9eacb1..a3d2f35 100644 --- a/cmd/user/ban.go +++ b/cmd/user/ban.go @@ -13,7 +13,7 @@ var BanCmd = &cobra.Command{ Use: "ban", Short: "ban user with user id", Long: "ban user with user id", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/user/delete.go b/cmd/user/delete.go index 5d59b40..4c2e30f 100644 --- a/cmd/user/delete.go +++ b/cmd/user/delete.go @@ -13,7 +13,7 @@ var DeleteCmd = &cobra.Command{ Use: "delete", Short: "delete", Long: `delete user`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/user/search.go b/cmd/user/search.go index dc66d1d..216c0b8 100644 --- a/cmd/user/search.go +++ b/cmd/user/search.go @@ -13,7 +13,7 @@ var SearchCmd = &cobra.Command{ Use: "search", Short: "search user by id or username", Long: `search user by id or username`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/cmd/user/unban.go b/cmd/user/unban.go index 0e09e4b..3eb20b8 100644 --- a/cmd/user/unban.go +++ b/cmd/user/unban.go @@ -13,7 +13,7 @@ var UnbanCmd = &cobra.Command{ Use: "unban", Short: "unban user with user id", Long: "unban user with user id", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( bootstrap.InitDiscardLog, bootstrap.InitConfig, diff --git a/internal/bootstrap/config.go b/internal/bootstrap/config.go index df53549..7988bf3 100644 --- a/internal/bootstrap/config.go +++ b/internal/bootstrap/config.go @@ -3,12 +3,9 @@ package bootstrap import ( "context" "errors" - "os" "path/filepath" - "strings" "github.com/caarlos0/env/v9" - "github.com/joho/godotenv" log "github.com/sirupsen/logrus" "github.com/synctv-org/synctv/cmd/flags" @@ -87,46 +84,7 @@ func restoreConfig(filePath string, conf *conf.Config) error { } func confFromEnv(prefix string, conf *conf.Config) error { - s, err := getEnvFiles(flags.DataDir) - if err != nil { - return err - } - if flags.Dev { - ss, err := getEnvFiles(".") - if err != nil { - return err - } - s = append(s, ss...) - } - if len(s) != 0 { - err = godotenv.Overload(s...) - if err != nil { - return err - } - } return env.ParseWithOptions(conf, env.Options{ Prefix: prefix, }) } - -func getEnvFiles(root string) ([]string, error) { - var files []string - - err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !info.IsDir() && strings.HasPrefix(info.Name(), ".env") { - files = append(files, path) - } - - return nil - }) - - if err != nil { - return nil, err - } - - return files, nil -} diff --git a/utils/utils.go b/utils/utils.go index 1fe6274..0453c4e 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -398,3 +398,20 @@ func TruncateByRune(s string, length int) string { } panic("truncate by rune error") } + +func GetEnvFiles(root string) ([]string, error) { + var envs []string + + files, err := os.ReadDir(root) + if err != nil { + return nil, err + } + + for _, file := range files { + if !file.IsDir() && strings.HasPrefix(file.Name(), ".env") { + envs = append(envs, file.Name()) + } + } + + return envs, nil +}