2024-04-23 02:47:53 -04:00
|
|
|
package common
|
2023-09-20 21:08:02 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-09-17 01:10:13 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2clash/config"
|
2023-09-20 21:08:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
}(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
|
|
|
|
}
|