use goctl template to generate all kinds of templates
This commit is contained in:
72
tools/goctl/util/files.go
Normal file
72
tools/goctl/util/files.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const goctlDir = ".goctl"
|
||||
|
||||
func GetTemplateDir(category string) (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(home, goctlDir, category), nil
|
||||
}
|
||||
|
||||
func InitTemplates(category string, templates map[string]string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := MkdirIfNotExist(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range templates {
|
||||
if err := createTemplate(filepath.Join(dir, k), v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadTemplate(category, file, builtin string) (string, error) {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
file = filepath.Join(dir, file)
|
||||
if !FileExists(file) {
|
||||
return builtin, nil
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
func createTemplate(file, content string) error {
|
||||
if FileExists(file) {
|
||||
println(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Create(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(content)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user