You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.6 KiB

  1. // +build bindata
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package options
  6. import (
  7. "fmt"
  8. "io/ioutil"
  9. "path"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/unknwon/com"
  12. )
  13. var (
  14. directories = make(directorySet)
  15. )
  16. // Dir returns all files from bindata or custom directory.
  17. func Dir(name string) ([]string, error) {
  18. if directories.Filled(name) {
  19. return directories.Get(name), nil
  20. }
  21. var (
  22. result []string
  23. )
  24. customDir := path.Join(setting.CustomPath, "options", name)
  25. if com.IsDir(customDir) {
  26. files, err := com.StatDir(customDir, true)
  27. if err != nil {
  28. return []string{}, fmt.Errorf("Failed to read custom directory. %v", err)
  29. }
  30. result = append(result, files...)
  31. }
  32. files, err := AssetDir(name)
  33. if err != nil {
  34. return []string{}, fmt.Errorf("Failed to read embedded directory. %v", err)
  35. }
  36. result = append(result, files...)
  37. return directories.AddAndGet(name, result), nil
  38. }
  39. func AssetDir(dirName string) ([]string, error) {
  40. d, err := Assets.Open(dirName)
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer d.Close()
  45. files, err := d.Readdir(-1)
  46. if err != nil {
  47. return nil, err
  48. }
  49. var results = make([]string, 0, len(files))
  50. for _, file := range files {
  51. results = append(results, file.Name())
  52. }
  53. return results, nil
  54. }
  55. // Locale reads the content of a specific locale from bindata or custom path.
  56. func Locale(name string) ([]byte, error) {
  57. return fileFromDir(path.Join("locale", name))
  58. }
  59. // Readme reads the content of a specific readme from bindata or custom path.
  60. func Readme(name string) ([]byte, error) {
  61. return fileFromDir(path.Join("readme", name))
  62. }
  63. // Gitignore reads the content of a gitignore locale from bindata or custom path.
  64. func Gitignore(name string) ([]byte, error) {
  65. return fileFromDir(path.Join("gitignore", name))
  66. }
  67. // License reads the content of a specific license from bindata or custom path.
  68. func License(name string) ([]byte, error) {
  69. return fileFromDir(path.Join("license", name))
  70. }
  71. // Labels reads the content of a specific labels from static or custom path.
  72. func Labels(name string) ([]byte, error) {
  73. return fileFromDir(path.Join("label", name))
  74. }
  75. // fileFromDir is a helper to read files from bindata or custom path.
  76. func fileFromDir(name string) ([]byte, error) {
  77. customPath := path.Join(setting.CustomPath, "options", name)
  78. if com.IsFile(customPath) {
  79. return ioutil.ReadFile(customPath)
  80. }
  81. f, err := Assets.Open(name)
  82. if err != nil {
  83. return nil, err
  84. }
  85. defer f.Close()
  86. return ioutil.ReadAll(f)
  87. }