From 1d017d056329497f035bf013415561ba8a80f712 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Thu, 1 Apr 2021 20:44:54 +0200 Subject: [PATCH] Tidying and linting --- .../apimodule/account/accountcreate_test.go | 8 +- internal/apimodule/account/accountget.go | 9 +- internal/apimodule/account/accountupdate.go | 24 ++-- .../apimodule/account/accountupdate_test.go | 2 - internal/apimodule/fileserver/fileserver.go | 2 +- internal/config/config.go | 114 +++++++++--------- internal/media/media.go | 4 +- internal/media/media_test.go | 6 +- internal/media/util.go | 4 +- internal/oauth/server.go | 2 +- internal/util/validation_test.go | 2 +- 11 files changed, 85 insertions(+), 92 deletions(-) diff --git a/internal/apimodule/account/accountcreate_test.go b/internal/apimodule/account/accountcreate_test.go index 95f73de..d14ae38 100644 --- a/internal/apimodule/account/accountcreate_test.go +++ b/internal/apimodule/account/accountcreate_test.go @@ -57,8 +57,6 @@ type AccountCreateTestSuite struct { config *config.Config log *logrus.Logger testAccountLocal *model.Account - testAccountRemote *model.Account - testUser *model.User testApplication *model.Application testToken oauth2.TokenInfo mockOauthServer *oauth.MockServer @@ -124,10 +122,10 @@ func (suite *AccountCreateTestSuite) SetupSuite() { MaxImageSize: 2 << 20, } c.StorageConfig = &config.StorageConfig{ - Backend: "local", - BasePath: "/tmp", + Backend: "local", + BasePath: "/tmp", ServeProtocol: "http", - ServeHost: "localhost", + ServeHost: "localhost", ServeBasePath: "/fileserver/media", } suite.config = c diff --git a/internal/apimodule/account/accountget.go b/internal/apimodule/account/accountget.go index 1458c34..5ee9338 100644 --- a/internal/apimodule/account/accountget.go +++ b/internal/apimodule/account/accountget.go @@ -33,19 +33,18 @@ import ( func (m *accountModule) accountGETHandler(c *gin.Context) { targetAcctID := c.Param(idKey) if targetAcctID == "" { - c.JSON(http.StatusBadRequest, gin.H{"error":"no account id specified"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "no account id specified"}) return } targetAccount := &model.Account{} if err := m.db.GetByID(targetAcctID, targetAccount); err != nil { if _, ok := err.(db.ErrNoEntries); ok { - c.JSON(http.StatusNotFound, gin.H{"error":"Record not found"}) - return - } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"}) return } + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return } acctInfo, err := m.db.AccountToMastoPublic(targetAccount) diff --git a/internal/apimodule/account/accountupdate.go b/internal/apimodule/account/accountupdate.go index af312a2..6686d3a 100644 --- a/internal/apimodule/account/accountupdate.go +++ b/internal/apimodule/account/accountupdate.go @@ -137,11 +137,10 @@ func (m *accountModule) accountUpdateCredentialsPATCHHandler(c *gin.Context) { if err := util.ValidateLanguage(*form.Source.Language); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return - } else { - if err := m.db.UpdateOneByID(authed.Account.ID, "language", *form.Source.Language, &model.Account{}); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } + } + if err := m.db.UpdateOneByID(authed.Account.ID, "language", *form.Source.Language, &model.Account{}); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return } } @@ -156,18 +155,17 @@ func (m *accountModule) accountUpdateCredentialsPATCHHandler(c *gin.Context) { if err := util.ValidatePrivacy(*form.Source.Privacy); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return - } else { - if err := m.db.UpdateOneByID(authed.Account.ID, "privacy", *form.Source.Privacy, &model.Account{}); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } + } + if err := m.db.UpdateOneByID(authed.Account.ID, "privacy", *form.Source.Privacy, &model.Account{}); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return } } } - if form.FieldsAttributes != nil { - // TODO: parse fields attributes nicely and update - } + // if form.FieldsAttributes != nil { + // // TODO: parse fields attributes nicely and update + // } // fetch the account with all updated values set updatedAccount := &model.Account{} diff --git a/internal/apimodule/account/accountupdate_test.go b/internal/apimodule/account/accountupdate_test.go index 5886b7b..651b4d2 100644 --- a/internal/apimodule/account/accountupdate_test.go +++ b/internal/apimodule/account/accountupdate_test.go @@ -53,8 +53,6 @@ type AccountUpdateTestSuite struct { config *config.Config log *logrus.Logger testAccountLocal *model.Account - testAccountRemote *model.Account - testUser *model.User testApplication *model.Application testToken oauth2.TokenInfo mockOauthServer *oauth.MockServer diff --git a/internal/apimodule/fileserver/fileserver.go b/internal/apimodule/fileserver/fileserver.go index 4f120fb..bbafff7 100644 --- a/internal/apimodule/fileserver/fileserver.go +++ b/internal/apimodule/fileserver/fileserver.go @@ -25,7 +25,7 @@ type fileServer struct { // New returns a new fileServer module func New(config *config.Config, db db.DB, storage storage.Storage, log *logrus.Logger) apimodule.ClientAPIModule { - storageBase := fmt.Sprintf("%s", config.StorageConfig.BasePath) // TODO: do this properly + storageBase := config.StorageConfig.BasePath // TODO: do this properly return &fileServer{ config: config, diff --git a/internal/config/config.go b/internal/config/config.go index c68e585..811cf16 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -187,64 +187,64 @@ type KeyedFlags interface { // Flags is used for storing the names of the various flags used for // initializing and storing urfavecli flag variables. type Flags struct { - LogLevel string - ApplicationName string - ConfigPath string - Host string - Protocol string + LogLevel string + ApplicationName string + ConfigPath string + Host string + Protocol string - DbType string - DbAddress string - DbPort string - DbUser string - DbPassword string - DbDatabase string + DbType string + DbAddress string + DbPort string + DbUser string + DbPassword string + DbDatabase string - TemplateBaseDir string + TemplateBaseDir string AccountsOpenRegistration string AccountsRequireApproval string - MediaMaxImageSize string - MediaMaxVideoSize string + MediaMaxImageSize string + MediaMaxVideoSize string - StorageBackend string - StorageBasePath string - StorageServeProtocol string - StorageServeHost string - StorageServeBasePath string + StorageBackend string + StorageBasePath string + StorageServeProtocol string + StorageServeHost string + StorageServeBasePath string } // GetFlagNames returns a struct containing the names of the various flags used for // initializing and storing urfavecli flag variables. func GetFlagNames() Flags { return Flags{ - LogLevel: "log-level", - ApplicationName: "application-name", - ConfigPath: "config-path", - Host: "host", - Protocol: "protocol", + LogLevel: "log-level", + ApplicationName: "application-name", + ConfigPath: "config-path", + Host: "host", + Protocol: "protocol", - DbType: "db-type", - DbAddress: "db-address", - DbPort: "db-port", - DbUser: "db-user", - DbPassword: "db-password", - DbDatabase: "db-database", + DbType: "db-type", + DbAddress: "db-address", + DbPort: "db-port", + DbUser: "db-user", + DbPassword: "db-password", + DbDatabase: "db-database", - TemplateBaseDir: "template-basedir", + TemplateBaseDir: "template-basedir", AccountsOpenRegistration: "accounts-open-registration", AccountsRequireApproval: "accounts-require-approval", - MediaMaxImageSize: "media-max-image-size", - MediaMaxVideoSize: "media-max-video-size", + MediaMaxImageSize: "media-max-image-size", + MediaMaxVideoSize: "media-max-video-size", - StorageBackend: "storage-backend", - StorageBasePath: "storage-base-path", - StorageServeProtocol: "storage-serve-protocol", - StorageServeHost: "storage-serve-host", - StorageServeBasePath: "storage-serve-base-path", + StorageBackend: "storage-backend", + StorageBasePath: "storage-base-path", + StorageServeProtocol: "storage-serve-protocol", + StorageServeHost: "storage-serve-host", + StorageServeBasePath: "storage-serve-base-path", } } @@ -252,31 +252,31 @@ func GetFlagNames() Flags { // initializing and storing urfavecli flag variables. func GetEnvNames() Flags { return Flags{ - LogLevel: "GTS_LOG_LEVEL", - ApplicationName: "GTS_APPLICATION_NAME", - ConfigPath: "GTS_CONFIG_PATH", - Host: "GTS_HOST", - Protocol: "GTS_PROTOCOL", + LogLevel: "GTS_LOG_LEVEL", + ApplicationName: "GTS_APPLICATION_NAME", + ConfigPath: "GTS_CONFIG_PATH", + Host: "GTS_HOST", + Protocol: "GTS_PROTOCOL", - DbType: "GTS_DB_TYPE", - DbAddress: "GTS_DB_ADDRESS", - DbPort: "GTS_DB_PORT", - DbUser: "GTS_DB_USER", - DbPassword: "GTS_DB_PASSWORD", - DbDatabase: "GTS_DB_DATABASE", + DbType: "GTS_DB_TYPE", + DbAddress: "GTS_DB_ADDRESS", + DbPort: "GTS_DB_PORT", + DbUser: "GTS_DB_USER", + DbPassword: "GTS_DB_PASSWORD", + DbDatabase: "GTS_DB_DATABASE", - TemplateBaseDir: "GTS_TEMPLATE_BASEDIR", + TemplateBaseDir: "GTS_TEMPLATE_BASEDIR", AccountsOpenRegistration: "GTS_ACCOUNTS_OPEN_REGISTRATION", AccountsRequireApproval: "GTS_ACCOUNTS_REQUIRE_APPROVAL", - MediaMaxImageSize: "GTS_MEDIA_MAX_IMAGE_SIZE", - MediaMaxVideoSize: "GTS_MEDIA_MAX_VIDEO_SIZE", + MediaMaxImageSize: "GTS_MEDIA_MAX_IMAGE_SIZE", + MediaMaxVideoSize: "GTS_MEDIA_MAX_VIDEO_SIZE", - StorageBackend: "GTS_STORAGE_BACKEND", - StorageBasePath: "GTS_STORAGE_BASE_PATH", - StorageServeProtocol: "GTS_STORAGE_SERVE_PROTOCOL", - StorageServeHost: "GTS_STORAGE_SERVE_HOST", - StorageServeBasePath: "GTS_STORAGE_SERVE_BASE_PATH", + StorageBackend: "GTS_STORAGE_BACKEND", + StorageBasePath: "GTS_STORAGE_BASE_PATH", + StorageServeProtocol: "GTS_STORAGE_SERVE_PROTOCOL", + StorageServeHost: "GTS_STORAGE_SERVE_HOST", + StorageServeBasePath: "GTS_STORAGE_SERVE_BASE_PATH", } } diff --git a/internal/media/media.go b/internal/media/media.go index fd517e2..d25fd25 100644 --- a/internal/media/media.go +++ b/internal/media/media.go @@ -120,7 +120,7 @@ func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string return nil, errors.New("header or avatar not selected") } - clean := []byte{} + var clean []byte var err error switch contentType { @@ -152,7 +152,7 @@ func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string extension := strings.Split(contentType, "/")[1] newMediaID := uuid.NewString() - base := fmt.Sprintf("%s://%s%s", mh.config.StorageConfig.ServeProtocol, mh.config.StorageConfig.ServeHost, mh.config.StorageConfig.ServeBasePath, ) + base := fmt.Sprintf("%s://%s%s", mh.config.StorageConfig.ServeProtocol, mh.config.StorageConfig.ServeHost, mh.config.StorageConfig.ServeBasePath) // we store the original... originalPath := fmt.Sprintf("%s/%s/%s/original/%s.%s", base, accountID, headerOrAvi, newMediaID, extension) diff --git a/internal/media/media_test.go b/internal/media/media_test.go index 18855a2..ae5896c 100644 --- a/internal/media/media_test.go +++ b/internal/media/media_test.go @@ -70,10 +70,10 @@ func (suite *MediaTestSuite) SetupSuite() { MaxImageSize: 2 << 20, } c.StorageConfig = &config.StorageConfig{ - Backend: "local", - BasePath: "/tmp", + Backend: "local", + BasePath: "/tmp", ServeProtocol: "http", - ServeHost: "localhost", + ServeHost: "localhost", ServeBasePath: "/fileserver/media", } suite.config = c diff --git a/internal/media/util.go b/internal/media/util.go index 26f270d..9ffb79a 100644 --- a/internal/media/util.go +++ b/internal/media/util.go @@ -73,7 +73,7 @@ func supportedImageType(mimeType string) bool { // purgeExif is a little wrapper for the action of removing exif data from an image. // Only pass pngs or jpegs to this function. func purgeExif(b []byte) ([]byte, error) { - if b == nil || len(b) == 0 { + if len(b) == 0 { return nil, errors.New("passed image was not valid") } @@ -81,7 +81,7 @@ func purgeExif(b []byte) ([]byte, error) { if err != nil { return nil, fmt.Errorf("could not purge exif from image: %s", err) } - if clean == nil || len(clean) == 0 { + if len(clean) == 0 { return nil, errors.New("purged image was not valid") } return clean, nil diff --git a/internal/oauth/server.go b/internal/oauth/server.go index 6c4569d..8bac8fc 100644 --- a/internal/oauth/server.go +++ b/internal/oauth/server.go @@ -249,6 +249,6 @@ func New(database db.DB, log *logrus.Logger) Server { srv.SetClientInfoHandler(server.ClientFormHandler) return &s{ server: srv, - log: log, + log: log, } } diff --git a/internal/util/validation_test.go b/internal/util/validation_test.go index 28d6457..dbac5e2 100644 --- a/internal/util/validation_test.go +++ b/internal/util/validation_test.go @@ -90,7 +90,7 @@ func (suite *ValidationTestSuite) TestValidateUsername() { weirdChars := "thisusername&&&&&&&istooweird!!" leadingSpace := " see_that_leading_space" trailingSpace := "thisusername_ends_with_a_space " - newlines := fmt.Sprintf("this_is\n_almost_ok") + newlines := "this_is\n_almost_ok" goodUsername := "this_is_a_good_username" var err error