From 6af34097273ae4e38cf604638321f2a95f72425f Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Fri, 13 Oct 2023 21:39:32 +0800 Subject: [PATCH] Feat: Refine the init process --- cmd/conf.go | 11 ++++++----- cmd/flags/config.go | 2 +- cmd/flags/server.go | 5 +++++ cmd/root.go | 1 + cmd/self-update.go | 7 +++---- cmd/server.go | 2 +- internal/bootstrap/config.go | 5 +++++ internal/bootstrap/init.go | 4 +++- internal/bootstrap/log.go | 5 +++++ internal/conf/config.go | 6 ------ internal/conf/global.go | 11 ----------- 11 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 cmd/flags/server.go delete mode 100644 internal/conf/global.go diff --git a/cmd/conf.go b/cmd/conf.go index e7191ac..eecb036 100644 --- a/cmd/conf.go +++ b/cmd/conf.go @@ -12,14 +12,15 @@ var ConfCmd = &cobra.Command{ Use: "conf", Short: "conf", Long: `config file`, - RunE: Conf, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( + bootstrap.InitConfig, + ).Run() + }, + RunE: Conf, } func Conf(cmd *cobra.Command, args []string) error { - err := bootstrap.InitConfig(cmd.Context()) - if err != nil { - return err - } fmt.Println(conf.Conf.String()) return nil } diff --git a/cmd/flags/config.go b/cmd/flags/config.go index ad788c4..4ed6aab 100644 --- a/cmd/flags/config.go +++ b/cmd/flags/config.go @@ -14,5 +14,5 @@ var ( EnvNoPrefix bool - DisableUpdateCheck bool + GitHubBaseURL string ) diff --git a/cmd/flags/server.go b/cmd/flags/server.go new file mode 100644 index 0000000..d358e05 --- /dev/null +++ b/cmd/flags/server.go @@ -0,0 +1,5 @@ +package flags + +var ( + DisableUpdateCheck bool +) diff --git a/cmd/root.go b/cmd/root.go index 7fe77c7..f02afbf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,4 +29,5 @@ func init() { RootCmd.PersistentFlags().BoolVar(&flags.SkipConfig, "skip-config", false, "skip config") RootCmd.PersistentFlags().BoolVar(&flags.SkipEnv, "skip-env", false, "skip env") RootCmd.PersistentFlags().StringVarP(&flags.ConfigFile, "config", "f", "", "config file path") + RootCmd.PersistentFlags().StringVar(&flags.GitHubBaseURL, "github-base-url", "https://api.github.com/", "github api base url") } diff --git a/cmd/self-update.go b/cmd/self-update.go index f73e058..d096a7b 100644 --- a/cmd/self-update.go +++ b/cmd/self-update.go @@ -3,8 +3,8 @@ package cmd import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/synctv-org/synctv/cmd/flags" "github.com/synctv-org/synctv/internal/bootstrap" - "github.com/synctv-org/synctv/internal/conf" "github.com/synctv-org/synctv/internal/version" ) @@ -20,15 +20,14 @@ var SelfUpdateCmd = &cobra.Command{ Long: SelfUpdateLong, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add( - bootstrap.InitConfig, - bootstrap.InitLog, + bootstrap.InitStdLog, ).Run() }, RunE: SelfUpdate, } func SelfUpdate(cmd *cobra.Command, args []string) error { - v, err := version.NewVersionInfo(version.WithBaseURL(conf.Conf.Global.GitHubBaseURL)) + v, err := version.NewVersionInfo(version.WithBaseURL(flags.GitHubBaseURL)) if err != nil { log.Errorf("get version info error: %v", err) return err diff --git a/cmd/server.go b/cmd/server.go index a825926..bd92120 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -125,5 +125,5 @@ func Server(cmd *cobra.Command, args []string) { func init() { RootCmd.AddCommand(ServerCmd) - ServerCmd.Flags().BoolVar(&flags.DisableUpdateCheck, "disable-update-check", false, "disable update check") + ServerCmd.PersistentFlags().BoolVar(&flags.DisableUpdateCheck, "disable-update-check", false, "disable update check") } diff --git a/internal/bootstrap/config.go b/internal/bootstrap/config.go index a68ab8f..e3babe2 100644 --- a/internal/bootstrap/config.go +++ b/internal/bootstrap/config.go @@ -14,6 +14,11 @@ import ( "github.com/synctv-org/synctv/utils" ) +func InitDefaultConfig(ctx context.Context) error { + conf.Conf = conf.DefaultConfig() + return nil +} + func InitConfig(ctx context.Context) error { if flags.SkipConfig && flags.SkipEnv { log.Fatal("skip config and skip env at the same time") diff --git a/internal/bootstrap/init.go b/internal/bootstrap/init.go index 098f901..558cc3c 100644 --- a/internal/bootstrap/init.go +++ b/internal/bootstrap/init.go @@ -1,6 +1,8 @@ package bootstrap -import "context" +import ( + "context" +) type BootstrapConf func(*Bootstrap) diff --git a/internal/bootstrap/log.go b/internal/bootstrap/log.go index 9a3102f..22217a2 100644 --- a/internal/bootstrap/log.go +++ b/internal/bootstrap/log.go @@ -63,3 +63,8 @@ func InitLog(ctx context.Context) error { log.SetOutput(logrus.StandardLogger().Out) return nil } + +func InitStdLog(ctx context.Context) error { + setLog(logrus.StandardLogger()) + return nil +} diff --git a/internal/conf/config.go b/internal/conf/config.go index 7ff5dce..f433a63 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -7,9 +7,6 @@ import ( ) type Config struct { - // Global - Global GlobalConfig `yaml:"global"` - // Log Log LogConfig `yaml:"log"` @@ -37,9 +34,6 @@ func (c *Config) String() string { func DefaultConfig() *Config { return &Config{ - // Global - Global: DefaultGlobalConfig(), - // Log Log: DefaultLogConfig(), diff --git a/internal/conf/global.go b/internal/conf/global.go deleted file mode 100644 index 496ccb7..0000000 --- a/internal/conf/global.go +++ /dev/null @@ -1,11 +0,0 @@ -package conf - -type GlobalConfig struct { - GitHubBaseURL string `yaml:"github_base_url" lc:"default: https://api.github.com/" env:"GITHUB_BASE_URL"` -} - -func DefaultGlobalConfig() GlobalConfig { - return GlobalConfig{ - GitHubBaseURL: "https://api.github.com/", - } -}