restructure

This commit is contained in:
yanghao05
2023-04-28 15:10:28 +08:00
parent 3a9d5492f8
commit ebcd25f776
6 changed files with 118 additions and 14 deletions

View File

@@ -4,6 +4,8 @@ import (
"context" "context"
"log" "log"
"github.com/rogeecn/atom/utils/opt"
"github.com/spf13/viper"
"go.uber.org/dig" "go.uber.org/dig"
) )
@@ -19,3 +21,20 @@ func init() {
log.Fatal(err) log.Fatal(err)
} }
} }
type ProviderContainer struct {
Provider func(...opt.Option) error
Options []opt.Option
}
type Providers []ProviderContainer
func (p Providers) Provide(config *viper.Viper) error {
for _, provider := range p {
provider.Options = append(provider.Options, opt.Config(config))
if err := provider.Provider(provider.Options...); err != nil {
return err
}
}
return nil
}

View File

@@ -1,4 +1,4 @@
package cmd package atom
import ( import (
"log" "log"

View File

@@ -1,7 +1,4 @@
/* package atom
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import ( import (
"errors" "errors"

View File

@@ -5,14 +5,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func AutoLoad() *viper.Viper {
v, err := Load("config.toml")
if err != nil {
return nil
}
return v
}
func Load(file string) (*viper.Viper, error) { func Load(file string) (*viper.Viper, error) {
v := viper.NewWithOptions(viper.KeyDelimiter("_")) v := viper.NewWithOptions(viper.KeyDelimiter("_"))
v.AutomaticEnv() v.AutomaticEnv()

96
root.go Normal file
View File

@@ -0,0 +1,96 @@
package atom
import (
"github.com/pkg/errors"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/config"
"github.com/spf13/cobra"
)
var cfgFile string
func Serve(providers container.Providers, opts ...Option) error {
var rootCmd = &cobra.Command{}
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.toml", "config file path")
for _, opt := range opts {
opt(rootCmd)
}
WithMigration(rootCmd)
WithModel(rootCmd)
WithSeeder(rootCmd)
// parse config files
configure, err := config.Load(cfgFile)
if err != nil {
return errors.Wrapf(err, "load config file: %s", cfgFile)
}
if err := providers.Provide(configure); err != nil {
return err
}
return rootCmd.Execute()
}
type Option func(*cobra.Command)
func Name(name string) Option {
return func(cmd *cobra.Command) {
cmd.Use = name
}
}
func Short(short string) Option {
return func(cmd *cobra.Command) {
cmd.Short = short
}
}
func Long(long string) Option {
return func(cmd *cobra.Command) {
cmd.Long = long
}
}
func Run(run func(cmd *cobra.Command, args []string)) Option {
return func(cmd *cobra.Command) {
cmd.Run = run
}
}
func RunE(run func(cmd *cobra.Command, args []string) error) Option {
return func(cmd *cobra.Command) {
cmd.RunE = run
}
}
func PostRun(run func(cmd *cobra.Command, args []string)) Option {
return func(cmd *cobra.Command) {
cmd.PostRun = run
}
}
func PostRunE(run func(cmd *cobra.Command, args []string) error) Option {
return func(cmd *cobra.Command) {
cmd.PostRunE = run
}
}
func PreRun(run func(cmd *cobra.Command, args []string)) Option {
return func(cmd *cobra.Command) {
cmd.PreRun = run
}
}
func PreRunE(run func(cmd *cobra.Command, args []string) error) Option {
return func(cmd *cobra.Command) {
cmd.PreRunE = run
}
}
func Config(file string) Option {
return func(cmd *cobra.Command) {
_ = cmd.PersistentFlags().Set("config", file)
}
}

View File

@@ -1,4 +1,4 @@
package cmd package atom
import ( import (
"log" "log"