Skip to content

Commit e2812ca

Browse files
author
Aly Sewelam
committed
Resolve linting issues
Modernise string suffix check with CutSuffix Added nilcheck for entry after the org version check attempt
1 parent 83353a8 commit e2812ca

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

routers/web/repo/wiki.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func wikiEntryByName(ctx *context.Context, commit *git.Commit, wikiName wiki_ser
154154
}
155155
if entry == nil {
156156
// If .md file not found, try .org file
157-
if strings.HasSuffix(gitFilename, ".md") {
158-
orgFilename := strings.TrimSuffix(gitFilename, ".md") + ".org"
157+
if base, ok := strings.CutSuffix(gitFilename, ".md"); ok {
158+
orgFilename := base + ".org"
159159
entry, err = findEntryForFile(commit, orgFilename)
160160
if err != nil && !git.IsErrNotExist(err) {
161161
ctx.ServerError("findEntryForFile", err)
@@ -168,10 +168,10 @@ func wikiEntryByName(ctx *context.Context, commit *git.Commit, wikiName wiki_ser
168168
// If still not found, check if the file without extension exists (for raw files)
169169
if entry == nil {
170170
baseFilename := gitFilename
171-
if strings.HasSuffix(baseFilename, ".md") {
172-
baseFilename = strings.TrimSuffix(baseFilename, ".md")
173-
} else if strings.HasSuffix(baseFilename, ".org") {
174-
baseFilename = strings.TrimSuffix(baseFilename, ".org")
171+
if base, ok := strings.CutSuffix(baseFilename, ".md"); ok {
172+
baseFilename = base
173+
} else if base, ok := strings.CutSuffix(baseFilename, ".org"); ok {
174+
baseFilename = base
175175
}
176176
entry, err = findEntryForFile(commit, baseFilename)
177177
if err != nil && !git.IsErrNotExist(err) {
@@ -678,10 +678,12 @@ func WikiRaw(ctx *context.Context) {
678678
ctx.ServerError("findFile", err)
679679
return
680680
}
681-
entry, err = findEntryForFile(commit, providedGitPath)
682-
if err != nil && !git.IsErrNotExist(err) {
683-
ctx.ServerError("findFile", err)
684-
return
681+
if entry == nil {
682+
entry, err = findEntryForFile(commit, providedGitPath)
683+
if err != nil && !git.IsErrNotExist(err) {
684+
ctx.ServerError("findFile", err)
685+
return
686+
}
685687
}
686688
}
687689
}

services/wiki/wiki_path.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ func WebPathToGitPath(s WebPath) string {
114114

115115
func GitPathToWebPath(s string) (wp WebPath, err error) {
116116
// Trim .md or .org suffix if present
117-
if strings.HasSuffix(s, ".md") {
118-
s = strings.TrimSuffix(s, ".md")
119-
} else if strings.HasSuffix(s, ".org") {
120-
s = strings.TrimSuffix(s, ".org")
117+
if before, ok := strings.CutSuffix(s, ".md"); ok {
118+
s = before
119+
} else if before, ok := strings.CutSuffix(s, ".org"); ok {
120+
s = before
121121
} else {
122122
// If it doesn't end with .md or .org, it's not a valid wiki file
123123
return "", repo_model.ErrWikiInvalidFileName{FileName: s}
@@ -139,8 +139,8 @@ func WebPathToUserTitle(s WebPath) (dir, display string) {
139139
if before, ok := strings.CutSuffix(display, ".md"); ok {
140140
display = before
141141
display, _ = url.PathUnescape(display)
142-
} else if strings.HasSuffix(display, ".org") {
143-
display = strings.TrimSuffix(display, ".org")
142+
} else if before, ok := strings.CutSuffix(display, ".org"); ok {
143+
display = before
144144
display, _ = url.PathUnescape(display)
145145
}
146146
display, _ = unescapeSegment(display)

0 commit comments

Comments
 (0)