Skip to content

Commit

Permalink
simplify os.ReadFile, fine tune messages and comments, fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 21, 2023
1 parent 6211415 commit 8525289
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
12 changes: 6 additions & 6 deletions modules/options/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func readFileFromLocal(base []string, sub string, elems ...string) ([]byte, erro
for _, dir := range base {
localPathElems[0] = dir
localPath := util.SafeFilePathAbs(localPathElems...)
isFile, err := util.IsFile(localPath)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", localPath, err)
} else if isFile {
return os.ReadFile(localPath)
data, err := os.ReadFile(localPath)
if err == nil {
return data, nil
} else if !os.IsNotExist(err) {
log.Error("Unable to read file %q. Error: %v", localPath, err)
}
}
return nil, fmt.Errorf("asset file does not exist: %v", elems)
return nil, fmt.Errorf("local file does not exist: %v", elems)
}
2 changes: 1 addition & 1 deletion modules/options/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Dir(name string) ([]string, error) {
return directories.AddAndGet(name, result), nil
}

// fileFromOptionsDir is a helper to read files from static or custom path.
// fileFromOptionsDir is a helper to read files from custom or static path.
func fileFromOptionsDir(elems ...string) ([]byte, error) {
return readFileFromLocal([]string{setting.CustomPath, setting.StaticRootPath}, "options", elems...)
}
Expand Down
5 changes: 4 additions & 1 deletion modules/options/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ package options
import (
"fmt"
"io"
"path/filepath"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

var directories = make(directorySet)
Expand Down Expand Up @@ -62,11 +64,12 @@ func AssetDir(dirName string) ([]string, error) {

// fileFromOptionsDir is a helper to read files from custom path or bindata.
func fileFromOptionsDir(elems ...string) ([]byte, error) {
// only try custom dir, no static dir
if data, err := readFileFromLocal([]string{setting.CustomPath}, "options", elems...); err == nil {
return data, nil
}

f, err := Assets.Open(name)
f, err := Assets.Open(util.SafePathRelX(elems...))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8525289

Please sign in to comment.