Opt: preload env file

pull/83/head
zijiren233 2 years ago
parent 2b4ca26675
commit 47d58d3784

@ -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,

@ -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,

@ -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,

@ -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()

@ -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() {

@ -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,

@ -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,

@ -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,

@ -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()

@ -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,

@ -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,

@ -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,

@ -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,

@ -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,

@ -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,

@ -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,

@ -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
}

@ -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
}

Loading…
Cancel
Save