1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 01:34:41 -05:00
sub2clash/utils/template.go

33 lines
588 B
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package utils
import (
"errors"
"io"
"os"
"path/filepath"
)
// LoadTemplate 加载模板
// template 模板文件名
2023-09-13 01:47:22 -04:00
func LoadTemplate(template string) ([]byte, error) {
2023-09-12 06:40:24 -04:00
tPath := filepath.Join("templates", template)
if _, err := os.Stat(tPath); err == nil {
file, err := os.Open(tPath)
if err != nil {
2023-09-13 01:47:22 -04:00
return nil, err
2023-09-12 06:40:24 -04:00
}
defer func(file *os.File) {
2023-09-13 01:47:22 -04:00
_ = file.Close()
2023-09-12 06:40:24 -04:00
}(file)
result, err := io.ReadAll(file)
if err != nil {
2023-09-13 01:47:22 -04:00
return nil, err
2023-09-12 06:40:24 -04:00
}
if err != nil {
2023-09-13 01:47:22 -04:00
return nil, err
2023-09-12 06:40:24 -04:00
}
2023-09-13 01:47:22 -04:00
return result, nil
2023-09-12 06:40:24 -04:00
}
2023-09-13 01:47:22 -04:00
return nil, errors.New("模板文件不存在")
2023-09-12 06:40:24 -04:00
}