1 Star 0 Fork 0

bughou / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
walk.go 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
bughou 提交于 2022-03-21 20:37 . save
package new
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"gitee.com/go-better/dev/os/fs"
)
func walk(tmplsDir, proDir string, config *Config, force bool) error {
return filepath.Walk(tmplsDir, func(src string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dst := strings.Replace(src, tmplsDir, proDir, 1)
if info.IsDir() {
if err := os.MkdirAll(dst, 0755); err == os.ErrExist {
return nil
} else {
return err
}
} else {
return copyFile(src, dst, info, config, force)
}
})
}
func copyFile(src, dst string, info os.FileInfo, config *Config, force bool) error {
dir, file := filepath.Split(dst)
isTmpl := strings.HasPrefix(file, `__`)
if isTmpl {
dst = filepath.Join(dir, strings.TrimPrefix(file, `__`))
}
if !force && fs.Exist(dst) {
return fmt.Errorf(`%s: aready exists, use "-f" flag to override.`, dst)
}
if isTmpl {
if content, err := renderTmpl(src, config); err == nil {
return ioutil.WriteFile(dst, content, 0644)
} else {
return err
}
}
return fs.CopyWithMode(src, dst, 0644)
}
func renderTmpl(tmplPath string, config *Config) ([]byte, error) {
tmpl, err := template.New(filepath.Base(tmplPath)).Funcs(template.FuncMap{
"genSecret": genSecret,
}).ParseFiles(tmplPath)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, config); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
Go
1
https://gitee.com/bughou/go.git
git@gitee.com:bughou/go.git
bughou
go
go
d31700df43a9

搜索帮助