From 47672c586d6c07ba5af7fbc08a1780f071f4a3ca Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Sat, 29 Feb 2020 22:41:06 +0500 Subject: [PATCH] Linter configuration fixes and linting issues fixes. Fixes #17. --- .golangci.yml | 5 ++++- domains/pastes/paste_get.go | 2 +- domains/pastes/paste_post.go | 2 +- internal/database/dialects/flatfiles/flatfiles.go | 3 +-- .../database/dialects/mysql/migrations/1_initial.go | 10 ++++++++-- internal/pagination/exported.go | 4 ++-- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 549ef2f..a947ef2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,9 +10,12 @@ linters: - gocritic # Complains about main() lengths, which isn't an issue. - funlen + # Magic numbers everywhere and we can't get rid of them. + - gomnd linters-settings: lll: line-length: 420 + gocognit: + min-complexity: 50 gocyclo: min-complexity: 40 - \ No newline at end of file diff --git a/domains/pastes/paste_get.go b/domains/pastes/paste_get.go index 159436d..53cf989 100644 --- a/domains/pastes/paste_get.go +++ b/domains/pastes/paste_get.go @@ -232,7 +232,7 @@ func pastePasswordedVerifyGet(ec echo.Context) error { func pastePasswordedVerifyPost(ec echo.Context) error { // We should check if database connection available. dbConn := c.Database.GetDatabaseConnection() - // nolint + if c.Config.Database.Type != "flatfiles" && dbConn == nil { return ec.Redirect(http.StatusFound, "/database_not_available") } diff --git a/domains/pastes/paste_post.go b/domains/pastes/paste_post.go index f822b26..3637805 100644 --- a/domains/pastes/paste_post.go +++ b/domains/pastes/paste_post.go @@ -48,9 +48,9 @@ func pastePOSTWebInterface(ec echo.Context) error { return ec.HTML(http.StatusBadRequest, errtpl) } - // nolint if !strings.ContainsAny(params["paste-keep-for"][0], "Mmhd") && params["paste-keep-for"][0] != "forever" { c.Logger.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value") + errtpl := templater.GetErrorTemplate(ec, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).") return ec.HTML(http.StatusBadRequest, errtpl) diff --git a/internal/database/dialects/flatfiles/flatfiles.go b/internal/database/dialects/flatfiles/flatfiles.go index 4d7dfac..a34c742 100644 --- a/internal/database/dialects/flatfiles/flatfiles.go +++ b/internal/database/dialects/flatfiles/flatfiles.go @@ -96,8 +96,7 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) { c.Logger.Debug().Msgf("%+v", publicPastes) // Iteration two - get paginated pastes. - // nolint - var pastesData []structs.Paste + pastesData := make([]structs.Paste, 0) for idx, paste := range publicPastes { if len(pastesData) == c.Config.Pastes.Pagination { diff --git a/internal/database/dialects/mysql/migrations/1_initial.go b/internal/database/dialects/mysql/migrations/1_initial.go index c8dd86a..d6a8a31 100644 --- a/internal/database/dialects/mysql/migrations/1_initial.go +++ b/internal/database/dialects/mysql/migrations/1_initial.go @@ -30,8 +30,14 @@ import ( ) func InitialUp(tx *sql.Tx) error { - // nolint - _, err := tx.Exec("CREATE TABLE `pastes` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Paste ID', `title` text NOT NULL COMMENT 'Paste title', `data` longtext NOT NULL COMMENT 'Paste data', `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Paste creation timestamp', `keep_for` int(4) NOT NULL DEFAULT 1 COMMENT 'Keep for integer. 0 - forever.', `keep_for_unit_type` int(1) NOT NULL DEFAULT 1 COMMENT 'Keep for unit type. 1 - minutes, 2 - hours, 3 - days, 4 - months.', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Pastes';") + _, err := tx.Exec(`CREATE TABLE pastes ( + id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Paste ID', + title text NOT NULL COMMENT 'Paste title', + data longtext NOT NULL COMMENT 'Paste data', + created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Paste creation timestamp', + keep_for int(4) NOT NULL DEFAULT 1 COMMENT 'Keep for integer. 0 - forever.', + keep_for_unit_type int(1) NOT NULL DEFAULT 1 COMMENT 'Keep for unit type. 1 - minutes, 2 - hours, 3 - days, 4 - months.', + PRIMARY KEY (id), UNIQUE KEY id (id)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Pastes';`) if err != nil { return err } diff --git a/internal/pagination/exported.go b/internal/pagination/exported.go index 4014eae..5f74754 100644 --- a/internal/pagination/exported.go +++ b/internal/pagination/exported.go @@ -10,7 +10,6 @@ import ( ) // CreateHTML creates pagination HTML based on passed parameters. -// nolint func CreateHTML(currentPage int, pages int, linksBase string) string { // Load templates. paginationHTMLRaw, err := static.ReadFile("pagination.html") @@ -55,6 +54,7 @@ func CreateHTML(currentPage int, pages int, linksBase string) string { if i == currentPage { paginationItemRaw = string(paginationLinkCurrentRaw) } + paginationItem := strings.Replace(paginationItemRaw, "{pageNum}", strconv.Itoa(i), -1) paginationItem = strings.Replace(paginationItem, "{paginationLink}", linksBase+strconv.Itoa(i), 1) paginationString += paginationItem @@ -77,7 +77,7 @@ func CreateHTML(currentPage int, pages int, linksBase string) string { paginationString += paginationItem } - i += 1 + i++ } pagination := strings.Replace(string(paginationHTMLRaw), "{paginationLinks}", paginationString, 1)