Expanded linting + fix warnings (#1396)

* Expand the linters and types of warnings to improve consistency and safety

* Fail lint workflow if there are errors

* golint has been replaced by revive

* Hand-pick some of the default exclude list

* Ignore error when trying to delete preview gif

* Ignore linter warning opening playlist path

* Rename user field Id -> ID

* A bunch of renames to address linter warnings

* Rename ChatClient -> Client per linter suggestion best practice

* Rename ChatServer -> Server per linter suggestion best practice

* More linter warning fixes

* Add missing comments to all exported functions and properties
This commit is contained in:
Gabe Kangas
2021-09-12 00:18:15 -07:00
committed by GitHub
parent 70e9f4945f
commit c6c6f0233d
57 changed files with 331 additions and 186 deletions

View File

@@ -8,6 +8,7 @@ import (
const tokenLength = 32
// GenerateAccessToken will generate and return an access token.
func GenerateAccessToken() (string, error) {
return generateRandomString(tokenLength)
}

View File

@@ -12,15 +12,15 @@ import (
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/schollz/sqlite3dump"
log "github.com/sirupsen/logrus"
)
// Restore will attempt to restore the database using a specified backup file.
func Restore(backupFile string, databaseFile string) error {
log.Printf("Restoring database backup %s to %s", backupFile, databaseFile)
data, err := ioutil.ReadFile(backupFile)
data, err := ioutil.ReadFile(backupFile) // nolint
if err != nil {
return fmt.Errorf("Unable to read backup file %s", err)
}
@@ -38,7 +38,7 @@ func Restore(backupFile string, databaseFile string) error {
defer gz.Close()
rawSql := b.String()
rawSQL := b.String()
if _, err := os.Create(databaseFile); err != nil {
return errors.New("Unable to write restored database")
@@ -49,13 +49,14 @@ func Restore(backupFile string, databaseFile string) error {
if err != nil {
return err
}
if _, err := db.Exec(rawSql); err != nil {
if _, err := db.Exec(rawSQL); err != nil {
return err
}
return nil
}
// Backup will backup the provided instance of the database to the specified file.
func Backup(db *sql.DB, backupFile string) {
log.Traceln("Backing up database to", backupFile)
@@ -76,10 +77,10 @@ func Backup(db *sql.DB, backupFile string) {
handleError(err)
return
}
out.Flush()
_ = out.Flush()
// Create a new backup file
f, err := os.OpenFile(backupFile, os.O_WRONLY|os.O_CREATE, 0600)
f, err := os.OpenFile(backupFile, os.O_WRONLY|os.O_CREATE, 0600) // nolint
if err != nil {
handleError(err)
return

View File

@@ -6,6 +6,7 @@ import (
"time"
)
// NullTime is a custom nullable time for representing datetime.
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
@@ -25,6 +26,7 @@ func (nt NullTime) Value() (driver.Value, error) {
return nt.Time, nil
}
// MarshalJSON implements the JSON marshal function.
func (nt NullTime) MarshalJSON() ([]byte, error) {
if !nt.Valid {
return []byte("null"), nil
@@ -33,6 +35,7 @@ func (nt NullTime) MarshalJSON() ([]byte, error) {
return []byte(val), nil
}
// UnmarshalJSON implements the JSON unmarshal function.
func (nt NullTime) UnmarshalJSON(data []byte) error {
dateString := string(data)
if dateString == "null" {
@@ -45,6 +48,6 @@ func (nt NullTime) UnmarshalJSON(data []byte) error {
return err
}
nt.Time = parsedDateTime
nt.Time = parsedDateTime // nolint
return nil
}

View File

@@ -626,11 +626,12 @@ var (
}
)
// GeneratePhrase will generate and return a random string consisting of our word list.
func GeneratePhrase() string {
r := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint
left_index := int(r.Float32() * float32(len(left)))
right_index := int(r.Float32() * float32(len(right)))
leftIndex := int(r.Float32() * float32(len(left)))
rightIndex := int(r.Float32() * float32(len(right)))
return fmt.Sprintf("%s-%s", left[left_index], right[right_index])
return fmt.Sprintf("%s-%s", left[leftIndex], right[rightIndex])
}

View File

@@ -42,6 +42,7 @@ func GetRelativePathFromAbsolutePath(path string) string {
return filepath.Join(variant, file)
}
// GetIndexFromFilePath is a utility that will return the index/key/variant name in a full path.
func GetIndexFromFilePath(path string) string {
pathComponents := strings.Split(path, "/")
variant := pathComponents[len(pathComponents)-2]
@@ -51,7 +52,7 @@ func GetIndexFromFilePath(path string) string {
// Copy copies the file to destination.
func Copy(source, destination string) error {
input, err := ioutil.ReadFile(source)
input, err := ioutil.ReadFile(source) // nolint
if err != nil {
return err
}
@@ -108,6 +109,7 @@ func IsUserAgentAPlayer(userAgent string) bool {
return false
}
// RenderSimpleMarkdown will return HTML without sanitization or specific formatting rules.
func RenderSimpleMarkdown(raw string) string {
markdown := goldmark.New(
goldmark.WithRendererOptions(
@@ -135,6 +137,7 @@ func RenderSimpleMarkdown(raw string) string {
return buf.String()
}
// RenderPageContentMarkdown will return HTML specifically handled for the user-specified page content.
func RenderPageContentMarkdown(raw string) string {
markdown := goldmark.New(
goldmark.WithRendererOptions(
@@ -189,7 +192,8 @@ func GetCacheDurationSecondsForPath(filePath string) int {
return 60 * 10
}
func IsValidUrl(urlToTest string) bool {
// IsValidURL will return if a URL string is a valid URL or not.
func IsValidURL(urlToTest string) bool {
if _, err := url.ParseRequestURI(urlToTest); err != nil {
return false
}
@@ -207,9 +211,8 @@ func ValidatedFfmpegPath(ffmpegPath string) string {
if ffmpegPath != "" {
if err := VerifyFFMpegPath(ffmpegPath); err == nil {
return ffmpegPath
} else {
log.Warnln(ffmpegPath, "is an invalid path to ffmpeg will try to use a copy in your path, if possible")
}
log.Warnln(ffmpegPath, "is an invalid path to ffmpeg will try to use a copy in your path, if possible")
}
// First look to see if ffmpeg is in the current working directory
@@ -255,17 +258,18 @@ func VerifyFFMpegPath(path string) error {
return nil
}
// Removes the directory and makes it again. Throws fatal error on failure.
// CleanupDirectory removes the directory and makes it fresh again. Throws fatal error on failure.
func CleanupDirectory(path string) {
log.Traceln("Cleaning", path)
if err := os.RemoveAll(path); err != nil {
log.Fatalln("Unable to remove directory. Please check the ownership and permissions", err)
}
if err := os.MkdirAll(path, 0777); err != nil {
if err := os.MkdirAll(path, 0750); err != nil {
log.Fatalln("Unable to create directory. Please check the ownership and permissions", err)
}
}
// FindInSlice will return the index if a string is located in a slice of strings.
func FindInSlice(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {