feat: new provider command
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -21,3 +21,4 @@
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
test/*
|
||||
|
||||
@@ -15,6 +15,7 @@ func CommandInit(root *cobra.Command) {
|
||||
cmds := []func(*cobra.Command){
|
||||
CommandNewProject,
|
||||
CommandNewModule,
|
||||
CommandNewProvider,
|
||||
}
|
||||
|
||||
for _, c := range cmds {
|
||||
|
||||
81
cmd/new_provider.go
Normal file
81
cmd/new_provider.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"git.ipao.vip/rogeecn/atomctl/templates"
|
||||
"github.com/iancoleman/strcase"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// CommandNewProvider 注册 new_provider 命令
|
||||
func CommandNewProvider(root *cobra.Command) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "provider",
|
||||
Short: "创建新的 provider",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: commandNewProviderE,
|
||||
}
|
||||
|
||||
root.AddCommand(cmd)
|
||||
}
|
||||
|
||||
func commandNewProviderE(cmd *cobra.Command, args []string) error {
|
||||
providerName := args[0]
|
||||
targetPath := filepath.Join("providers", providerName)
|
||||
|
||||
if _, err := os.Stat(targetPath); err == nil {
|
||||
return fmt.Errorf("目录 %s 已存在", targetPath)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := fs.WalkDir(templates.Provider, "provider", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel("provider", path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destPath := filepath.Join(targetPath, strings.TrimSuffix(relPath, ".tpl"))
|
||||
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFS(templates.Provider, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destFile, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
return tmpl.Execute(destFile, map[string]string{
|
||||
"Name": providerName,
|
||||
"CamelName": strcase.ToCamel(providerName),
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("渲染 provider 模板失败")
|
||||
}
|
||||
|
||||
fmt.Printf("Provider 已创建: %s\n", targetPath)
|
||||
return nil
|
||||
}
|
||||
24
templates/provider/config.go.tpl
Normal file
24
templates/provider/config.go.tpl
Normal file
@@ -0,0 +1,24 @@
|
||||
package {{.Name}}
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "{{.CamelName}}"
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{
|
||||
opt.Prefix(DefaultPrefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
}
|
||||
17
templates/provider/provider.go.tpl
Normal file
17
templates/provider/provider.go.tpl
Normal file
@@ -0,0 +1,17 @@
|
||||
package {{.Name}}
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var config Config
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return container.Container.Provide(func() (*Config, error) {
|
||||
return &Config{}, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
@@ -7,3 +7,6 @@ var Project embed.FS
|
||||
|
||||
//go:embed module
|
||||
var Module embed.FS
|
||||
|
||||
//go:embed provider
|
||||
var Provider embed.FS
|
||||
|
||||
Reference in New Issue
Block a user