usePackFile.go 1.1 KB

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