1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 02:24:42 -05:00
sub2clash/utils/template.go

35 lines
615 B
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package utils
import (
"errors"
"io"
"os"
"path/filepath"
)
// LoadTemplate 加载模板
// templates 模板文件名
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-22 13:31:04 -04:00
if file != nil {
_ = 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
}