Skip to content

Commit bbd6767

Browse files
committed
Use fmt.Sprintf but vars.Expand when possible
1 parent b8370d0 commit bbd6767

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

modules/context/repo.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"code.gitea.io/gitea/modules/markup/markdown"
2626
"code.gitea.io/gitea/modules/setting"
2727
api "code.gitea.io/gitea/modules/structs"
28-
"code.gitea.io/gitea/modules/templates/vars"
2928
"code.gitea.io/gitea/modules/util"
3029
asymkey_service "code.gitea.io/gitea/services/asymkey"
3130

@@ -308,17 +307,11 @@ func EarlyResponseForGoGetMeta(ctx *Context) {
308307
ctx.PlainText(http.StatusBadRequest, "invalid repository path")
309308
return
310309
}
311-
res, err := vars.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`,
312-
map[string]string{
313-
"GoGetImport": ComposeGoGetImport(username, reponame),
314-
"CloneLink": repo_model.ComposeHTTPSCloneURL(username, reponame),
315-
})
316-
if err != nil {
317-
log.Error(err.Error())
318-
ctx.PlainText(http.StatusInternalServerError, "expand vars failed")
319-
return
320-
}
321-
ctx.PlainText(http.StatusOK, res)
310+
311+
ctx.PlainText(http.StatusOK, fmt.Sprintf(`<meta name="go-import" content="%s git %s">`,
312+
ComposeGoGetImport(username, reponame),
313+
repo_model.ComposeHTTPSCloneURL(username, reponame),
314+
))
322315
}
323316

324317
// RedirectToRepo redirect to a differently-named repository

routers/web/goget.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ package web
77
import (
88
"net/http"
99
"net/url"
10+
"os"
1011
"path"
1112
"strings"
1213

1314
repo_model "code.gitea.io/gitea/models/repo"
1415
"code.gitea.io/gitea/modules/context"
15-
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
17-
"code.gitea.io/gitea/modules/templates/vars"
1817
"code.gitea.io/gitea/modules/util"
1918
)
2019

@@ -67,35 +66,37 @@ func goGet(ctx *context.Context) {
6766
}
6867
ctx.RespHeader().Set("Content-Type", "text/html")
6968
ctx.Status(http.StatusOK)
70-
res, err := vars.Expand(`<!doctype html>
69+
res := os.Expand(`<!doctype html>
7170
<html>
7271
<head>
73-
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
74-
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
72+
<meta name="go-import" content="${GoGetImport} git ${CloneLink}">
73+
<meta name="go-source" content="${GoGetImport} _ ${GoDocDirectory} ${GoDocFile}">
7574
</head>
7675
<body>
77-
go get {Insecure}{GoGetImport}
76+
go get ${Insecure}${GoGetImport}
7877
</body>
7978
</html>
80-
`, map[string]string{
81-
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName),
82-
"CloneLink": repo_model.ComposeHTTPSCloneURL(ownerName, repoName),
83-
"GoDocDirectory": prefix + "{/dir}",
84-
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
85-
"Insecure": insecure,
79+
`, func(key string) string {
80+
switch key {
81+
case "GoGetImport":
82+
return context.ComposeGoGetImport(ownerName, trimmedRepoName)
83+
case "CloneLink":
84+
return repo_model.ComposeHTTPSCloneURL(ownerName, repoName)
85+
case "GoDocDirectory":
86+
return prefix + "{/dir}"
87+
case "GoDocFile":
88+
return prefix + "{/dir}/{file}#L{line}"
89+
case "Insecure":
90+
return insecure
91+
default:
92+
return `<!doctype html>
93+
<html>
94+
<body>
95+
invalid import path
96+
</body>
97+
</html>
98+
`
99+
}
86100
})
87-
if err != nil {
88-
log.Error(err.Error())
89-
_, _ = ctx.Write([]byte(`<!doctype html>
90-
<html>
91-
<body>
92-
invalid import path
93-
</body>
94-
</html>
95-
`))
96-
ctx.Status(400)
97-
return
98-
}
99-
100101
_, _ = ctx.Write([]byte(res))
101102
}

0 commit comments

Comments
 (0)