add
wangpengfei
2023-08-26 2083c6a4f3c69d7f0f1deca562b50df3e8bfd91e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//go:build packfile
// +build packfile
 
package packfile
 
import (
    "fmt"
    "os"
    "path/filepath"
    "strings"
)
 
//go:generate go-bindata -o=staticFile.go -pkg=packfile -tags=packfile ../resource/... ../config.yaml
 
func writeFile(path string, data []byte) {
    // 如果文件夹不存在,预先创建文件夹
    if lastSeparator := strings.LastIndex(path, "/"); lastSeparator != -1 {
        dirPath := path[:lastSeparator]
        if _, err := os.Stat(dirPath); err != nil && os.IsNotExist(err) {
            os.MkdirAll(dirPath, os.ModePerm)
        }
    }
 
    // 已存在的文件,不应该覆盖重写,可能在前端更改了配置文件等
    if _, err := os.Stat(path); os.IsNotExist(err) {
        if err2 := os.WriteFile(path, data, os.ModePerm); err2 != nil {
            fmt.Printf("Write file failed: %s\n", path)
        }
    } else {
        fmt.Printf("File exist, skip: %s\n", path)
    }
}
 
func init() {
    for key := range _bindata {
        filePath, _ := filepath.Abs(strings.TrimPrefix(key, "."))
        data, err := Asset(key)
        if err != nil {
            // Asset was not found.
            fmt.Printf("Fail to find: %s\n", filePath)
        } else {
            writeFile(filePath, data)
        }
    }
}