usePackFile.go 1.1 KB

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