More Greater Linting.

This commit is contained in:
2021-06-15 00:11:58 +05:00
parent 6ea6e2e144
commit 968d945205
28 changed files with 143 additions and 15 deletions

View File

@@ -100,6 +100,7 @@ func (c *Context) LoadConfiguration() {
c.Logger.Debug().Str("path", configPath).Msg("Configuration file path")
// nolint:exhaustivestruct
c.Config = &config.Struct{}
// Read configuration file.

View File

@@ -31,5 +31,6 @@ const (
// New creates new context.
func New() *Context {
// nolint:exhaustivestruct
return &Context{}
}

View File

@@ -24,6 +24,7 @@ func (c *Context) getMemoryUsage(e *zerolog.Event, level zerolog.Level, message
// Initializes logger.
func (c *Context) initializeLogger() {
// Устанавливаем форматирование логгера.
// nolint:exhaustivestruct
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
var v string

View File

@@ -28,6 +28,7 @@ import (
"database/sql"
"time"
// MySQL driver.
_ "github.com/go-sql-driver/mysql"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
@@ -78,6 +79,7 @@ func (db *Database) cleanup() {
}
func (db *Database) DeletePaste(pasteID int) error {
// nolint:wrapcheck
return db.db.DeletePaste(pasteID)
}
@@ -90,10 +92,12 @@ func (db *Database) GetDatabaseConnection() *sql.DB {
}
func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
// nolint:wrapcheck
return db.db.GetPaste(pasteID)
}
func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
// nolint:wrapcheck
return db.db.GetPagedPastes(page)
}
@@ -124,6 +128,7 @@ func (db *Database) RegisterDialect(di dialectinterface.Interface) {
}
func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
// nolint:wrapcheck
return db.db.SavePaste(p)
}

View File

@@ -38,6 +38,7 @@ var (
func New(cc *context.Context) {
c = cc
// nolint:exhaustivestruct
f = &FlatFiles{}
c.Database.RegisterDialect(dialectinterface.Interface(Handler{}))

View File

@@ -51,6 +51,7 @@ func (ff *FlatFiles) DeletePaste(pasteID int) error {
if err != nil {
c.Logger.Error().Err(err).Msg("Failed to delete paste!")
// nolint:wrapcheck
return err
}
@@ -88,18 +89,21 @@ func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
if err != nil {
c.Logger.Debug().Err(err).Msg("Failed to read paste from storage")
// nolint:wrapcheck
return nil, err
}
c.Logger.Debug().Int("paste bytes", len(pasteInBytes)).Msg("Loaded paste")
ff.writeMutex.Unlock()
// nolint:exhaustivestruct
paste := &structs.Paste{}
err1 := json.Unmarshal(pasteInBytes, paste)
if err1 != nil {
c.Logger.Error().Err(err1).Msgf("Failed to parse paste")
// nolint:wrapcheck
return nil, err1
}
@@ -145,6 +149,7 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
c.Logger.Debug().Int("ID", paste.ID).Int("index", idx).Msg("Getting paste data")
// Get paste data.
// nolint:exhaustivestruct
pasteData := &structs.Paste{}
pasteRawData, err := ioutil.ReadFile(filepath.Join(ff.path, "pastes", strconv.Itoa(paste.ID)+".json"))
@@ -258,17 +263,21 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
if err != nil {
ff.writeMutex.Unlock()
// nolint:wrapcheck
return 0, err
}
// nolint:gosec
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0644)
if err != nil {
ff.writeMutex.Unlock()
// nolint:wrapcheck
return 0, err
}
// Add it to cache.
// nolint:exhaustivestruct
indexData := Index{}
indexData.ID = pasteID
indexData.Private = p.Private
@@ -288,6 +297,7 @@ func (ff *FlatFiles) Shutdown() {
return
}
// nolint:gosec
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to write index data to file. Pretty sure that you've lost your pastes.")

View File

@@ -36,6 +36,7 @@ var (
func New(cc *context.Context) {
c = cc
// nolint:exhaustivestruct
d = &Database{}
c.Database.RegisterDialect(dialectinterface.Interface(Handler{}))

View File

@@ -38,6 +38,7 @@ func InitialUp(tx *sql.Tx) error {
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 {
// nolint:wrapcheck
return err
}

View File

@@ -31,6 +31,7 @@ import (
func PasteLangUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` ADD `language` VARCHAR(191) NOT NULL DEFAULT 'text' COMMENT 'Paste language'")
if err != nil {
// nolint:wrapcheck
return err
}
@@ -40,6 +41,7 @@ func PasteLangUp(tx *sql.Tx) error {
func PasteLangDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `language`")
if err != nil {
// nolint:wrapcheck
return err
}

View File

@@ -31,6 +31,7 @@ import (
func PrivatePastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` ADD `private` BOOL NOT NULL DEFAULT false COMMENT 'Private paste? If true - additional URL parameter (UNIX TIMESTAMP) of paste will be required to access.'")
if err != nil {
// nolint:wrapcheck
return err
}
@@ -40,6 +41,7 @@ func PrivatePastesUp(tx *sql.Tx) error {
func PrivatePastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `private`")
if err != nil {
// nolint:wrapcheck
return err
}

View File

@@ -31,11 +31,13 @@ import (
func PasswordedPastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` ADD `password` varchar(64) NOT NULL DEFAULT '' COMMENT 'Password for paste (scrypted and sha256ed).'")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE `pastes` ADD `password_salt` varchar(64) NOT NULL DEFAULT '' COMMENT 'Password salt (sha256ed).'")
if err1 != nil {
// nolint:wrapcheck
return err1
}
@@ -45,11 +47,13 @@ func PasswordedPastesUp(tx *sql.Tx) error {
func PasswordedPastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `password`")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `password_salt`")
if err1 != nil {
// nolint:wrapcheck
return err1
}

View File

@@ -28,6 +28,7 @@ import (
"database/sql"
"fmt"
// MySQL driver.
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/mysql/migrations"
@@ -58,6 +59,7 @@ func (db *Database) DeletePaste(pasteID int) error {
_, err := db.db.Exec(db.db.Rebind("DELETE FROM pastes WHERE id=?"), pasteID)
if err != nil {
// nolint:wrapcheck
return err
}
@@ -78,10 +80,12 @@ func (db *Database) GetDatabaseConnection() *sql.DB {
func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
db.check()
// nolint:exhaustivestruct
p := &structs.Paste{}
err := db.db.Get(p, db.db.Rebind("SELECT * FROM `pastes` WHERE id=?"), pasteID)
if err != nil {
// nolint:wrapcheck
return nil, err
}
@@ -104,6 +108,7 @@ func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
err := db.db.Select(&pastesRaw, db.db.Rebind("SELECT * FROM `pastes` WHERE private != true ORDER BY id DESC LIMIT ? OFFSET ?"), c.Config.Pastes.Pagination, startPagination)
if err != nil {
// nolint:wrapcheck
return nil, err
}
@@ -186,11 +191,13 @@ func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
result, err := db.db.NamedExec("INSERT INTO `pastes` (title, data, created_at, keep_for, keep_for_unit_type, language, private, password, password_salt) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private, :password, :password_salt)", p)
if err != nil {
// nolint:wrapcheck
return 0, err
}
ID, err1 := result.LastInsertId()
if err1 != nil {
// nolint:wrapcheck
return 0, err
}

View File

@@ -36,6 +36,7 @@ var (
func New(cc *context.Context) {
c = cc
// nolint:exhaustivestruct
d = &Database{}
c.Database.RegisterDialect(dialectinterface.Interface(Handler{}))

View File

@@ -48,6 +48,7 @@ func InitialUp(tx *sql.Tx) error {
COMMENT ON COLUMN pastes.keep_for_unit_type IS 'Keep for unit type. 0 - forever, 1 - minutes, 2 - hours, 3 - days, 4 - months.';
`)
if err != nil {
// nolint:wrapcheck
return err
}

View File

@@ -31,6 +31,7 @@ import (
func PasteLangUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes ADD COLUMN language VARCHAR(191) NOT NULL DEFAULT 'text'; COMMENT ON COLUMN pastes.language IS 'Paste language';")
if err != nil {
// nolint:wrapcheck
return err
}
@@ -40,6 +41,7 @@ func PasteLangUp(tx *sql.Tx) error {
func PasteLangDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes DROP COLUMN language")
if err != nil {
// nolint:wrapcheck
return err
}

View File

@@ -31,6 +31,7 @@ import (
func PrivatePastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes ADD COLUMN private BOOLEAN NOT NULL DEFAULT false; COMMENT ON COLUMN pastes.private IS 'Private paste? If true - additional URL parameter (UNIX TIMESTAMP) of paste will be required to access.';")
if err != nil {
// nolint:wrapcheck
return err
}
@@ -40,6 +41,7 @@ func PrivatePastesUp(tx *sql.Tx) error {
func PrivatePastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes DROP COLUMN private")
if err != nil {
// nolint:wrapcheck
return err
}

View File

@@ -31,11 +31,13 @@ import (
func PasswordedPastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes ADD COLUMN password VARCHAR(64) NOT NULL DEFAULT ''; COMMENT ON COLUMN pastes.password IS 'Password for paste (scrypted and sha256ed).';")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE pastes ADD COLUMN password_salt VARCHAR(64) NOT NULL DEFAULT ''; COMMENT ON COLUMN pastes.password_salt IS 'Password salt (sha256ed).';")
if err1 != nil {
// nolint:wrapcheck
return err1
}
@@ -45,11 +47,13 @@ func PasswordedPastesUp(tx *sql.Tx) error {
func PasswordedPastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes DROP COLUMN password")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE pastes DROP COLUMN password_salt")
if err1 != nil {
// nolint:wrapcheck
return err1
}

View File

@@ -24,13 +24,16 @@
package postgresql
// nolint:gci
import (
"database/sql"
"fmt"
"time"
"github.com/jmoiron/sqlx"
// PostgreSQL driver.
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/postgresql/migrations"
"go.dev.pztrn.name/fastpastebin/internal/structs"
)
@@ -59,6 +62,7 @@ func (db *Database) DeletePaste(pasteID int) error {
_, err := db.db.Exec(db.db.Rebind("DELETE FROM pastes WHERE id=?"), pasteID)
if err != nil {
// nolint:wrapcheck
return err
}
@@ -79,10 +83,12 @@ func (db *Database) GetDatabaseConnection() *sql.DB {
func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
db.check()
// nolint:exhaustivestruct
p := &structs.Paste{}
err := db.db.Get(p, db.db.Rebind("SELECT * FROM pastes WHERE id=$1"), pasteID)
if err != nil {
// nolint:wrapcheck
return nil, err
}
@@ -112,6 +118,7 @@ func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
err := db.db.Select(&pastesRaw, db.db.Rebind("SELECT * FROM pastes WHERE private != true ORDER BY id DESC LIMIT $1 OFFSET $2"), c.Config.Pastes.Pagination, startPagination)
if err != nil {
// nolint:wrapcheck
return nil, err
}
@@ -195,6 +202,7 @@ func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
stmt, err := db.db.PrepareNamed("INSERT INTO pastes (title, data, created_at, keep_for, keep_for_unit_type, language, private, password, password_salt) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private, :password, :password_salt) RETURNING id")
if err != nil {
// nolint:wrapcheck
return 0, err
}
@@ -202,6 +210,7 @@ func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
err = stmt.Get(&id, p)
if err != nil {
// nolint:wrapcheck
return 0, err
}

View File

@@ -37,6 +37,7 @@ var (
// New initializes database structure.
func New(cc *context.Context) {
c = cc
// nolint:exhaustivestruct
d = &Database{}
c.RegisterDatabaseInterface(databaseinterface.Interface(Handler{}))

View File

@@ -34,15 +34,22 @@ import (
)
const (
PasteKeepForever = 0
// PasteKeepForever indicates that paste should be kept forever.
PasteKeepForever = 0
// PasteKeepForMinutes indicates that saved timeout is in minutes.
PasteKeepForMinutes = 1
PasteKeepForHours = 2
PasteKeepForDays = 3
PasteKeepForMonths = 4
// PasteKeepForHours indicates that saved timeout is in hours.
PasteKeepForHours = 2
// PasteKeepForDays indicates that saved timeout is in days.
PasteKeepForDays = 3
// PasteKeepForMonths indicates that saved timeout is in months.
PasteKeepForMonths = 4
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
// PasteKeepsCorrelation is a correlation map between database representation
// and passed data representation.
var PasteKeepsCorrelation = map[string]int{
"M": PasteKeepForMinutes,
"h": PasteKeepForHours,
@@ -68,6 +75,8 @@ type Paste struct {
// CreatePassword creates password for current paste.
func (p *Paste) CreatePassword(password string) error {
// Create salt - random string.
// Yes, it is insecure. Should be refactored!
// nolint:gosec
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
saltBytes := make([]byte, 64)
@@ -81,6 +90,7 @@ func (p *Paste) CreatePassword(password string) error {
// Create crypted password and hash it.
passwordCrypted, err := scrypt.Key([]byte(password), []byte(p.PasswordSalt), 131072, 8, 1, 64)
if err != nil {
// nolint:wrapcheck
return err
}