diff --git a/internal/git/commands.go b/internal/git/commands.go index e26d753..693767c 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -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) diff --git a/internal/web/admin.go b/internal/web/admin.go index 4b7e2b0..6eeca4e 100644 --- a/internal/web/admin.go +++ b/internal/web/admin.go @@ -181,7 +181,6 @@ func adminSyncReposFromDB(ctx echo.Context) error { } } syncReposFromDB = false - return }() return redirect(ctx, "/admin-panel") } diff --git a/internal/web/auth.go b/internal/web/auth.go index d3979c5..95297df 100644 --- a/internal/web/auth.go +++ b/internal/web/auth.go @@ -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) diff --git a/internal/web/gist.go b/internal/web/gist.go index d03bdef..0e0f395 100644 --- a/internal/web/gist.go +++ b/internal/web/gist.go @@ -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") diff --git a/internal/web/run.go b/internal/web/run.go index efaf095..17c4221 100644 --- a/internal/web/run.go +++ b/internal/web/run.go @@ -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()) diff --git a/internal/web/util.go b/internal/web/util.go index 714f959..456d537 100644 --- a/internal/web/util.go +++ b/internal/web/util.go @@ -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 {