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

41 lines
782 B
Go
Raw Normal View History

package common
import (
"os"
"path/filepath"
"github.com/nitezs/sub2clash/config"
)
func writeTemplate(path string, template string) error {
tPath := filepath.Join(
"templates", path,
)
if _, err := os.Stat(tPath); os.IsNotExist(err) {
file, err := os.Create(tPath)
if err != nil {
return err
}
defer func(file *os.File) {
2023-09-22 13:31:04 -04:00
if file != nil {
_ = file.Close()
}
}(file)
_, err = file.WriteString(template)
if err != nil {
return err
}
}
return nil
}
func WriteDefalutTemplate(templateMeta string, templateClash string) error {
if err := writeTemplate(config.Default.MetaTemplate, templateMeta); err != nil {
return err
}
if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
return err
}
return nil
}