Fix lint issues

This commit is contained in:
kolaente 2020-10-11 20:29:17 +02:00
parent 56a6d6b511
commit 10e89ff03a
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
18 changed files with 111 additions and 42 deletions

View File

@ -14,17 +14,18 @@ linters:
- gofmt
- goimports
- golint
- gomnd
- lll
- maligned
- misspell
- nestif
- nlreturn
disable:
- scopelint # Obsolete, using exportloopref instead
presets:
- bugs
- unused
fast: false
linter-settings:
nestif:
min-complexity: 6
issues:
exclude-rules:
# Exclude some linters from running on tests files.
@ -32,3 +33,23 @@ issues:
linters:
- gocyclo
- deadcode
- path: pkg/integrations/*
linters:
- gocyclo
- deadcode
- varcheck
- unparam
- bodyclose
- path: pkg/modules/background/unsplash/unsplash\.go
linters:
- bodyclose
- path: pkg/migration/*
linters:
- exhaustive
- path: pkg/models/task_collection_filter\.go
linters:
- exhaustive
- path: pkg/utils/random_string\.go
text: "G404:" # We don't care about cryptographically secure randomness when we're using that utility function.
linters:
- gosec

View File

@ -53,23 +53,24 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
}
// Use Mysql if set
if config.DatabaseType.GetString() == "mysql" {
switch config.DatabaseType.GetString() {
case "mysql":
engine, err = initMysqlEngine()
if err != nil {
return
}
} else if config.DatabaseType.GetString() == "postgres" {
case "postgres":
engine, err = initPostgresEngine()
if err != nil {
return
}
} else if config.DatabaseType.GetString() == "sqlite" {
case "sqlite":
// Otherwise use sqlite
engine, err = initSqliteEngine()
if err != nil {
return
}
} else {
default:
log.Fatalf("Unknown database type %s", config.DatabaseType.GetString())
}

View File

@ -113,6 +113,8 @@ func validateTaskFieldComparator(comparator taskFilterComparator) error {
taskFilterComparatorLessEquals,
taskFilterComparatorNotEquals:
return nil
case taskFilterComparatorInvalid:
fallthrough
default:
return ErrInvalidTaskFilterComparator{Comparator: comparator}
}

View File

@ -175,6 +175,8 @@ func (rel *TaskRelation) Create(a web.Auth) error {
otherRelation.RelationKind = RelationKindCopiedTo
case RelationKindCopiedTo:
otherRelation.RelationKind = RelationKindCopiedFrom
case RelationKindUnknown:
// Nothing to do
}
// Finally insert everything

View File

@ -259,6 +259,8 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks []
} else {
filters = append(filters, &builder.Lte{f.field: f.value})
}
case taskFilterComparatorInvalid:
// Nothing to do
}
}

View File

@ -17,6 +17,7 @@
package gravatar
import (
"context"
"io/ioutil"
"net/http"
"strconv"
@ -63,10 +64,15 @@ func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error
}
if !exists || needsRefetch {
log.Debugf("Gravatar for user %d with size %d not cached, requesting from gravatar...", user.ID, size)
resp, err := http.Get("https://www.gravatar.com/avatar/" + utils.Md5String(user.Email) + "?s=" + sizeString + "&d=mp")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://www.gravatar.com/avatar/"+utils.Md5String(user.Email)+"?s="+sizeString+"&d=mp", nil)
if err != nil {
return nil, "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
avatarContent, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, "", err

View File

@ -42,15 +42,15 @@ type Provider struct {
var (
avatarBgColors = []*color.RGBA{
{69, 189, 243, 255},
{224, 143, 112, 255},
{77, 182, 172, 255},
{149, 117, 205, 255},
{176, 133, 94, 255},
{240, 98, 146, 255},
{163, 211, 108, 255},
{121, 134, 203, 255},
{241, 185, 29, 255},
{R: 69, G: 189, B: 243, A: 255},
{R: 224, G: 143, B: 112, A: 255},
{R: 77, G: 182, B: 172, A: 255},
{R: 149, G: 117, B: 205, A: 255},
{R: 176, G: 133, B: 94, A: 255},
{R: 240, G: 98, B: 146, A: 255},
{R: 163, G: 211, B: 108, A: 255},
{R: 121, G: 134, B: 203, A: 255},
{R: 241, G: 185, B: 29, A: 255},
}
)

View File

@ -17,6 +17,7 @@
package unsplash
import (
"context"
"net/http"
"strings"
@ -26,10 +27,15 @@ import (
func unsplashImage(url string, c echo.Context) error {
// Replacing and appending the url for security reasons
resp, err := http.Get("https://images.unsplash.com/" + strings.Replace(url, "https://images.unsplash.com/", "", 1))
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://images.unsplash.com/"+strings.Replace(url, "https://images.unsplash.com/", "", 1), nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
return echo.ErrNotFound
}

View File

@ -18,6 +18,7 @@ package unsplash
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/url"
@ -90,7 +91,7 @@ type initialCollection struct {
var emptySearchResult *initialCollection
func doGet(url string, result ...interface{}) (err error) {
req, err := http.NewRequest("GET", unsplashAPIURL+url, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, unsplashAPIURL+url, nil)
if err != nil {
return
}
@ -101,6 +102,7 @@ func doGet(url string, result ...interface{}) (err error) {
if err != nil {
return
}
defer resp.Body.Close()
if len(result) > 0 {
return json.NewDecoder(resp.Body).Decode(result[0])
@ -251,10 +253,14 @@ func (p *Provider) Set(image *background.Image, list *models.List, auth web.Auth
// Download the photo from unsplash
// The parameters crop the image to a max width of 2560 and a max height of 2048 to save bandwidth and storage.
resp, err := http.Get(photo.Urls.Raw + "&w=2560&h=2048&q=90")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, photo.Urls.Raw+"&w=2560&h=2048&q=90", nil)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
@ -328,9 +334,13 @@ func Pingback(f *files.File) {
}
func pingbackByPhotoID(photoID string) {
if _, err := http.Get("https://views.unsplash.com/v?app_id=" + config.BackgroundsUnsplashApplicationID.GetString() + "&photo_id=" + photoID); err != nil {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://views.unsplash.com/v?app_id="+config.BackgroundsUnsplashApplicationID.GetString()+"&photo_id="+photoID, nil)
if err != nil {
log.Errorf("Unsplash Pingback Failed: %s", err.Error())
}
_, err = http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Unsplash Pingback Failed: %s", err.Error())
}
log.Debugf("Pinged unsplash for photo %s", photoID)
}

View File

@ -18,6 +18,7 @@ package todoist
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@ -218,7 +219,7 @@ func (m *Migration) AuthURL() string {
}
func doPost(url string, form url.Values) (resp *http.Response, err error) {
req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, strings.NewReader(form.Encode()))
if err != nil {
return
}
@ -343,7 +344,12 @@ func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.Namespa
// Only add the attachment if there's something to download
if len(n.FileAttachment.FileURL) > 0 {
// Download the attachment and put it in the file
resp, err := http.Get(n.FileAttachment.FileURL)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, n.FileAttachment.FileURL, nil)
if err != nil {
return nil, err
}
hc := http.Client{}
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
@ -418,6 +424,7 @@ func getAccessTokenFromAuthToken(authToken string) (accessToken string, err erro
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
buf := &bytes.Buffer{}
@ -469,6 +476,7 @@ func (m *Migration) Migrate(u *user.User) (err error) {
if err != nil {
return
}
defer resp.Body.Close()
syncResponse := &sync{}
err = json.NewDecoder(resp.Body).Decode(syncResponse)

View File

@ -18,6 +18,7 @@ package wunderlist
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@ -52,17 +53,17 @@ type wunderlistAuthToken struct {
}
type task struct {
ID int `json:"id"`
AssigneeID int `json:"assignee_id"`
CreatedAt time.Time `json:"created_at"`
CreatedByID int `json:"created_by_id"`
Completed bool `json:"completed"`
CompletedAt time.Time `json:"completed_at"`
DueDate string `json:"due_date"`
ID int `json:"id"`
ListID int `json:"list_id"`
Revision int `json:"revision"`
Starred bool `json:"starred"`
Title string `json:"title"`
Completed bool `json:"completed"`
CompletedAt time.Time `json:"completed_at"`
}
type list struct {
@ -182,7 +183,11 @@ func convertListForFolder(listID int, list *list, content *wunderlistContents) (
for _, f := range content.files {
if f.TaskID == t.ID {
// Download the attachment and put it in the file
resp, err := http.Get(f.URL)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, f.URL, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
@ -297,7 +302,7 @@ func convertWunderlistToVikunja(content *wunderlistContents) (fullVikunjaHierach
}
func makeAuthGetRequest(token *wunderlistAuthToken, urlPart string, v interface{}, urlParams url.Values) error {
req, err := http.NewRequest(http.MethodGet, "https://a.wunderlist.com/api/v1/"+urlPart, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://a.wunderlist.com/api/v1/"+urlPart, nil)
if err != nil {
return err
}
@ -367,10 +372,16 @@ func (w *Migration) Migrate(user *user.User) (err error) {
if err != nil {
return
}
resp, err := http.Post("https://www.wunderlist.com/oauth/access_token", "application/json", bytes.NewBuffer(jsonAuth))
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "https://www.wunderlist.com/oauth/access_token", bytes.NewBuffer(jsonAuth))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
authToken := &wunderlistAuthToken{}
err = json.NewDecoder(resp.Body).Decode(authToken)

View File

@ -46,7 +46,7 @@ type Token struct {
func Login(c echo.Context) error {
u := user2.Login{}
if err := c.Bind(&u); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"Please provide a username and password."})
return c.JSON(http.StatusBadRequest, models.Message{Message: "Please provide a username and password."})
}
// Check user

View File

@ -31,5 +31,5 @@ func CheckToken(c echo.Context) error {
fmt.Println(user.Valid)
return c.JSON(418, models.Message{"🍵"})
return c.JSON(418, models.Message{Message: "🍵"})
}

View File

@ -48,5 +48,5 @@ func UserConfirmEmail(c echo.Context) error {
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, models.Message{"The email was confirmed successfully."})
return c.JSON(http.StatusOK, models.Message{Message: "The email was confirmed successfully."})
}

View File

@ -48,7 +48,7 @@ func UserResetPassword(c echo.Context) error {
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, models.Message{"The password was updated successfully."})
return c.JSON(http.StatusOK, models.Message{Message: "The password was updated successfully."})
}
// UserRequestResetPasswordToken is the handler to change a users password
@ -78,5 +78,5 @@ func UserRequestResetPasswordToken(c echo.Context) error {
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, models.Message{"Token was sent."})
return c.JSON(http.StatusOK, models.Message{Message: "Token was sent."})
}

View File

@ -44,10 +44,10 @@ func RegisterUser(c echo.Context) error {
// Check for Request Content
var datUser *user.APIUserPassword
if err := c.Bind(&datUser); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No or invalid user model provided."})
return c.JSON(http.StatusBadRequest, models.Message{Message: "No or invalid user model provided."})
}
if datUser == nil {
return c.JSON(http.StatusBadRequest, models.Message{"No or invalid user model provided."})
return c.JSON(http.StatusBadRequest, models.Message{Message: "No or invalid user model provided."})
}
// Insert the user

View File

@ -71,5 +71,5 @@ func UserChangePassword(c echo.Context) error {
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, models.Message{"The password was updated successfully."})
return c.JSON(http.StatusOK, models.Message{Message: "The password was updated successfully."})
}

View File

@ -120,8 +120,8 @@ func (vcls *VikunjaCaldavListStorage) GetResourcesByList(rpaths []string) ([]dat
var uids []string
for _, path := range rpaths {
parts := strings.Split(path, "/")
uid := []rune(parts[4]) // The 4th part is the id with ".ics" suffix
endlen := len(uid) - 4 // ".ics" are 4 bytes
uid := []rune(parts[4]) // The 4th part is the id with ".ics" suffix
endlen := len(uid) - len(".ics") // ".ics" are 4 bytes
uids = append(uids, string(uid[:endlen]))
}