Skip to content

Commit

Permalink
补充ForceLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Jan 25, 2024
1 parent e679aee commit a0bffb5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 61 deletions.
57 changes: 0 additions & 57 deletions ioc/config/cmd/cobra.go

This file was deleted.

1 change: 0 additions & 1 deletion ioc/config/cmd/cobra_test.go

This file was deleted.

1 change: 0 additions & 1 deletion ioc/config/cmd/interface.go

This file was deleted.

10 changes: 10 additions & 0 deletions ioc/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ func DevelopmentSetup() {
}
}

var (
isLoaded bool
)

func ConfigIocObject(req *LoadConfigRequest) error {
if isLoaded && !req.ForceLoad {
return nil
}

// 加载对象的配置
err := store.LoadConfig(req)
if err != nil {
Expand Down Expand Up @@ -49,6 +57,8 @@ func NewLoadConfigRequest() *LoadConfigRequest {
}

type LoadConfigRequest struct {
// 默认加载后, 不允许重复加载, 这是为了避免多次初始化可能引发的问题
ForceLoad bool
// 环境变量配置
ConfigEnv *configEnv
// 文件配置方式
Expand Down
64 changes: 64 additions & 0 deletions ioc/server/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/application"
"github.com/infraboard/mcube/v2/ioc/server"
)

var (
confType string
confFile string
vers bool
)

// Root represents the base command when called without any subcommands
var Root = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
if vers {
fmt.Println(application.FullVersion())
return nil
}
return cmd.Help()
},
}

// 初始化Ioc
func initail() error {
req := server.DefaultConfig
switch confType {
case "file":
req.ConfigFile.Enabled = true
req.ConfigFile.Path = confFile
default:
req.ConfigEnv.Enabled = true
}

// 初始化ioc
if err := ioc.ConfigIocObject(server.DefaultConfig); err != nil {
return err
}

// 补充Root命令信息
Root.Use = application.Get().AppName
Root.Short = application.Get().AppDescription
Root.Long = application.Get().AppDescription
return nil
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(initail())
cobra.CheckErr(Root.Execute())
}

func init() {
Root.PersistentFlags().StringVarP(&confType, "config-type", "t", "file", "the service config type [file/env]")
Root.PersistentFlags().StringVarP(&confFile, "config-file", "f", "etc/application.toml", "the service config from file")
Root.PersistentFlags().BoolVarP(&vers, "version", "v", false, "the mcenter version")
}
6 changes: 4 additions & 2 deletions ioc/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

type defaultStore struct {
conf *LoadConfigRequest
store []*NamespaceStore
isLoaded bool
conf *LoadConfigRequest
store []*NamespaceStore
}

func (s *defaultStore) Len() int {
Expand Down Expand Up @@ -81,6 +82,7 @@ func (s *defaultStore) LoadConfig(req *LoadConfigRequest) error {
}

s.conf = req
s.isLoaded = true
return nil
}

Expand Down

0 comments on commit a0bffb5

Please sign in to comment.