@@ -7,10 +7,8 @@ package options
7
7
8
8
import (
9
9
"fmt"
10
- "io/fs"
11
10
"os"
12
11
"path"
13
- "path/filepath"
14
12
15
13
"code.gitea.io/gitea/modules/log"
16
14
"code.gitea.io/gitea/modules/setting"
@@ -27,76 +25,20 @@ func Dir(name string) ([]string, error) {
27
25
28
26
var result []string
29
27
30
- customDir := path .Join (setting .CustomPath , "options" , name )
31
-
32
- isDir , err := util .IsDir (customDir )
33
- if err != nil {
34
- return []string {}, fmt .Errorf ("Unabe to check if custom directory %s is a directory. %w" , customDir , err )
35
- }
36
- if isDir {
37
- files , err := util .StatDir (customDir , true )
38
- if err != nil {
39
- return []string {}, fmt .Errorf ("Failed to read custom directory. %w" , err )
40
- }
41
-
42
- result = append (result , files ... )
43
- }
44
-
45
- staticDir := path .Join (setting .StaticRootPath , "options" , name )
46
-
47
- isDir , err = util .IsDir (staticDir )
48
- if err != nil {
49
- return []string {}, fmt .Errorf ("unable to check if static directory %s is a directory. %w" , staticDir , err )
50
- }
51
- if isDir {
52
- files , err := util .StatDir (staticDir , true )
28
+ for _ , dir := range []string {
29
+ path .Join (setting .CustomPath , "options" , name ), // custom dir
30
+ path .Join (setting .StaticRootPath , "options" , name ), // static dir
31
+ } {
32
+ files , err := statDirIfExist (dir )
53
33
if err != nil {
54
- return [] string {}, fmt . Errorf ( "Failed to read static directory. %w" , err )
34
+ return nil , err
55
35
}
56
-
57
36
result = append (result , files ... )
58
37
}
59
38
60
39
return directories .AddAndGet (name , result ), nil
61
40
}
62
41
63
- // Locale reads the content of a specific locale from static or custom path.
64
- func Locale (name string ) ([]byte , error ) {
65
- return fileFromDir (path .Join ("locale" , name ))
66
- }
67
-
68
- // WalkLocales reads the content of a specific locale from static or custom path.
69
- func WalkLocales (callback func (path , name string , d fs.DirEntry , err error ) error ) error {
70
- if err := walkAssetDir (filepath .Join (setting .StaticRootPath , "options" , "locale" ), callback ); err != nil && ! os .IsNotExist (err ) {
71
- return fmt .Errorf ("failed to walk locales. Error: %w" , err )
72
- }
73
-
74
- if err := walkAssetDir (filepath .Join (setting .CustomPath , "options" , "locale" ), callback ); err != nil && ! os .IsNotExist (err ) {
75
- return fmt .Errorf ("failed to walk locales. Error: %w" , err )
76
- }
77
- return nil
78
- }
79
-
80
- // Readme reads the content of a specific readme from static or custom path.
81
- func Readme (name string ) ([]byte , error ) {
82
- return fileFromDir (path .Join ("readme" , path .Clean ("/" + name )))
83
- }
84
-
85
- // Gitignore reads the content of a specific gitignore from static or custom path.
86
- func Gitignore (name string ) ([]byte , error ) {
87
- return fileFromDir (path .Join ("gitignore" , path .Clean ("/" + name )))
88
- }
89
-
90
- // License reads the content of a specific license from static or custom path.
91
- func License (name string ) ([]byte , error ) {
92
- return fileFromDir (path .Join ("license" , path .Clean ("/" + name )))
93
- }
94
-
95
- // Labels reads the content of a specific labels from static or custom path.
96
- func Labels (name string ) ([]byte , error ) {
97
- return fileFromDir (path .Join ("label" , path .Clean ("/" + name )))
98
- }
99
-
100
42
// fileFromDir is a helper to read files from static or custom path.
101
43
func fileFromDir (name string ) ([]byte , error ) {
102
44
customPath := path .Join (setting .CustomPath , "options" , name )
0 commit comments