Satisfy Staticcheck

This commit is contained in:
Thomas Miceli 2023-04-26 23:13:11 +02:00
parent 09507500a9
commit 91db4dcd30
No known key found for this signature in database
GPG Key ID: D86C6F6390AF050F
6 changed files with 29 additions and 18 deletions

View File

@ -272,6 +272,9 @@ func GetGitVersion() (string, error) {
func copyFiles(repositoryPath string) error { func copyFiles(repositoryPath string) error {
f1, err := os.OpenFile(filepath.Join(repositoryPath, "git-daemon-export-ok"), os.O_RDONLY|os.O_CREATE, 0644) f1, err := os.OpenFile(filepath.Join(repositoryPath, "git-daemon-export-ok"), os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f1.Close() defer f1.Close()
preReceiveDst, err := os.OpenFile(filepath.Join(repositoryPath, "hooks", "pre-receive"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0744) preReceiveDst, err := os.OpenFile(filepath.Join(repositoryPath, "hooks", "pre-receive"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0744)

View File

@ -181,7 +181,6 @@ func adminSyncReposFromDB(ctx echo.Context) error {
} }
} }
syncReposFromDB = false syncReposFromDB = false
return
}() }()
return redirect(ctx, "/admin-panel") return redirect(ctx, "/admin-panel")
} }

View File

@ -11,6 +11,8 @@ import (
"github.com/markbates/goth/providers/gitea" "github.com/markbates/goth/providers/gitea"
"github.com/markbates/goth/providers/github" "github.com/markbates/goth/providers/github"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gorm.io/gorm" "gorm.io/gorm"
"io" "io"
"net/http" "net/http"
@ -19,6 +21,8 @@ import (
"strings" "strings"
) )
var title = cases.Title(language.English)
func register(ctx echo.Context) error { func register(ctx echo.Context) error {
setData(ctx, "title", "New account") setData(ctx, "title", "New account")
setData(ctx, "htmlTitle", "New account") setData(ctx, "htmlTitle", "New account")
@ -134,10 +138,10 @@ func oauthCallback(ctx echo.Context) error {
} }
if err = currUser.Update(); err != nil { if err = currUser.Update(); err != nil {
return errorRes(500, "Cannot update user "+strings.Title(user.Provider)+" id", err) return errorRes(500, "Cannot update user "+title.String(user.Provider)+" id", err)
} }
addFlash(ctx, "Account linked to "+strings.Title(user.Provider), "success") addFlash(ctx, "Account linked to "+title.String(user.Provider), "success")
return redirect(ctx, "/settings") return redirect(ctx, "/settings")
} }
@ -268,16 +272,16 @@ func oauth(ctx echo.Context) error {
} }
if err != nil { if err != nil {
return errorRes(500, "Cannot unlink account from "+strings.Title(provider), err) return errorRes(500, "Cannot unlink account from "+title.String(provider), err)
} }
if isDelete { if isDelete {
addFlash(ctx, "Account unlinked from "+strings.Title(provider), "success") addFlash(ctx, "Account unlinked from "+title.String(provider), "success")
return redirect(ctx, "/settings") return redirect(ctx, "/settings")
} }
} }
ctxValue := context.WithValue(ctx.Request().Context(), "provider", provider) ctxValue := context.WithValue(ctx.Request().Context(), providerKey, provider)
ctx.SetRequest(ctx.Request().WithContext(ctxValue)) ctx.SetRequest(ctx.Request().WithContext(ctxValue))
if provider != "github" && provider != "gitea" { if provider != "github" && provider != "gitea" {
return errorRes(400, "Unsupported provider", nil) return errorRes(400, "Unsupported provider", nil)

View File

@ -20,9 +20,7 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
userName := ctx.Param("user") userName := ctx.Param("user")
gistName := ctx.Param("gistname") gistName := ctx.Param("gistname")
if strings.HasSuffix(gistName, ".git") {
gistName = strings.TrimSuffix(gistName, ".git") gistName = strings.TrimSuffix(gistName, ".git")
}
gist, err := models.GetGist(userName, gistName) gist, err := models.GetGist(userName, gistName)
if err != nil { if err != nil {
@ -123,7 +121,8 @@ func allGists(ctx echo.Context) error {
} else { } else {
setData(ctx, "htmlTitle", "All gists from "+fromUserStr) setData(ctx, "htmlTitle", "All gists from "+fromUserStr)
fromUser, err := models.GetUserByUsername(fromUserStr) var fromUser *models.User
fromUser, err = models.GetUserByUsername(fromUserStr)
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return notFound("User not found") return notFound("User not found")

View File

@ -47,13 +47,13 @@ var fm = template.FuncMap{
return strings.Split(i, "\n") return strings.Split(i, "\n")
}, },
"isMarkdown": func(i string) bool { "isMarkdown": func(i string) bool {
return ".md" == strings.ToLower(filepath.Ext(i)) return strings.ToLower(filepath.Ext(i)) == ".md"
}, },
"isCsv": func(i string) bool { "isCsv": func(i string) bool {
return ".csv" == strings.ToLower(filepath.Ext(i)) return strings.ToLower(filepath.Ext(i)) == ".csv"
}, },
"csvFile": func(file *git.File) *git.CsvFile { "csvFile": func(file *git.File) *git.CsvFile {
if ".csv" != strings.ToLower(filepath.Ext(file.Filename)) { if strings.ToLower(filepath.Ext(file.Filename)) != ".csv" {
return nil return nil
} }
@ -239,7 +239,7 @@ func Start() {
func dataInit(next echo.HandlerFunc) echo.HandlerFunc { func dataInit(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error { return func(ctx echo.Context) error {
ctxValue := context.WithValue(ctx.Request().Context(), "data", echo.Map{}) ctxValue := context.WithValue(ctx.Request().Context(), dataKey, echo.Map{})
ctx.SetRequest(ctx.Request().WithContext(ctxValue)) ctx.SetRequest(ctx.Request().WithContext(ctxValue))
setData(ctx, "loadStartTime", time.Now()) setData(ctx, "loadStartTime", time.Now())

View File

@ -18,15 +18,21 @@ import (
"strings" "strings"
) )
type providerKeyType string
type dataTypeKey string
const providerKey providerKeyType = "provider"
const dataKey dataTypeKey = "data"
func setData(ctx echo.Context, key string, value any) { func setData(ctx echo.Context, key string, value any) {
data := ctx.Request().Context().Value("data").(echo.Map) data := ctx.Request().Context().Value(dataKey).(echo.Map)
data[key] = value data[key] = value
ctxValue := context.WithValue(ctx.Request().Context(), "data", data) ctxValue := context.WithValue(ctx.Request().Context(), dataKey, data)
ctx.SetRequest(ctx.Request().WithContext(ctxValue)) ctx.SetRequest(ctx.Request().WithContext(ctxValue))
} }
func getData(ctx echo.Context, key string) any { func getData(ctx echo.Context, key string) any {
data := ctx.Request().Context().Value("data").(echo.Map) data := ctx.Request().Context().Value(dataKey).(echo.Map)
return data[key] return data[key]
} }
@ -36,7 +42,7 @@ func html(ctx echo.Context, template string) error {
func htmlWithCode(ctx echo.Context, code int, template string) error { func htmlWithCode(ctx echo.Context, code int, template string) error {
setErrorFlashes(ctx) setErrorFlashes(ctx)
return ctx.Render(code, template, ctx.Request().Context().Value("data")) return ctx.Render(code, template, ctx.Request().Context().Value(dataKey))
} }
func redirect(ctx echo.Context, location string) error { func redirect(ctx echo.Context, location string) error {