1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 15:04:41 -05:00
sub2clash/common/template.go

30 lines
517 B
Go
Raw Permalink Normal View History

package common
2023-09-12 06:40:24 -04:00
import (
"errors"
"io"
"os"
"path/filepath"
)
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
}
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
}