diff --git a/cmd/cli.go b/cmd/cli.go index e432890c09f981468fc43a9ce8c80681c59c8318..0c5f06d1bb89ce92029a2bd2c66f00b721b3f0f1 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -17,7 +17,7 @@ import ( var bmStrict = bluemonday.StrictPolicy() -// createUser is a CLI that creates a new user with the specified username +// createUser is a CLI that creates a new user with the specified username. func createUser(dbConn *sql.DB, username string) { fmt.Println("Creating user", username) @@ -60,7 +60,7 @@ func createUser(dbConn *sql.DB, username string) { os.Exit(0) } -// deleteUser is a CLI that deletes a user with the specified username +// deleteUser is a CLI that deletes a user with the specified username. func deleteUser(dbConn *sql.DB, username string) { fmt.Println("Deleting user", username) err := users.Delete(dbConn, username) @@ -73,7 +73,7 @@ func deleteUser(dbConn *sql.DB, username string) { os.Exit(0) } -// listUsers is a CLI that lists all users in the database +// listUsers is a CLI that lists all users in the database. func listUsers(dbConn *sql.DB) { fmt.Println("Listing all users") diff --git a/db/db.go b/db/db.go index 8e160dfa29c32e4e605d80a8a65008da0ad1cc7d..dbe06f6701b610f340c236f07d893a5f228491b9 100644 --- a/db/db.go +++ b/db/db.go @@ -18,13 +18,13 @@ var schema string var mutex = &sync.Mutex{} -// Open opens a connection to the SQLite database +// Open opens a connection to the SQLite database. func Open(dbPath string) (*sql.DB, error) { return sql.Open("sqlite", "file:"+dbPath+"?_pragma=journal_mode%3DWAL") } // VerifySchema checks whether the schema has been initialised and initialises it -// if not +// if not. func InitialiseDatabase(dbConn *sql.DB) error { var name string err := dbConn.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name='users'").Scan(&name) diff --git a/db/migrations.go b/db/migrations.go index 70d45c1bea284ad5f66472237670fe9126db7471..b0afc25a981a7ed8b7ffd3c86c3b0ca232d569d2 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -48,7 +48,7 @@ var migrations = [...]migration{ }, } -// Migrate runs all pending migrations +// Migrate runs all pending migrations. func Migrate(db *sql.DB) error { version := getSchemaVersion(db) for nextMigration := version + 1; nextMigration < len(migrations); nextMigration++ { @@ -123,7 +123,7 @@ func undoMigration(db *sql.DB, migrationIdx int) (err error) { return updateSchemaVersion(tx, migrationIdx-1) } -// getSchemaVersion returns the schema version from the database +// getSchemaVersion returns the schema version from the database. func getSchemaVersion(db *sql.DB) int { row := db.QueryRowContext(context.Background(), `SELECT version FROM schema_migrations LIMIT 1;`) var version int @@ -133,7 +133,7 @@ func getSchemaVersion(db *sql.DB) int { return version } -// updateSchemaVersion sets the version to the provided int +// updateSchemaVersion sets the version to the provided int. func updateSchemaVersion(tx *sql.Tx, version int) error { if version < 0 { // Do not try to use the schema_migrations table in a schema version where it doesn't exist diff --git a/db/project.go b/db/project.go index 02933a1be19d469950bbcf2268b905c13f90f4ad..6a5ed4a3baccd7c47ebca47a6791527d6eb0cd7d 100644 --- a/db/project.go +++ b/db/project.go @@ -9,7 +9,7 @@ import ( "sync" ) -// DeleteProject deletes a project from the database +// DeleteProject deletes a project from the database. func DeleteProject(db *sql.DB, mu *sync.Mutex, id string) error { mu.Lock() defer mu.Unlock() @@ -21,7 +21,7 @@ func DeleteProject(db *sql.DB, mu *sync.Mutex, id string) error { return err } -// GetProject returns a project from the database +// GetProject returns a project from the database. func GetProject(db *sql.DB, id string) (map[string]string, error) { var name, forge, url, version string err := db.QueryRow("SELECT name, forge, url, version FROM projects WHERE id = ?", id).Scan(&name, &forge, &url, &version) @@ -38,13 +38,13 @@ func GetProject(db *sql.DB, id string) (map[string]string, error) { return project, nil } -// UpsertProject adds or updates a project in the database +// UpsertProject adds or updates a project in the database. func UpsertProject(db *sql.DB, mu *sync.Mutex, id, url, name, forge, running string) error { mu.Lock() defer mu.Unlock() _, err := db.Exec(`INSERT INTO projects (id, url, name, forge, version) VALUES (?, ?, ?, ?, ?) - ON CONFLICT(id) DO + ON CONFLICT(id) DO UPDATE SET name = excluded.name, forge = excluded.forge, @@ -52,7 +52,7 @@ func UpsertProject(db *sql.DB, mu *sync.Mutex, id, url, name, forge, running str return err } -// GetProjects returns a list of all projects in the database +// GetProjects returns a list of all projects in the database. func GetProjects(db *sql.DB) ([]map[string]string, error) { rows, err := db.Query("SELECT id, name, url, forge, version FROM projects") if err != nil { diff --git a/db/release.go b/db/release.go index e6ae4c0f528f1e77fb55ac15f0494c84cb8739e9..0fc909a9a01da4a526b2361853656e2b930778f8 100644 --- a/db/release.go +++ b/db/release.go @@ -10,13 +10,13 @@ import ( ) // UpsertRelease adds or updates a release for a project with a given ID in the -// database +// database. func UpsertRelease(db *sql.DB, mu *sync.Mutex, id, projectID, url, tag, content, date string) error { mu.Lock() defer mu.Unlock() _, err := db.Exec(`INSERT INTO releases (id, project_id, url, tag, content, date) VALUES (?, ?, ?, ?, ?, ?) - ON CONFLICT(id) DO + ON CONFLICT(id) DO UPDATE SET url = excluded.url, content = excluded.content, @@ -26,7 +26,7 @@ func UpsertRelease(db *sql.DB, mu *sync.Mutex, id, projectID, url, tag, content, return err } -// GetReleases returns all releases for a project with a given id from the database +// GetReleases returns all releases for a project with a given id from the database. func GetReleases(db *sql.DB, projectID string) ([]map[string]string, error) { rows, err := db.Query(`SELECT id, url, tag, content, date FROM releases WHERE project_id = ?`, projectID) if err != nil { diff --git a/db/users.go b/db/users.go index 5abaf4a6ae9585c0ad2780316e160f35c51b0c51..48dad0fcbc39291edfec16d9ef1438aedcdb84f0 100644 --- a/db/users.go +++ b/db/users.go @@ -10,7 +10,7 @@ import ( ) // DeleteUser deletes specific user from the database and returns an error if it -// fails +// fails. func DeleteUser(db *sql.DB, user string) error { mutex.Lock() defer mutex.Unlock() @@ -18,7 +18,7 @@ func DeleteUser(db *sql.DB, user string) error { return err } -// CreateUser creates a new user in the database and returns an error if it fails +// CreateUser creates a new user in the database and returns an error if it fails. func CreateUser(db *sql.DB, username, hash, salt string) error { mutex.Lock() defer mutex.Unlock() @@ -27,7 +27,7 @@ func CreateUser(db *sql.DB, username, hash, salt string) error { } // GetUser returns a user's hash and salt from the database as strings and -// returns an error if it fails +// returns an error if it fails. func GetUser(db *sql.DB, username string) (string, string, error) { var hash, salt string err := db.QueryRow("SELECT hash, salt FROM users WHERE username = ?", username).Scan(&hash, &salt) @@ -35,7 +35,7 @@ func GetUser(db *sql.DB, username string) (string, string, error) { } // GetUsers returns a list of all users in the database as a slice of strings -// and returns an error if it fails +// and returns an error if it fails. func GetUsers(db *sql.DB) ([]string, error) { rows, err := db.Query("SELECT username FROM users") if err != nil { @@ -57,7 +57,7 @@ func GetUsers(db *sql.DB) ([]string, error) { } // GetSession accepts a session ID and returns the username associated with it -// and an error +// and an error. func GetSession(db *sql.DB, session string) (string, time.Time, error) { var username string var expiresString string @@ -83,7 +83,7 @@ func InvalidateSession(db *sql.DB, session string, expiry time.Time) error { } // CreateSession creates a new session in the database and returns an error if -// it fails +// it fails. func CreateSession(db *sql.DB, username, token string, expiry time.Time) error { mutex.Lock() defer mutex.Unlock() diff --git a/project/project.go b/project/project.go index 96301092253a1c74a87ad86dd335fc6669cd19ec..8f649e511494e50186d00842e936e7014e90132a 100644 --- a/project/project.go +++ b/project/project.go @@ -40,7 +40,7 @@ type Release struct { Date time.Time } -// GetReleases returns a list of all releases for a project from the database +// GetReleases returns a list of all releases for a project from the database. func GetReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Project, error) { proj.ID = GenProjectID(proj.URL, proj.Name, proj.Forge) @@ -67,7 +67,7 @@ func GetReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Project, error) return proj, nil } -// fetchReleases fetches releases from a project's forge given its URI +// fetchReleases fetches releases from a project's forge given its URI. func fetchReleases(dbConn *sql.DB, mu *sync.Mutex, p Project) (Project, error) { var err error switch p.Forge { @@ -129,7 +129,7 @@ func SortProjects(projects []Project) []Project { return projects } -// upsertReleases updates or inserts a release in the database +// upsertReleases updates or inserts a release in the database. func upsertReleases(dbConn *sql.DB, mu *sync.Mutex, projID string, releases []Release) error { for _, release := range releases { date := release.Date.Format("2006-01-02 15:04:05") @@ -142,13 +142,13 @@ func upsertReleases(dbConn *sql.DB, mu *sync.Mutex, projID string, releases []Re return nil } -// GenReleaseID generates a likely-unique ID from its project's URL, its release's URL, and its tag +// GenReleaseID generates a likely-unique ID from its project's URL, its release's URL, and its tag. func GenReleaseID(projectURL, releaseURL, tag string) string { idByte := sha256.Sum256([]byte(projectURL + releaseURL + tag)) return fmt.Sprintf("%x", idByte) } -// GenProjectID generates a likely-unique ID from a project's URI, name, and forge +// GenProjectID generates a likely-unique ID from a project's URI, name, and forge. func GenProjectID(url, name, forge string) string { idByte := sha256.Sum256([]byte(url + name + forge)) return fmt.Sprintf("%x", idByte) @@ -228,7 +228,7 @@ func RefreshLoop(dbConn *sql.DB, mu *sync.Mutex, interval int, manualRefresh, re } } -// GetProject returns a project from the database +// GetProject returns a project from the database. func GetProject(dbConn *sql.DB, proj Project) (Project, error) { projectDB, err := db.GetProject(dbConn, proj.ID) if err != nil && errors.Is(err, sql.ErrNoRows) { @@ -246,7 +246,7 @@ func GetProject(dbConn *sql.DB, proj Project) (Project, error) { return p, err } -// GetProjectWithReleases returns a single project from the database along with its releases +// GetProjectWithReleases returns a single project from the database along with its releases. func GetProjectWithReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Project, error) { project, err := GetProject(dbConn, proj) if err != nil { @@ -256,7 +256,7 @@ func GetProjectWithReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Proje return GetReleases(dbConn, mu, project) } -// GetProjects returns a list of all projects from the database +// GetProjects returns a list of all projects from the database. func GetProjects(dbConn *sql.DB) ([]Project, error) { projectsDB, err := db.GetProjects(dbConn) if err != nil { @@ -278,7 +278,7 @@ func GetProjects(dbConn *sql.DB) ([]Project, error) { } // GetProjectsWithReleases returns a list of all projects and all their releases -// from the database +// from the database. func GetProjectsWithReleases(dbConn *sql.DB, mu *sync.Mutex) ([]Project, error) { projects, err := GetProjects(dbConn) if err != nil { diff --git a/users/users.go b/users/users.go index ed96cb64a6b02d76ecb5f9c96c1ab1328a231d98..cc2c4b1fcf778ae16719e541a67c35afaeb3310b 100644 --- a/users/users.go +++ b/users/users.go @@ -88,7 +88,7 @@ func InvalidateSession(dbConn *sql.DB, session string) error { } // CreateSession accepts a username, generates a token, stores it in the -// database, and returns it +// database, and returns it. func CreateSession(dbConn *sql.DB, username string) (string, time.Time, error) { token, err := generateSalt() if err != nil { diff --git a/ws/ws.go b/ws/ws.go index 101c3081630483ab9afee5824f0bab6142bd62b1..c024f071ecb184bc31ce44a9d76d5675ea2eae25 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -33,7 +33,6 @@ type Handler struct { //go:embed static var fs embed.FS -// bmUGC = bluemonday.UGCPolicy() var bmStrict = bluemonday.StrictPolicy() func (h Handler) RootHandler(w http.ResponseWriter, r *http.Request) {