Feat: Refine the init process

pull/21/head
zijiren233 1 year ago
parent 51235aae70
commit 6af3409727

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

@ -14,5 +14,5 @@ var (
EnvNoPrefix bool
DisableUpdateCheck bool
GitHubBaseURL string
)

@ -0,0 +1,5 @@
package flags
var (
DisableUpdateCheck bool
)

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

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

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

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

@ -1,6 +1,8 @@
package bootstrap
import "context"
import (
"context"
)
type BootstrapConf func(*Bootstrap)

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

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

@ -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/",
}
}
Loading…
Cancel
Save