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 {
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()
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
return
}()
return redirect(ctx, "/admin-panel")
}

View File

@ -11,6 +11,8 @@ import (
"github.com/markbates/goth/providers/gitea"
"github.com/markbates/goth/providers/github"
"github.com/rs/zerolog/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gorm.io/gorm"
"io"
"net/http"
@ -19,6 +21,8 @@ import (
"strings"
)
var title = cases.Title(language.English)
func register(ctx echo.Context) error {
setData(ctx, "title", "New account")
setData(ctx, "htmlTitle", "New account")
@ -134,10 +138,10 @@ func oauthCallback(ctx echo.Context) error {
}
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")
}
@ -268,16 +272,16 @@ func oauth(ctx echo.Context) error {
}
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 {
addFlash(ctx, "Account unlinked from "+strings.Title(provider), "success")
addFlash(ctx, "Account unlinked from "+title.String(provider), "success")
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))
if provider != "github" && provider != "gitea" {
return errorRes(400, "Unsupported provider", nil)

View File

@ -20,9 +20,7 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
userName := ctx.Param("user")
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)
if err != nil {
@ -123,7 +121,8 @@ func allGists(ctx echo.Context) error {
} else {
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 errors.Is(err, gorm.ErrRecordNotFound) {
return notFound("User not found")

View File

@ -47,13 +47,13 @@ var fm = template.FuncMap{
return strings.Split(i, "\n")
},
"isMarkdown": func(i string) bool {
return ".md" == strings.ToLower(filepath.Ext(i))
return strings.ToLower(filepath.Ext(i)) == ".md"
},
"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 {
if ".csv" != strings.ToLower(filepath.Ext(file.Filename)) {
if strings.ToLower(filepath.Ext(file.Filename)) != ".csv" {
return nil
}
@ -239,7 +239,7 @@ func Start() {
func dataInit(next echo.HandlerFunc) echo.HandlerFunc {
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))
setData(ctx, "loadStartTime", time.Now())

View File

@ -18,15 +18,21 @@ import (
"strings"
)
type providerKeyType string
type dataTypeKey string
const providerKey providerKeyType = "provider"
const dataKey dataTypeKey = "data"
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
ctxValue := context.WithValue(ctx.Request().Context(), "data", data)
ctxValue := context.WithValue(ctx.Request().Context(), dataKey, data)
ctx.SetRequest(ctx.Request().WithContext(ctxValue))
}
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]
}
@ -36,7 +42,7 @@ func html(ctx echo.Context, template string) error {
func htmlWithCode(ctx echo.Context, code int, template string) error {
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 {