Documentation ¶
Overview ¶
Package db stores and manipulates dotfiles via a sqlite3 database.
Index ¶
- Constants
- Variables
- func CheckPassword(e Executor, username, password string) error
- func CheckPasswordResetToken(e Executor, token string) (string, error)
- func ClearCommits(tx *sql.Tx, username, alias string) error
- func Close()
- func DeleteFile(tx *sql.Tx, username, alias string) error
- func DeleteTempFile(e Executor, username string) error
- func DeleteUser(username, password string) error
- func FileData(e Executor, username, alias string) (*dotfile.TrackingData, error)
- func ForkFile(username, alias, hash string, newUserID int64) error
- func InitOrCommit(userID int64, alias, message string) error
- func Logout(e Executor, session string) error
- func NotFound(err error) bool
- func ResetPassword(e Executor, token, newPassword string) (string, error)
- func Rollback(tx *sql.Tx, err error) error
- func RotateCLIToken(e Executor, userID int64, currentToken string) error
- func SeedReservedUsernames(e Executor, usernames []interface{}) error
- func SetFileToHash(e Executor, username, alias, hash string) error
- func SetPasswordResetToken(e Executor, email string) (string, error)
- func Start(dbPath string) (err error)
- func UpdateEmail(e Executor, userID int64, email string) error
- func UpdatePassword(e Executor, username string, currentPass, newPassword string) error
- func UpdateTheme(e Executor, userID int64, theme UserTheme) error
- func UpdateTimezone(e Executor, userID int64, timezone string) error
- func UserLoginAPI(e Executor, username, cliToken string) (int64, error)
- func ValidateFileNotExists(e Executor, userID int64, alias, path string) error
- func ValidateUserExists(e Executor, username string) error
- type CommitRecord
- type CommitSummary
- type CommitView
- type Executor
- type FileContent
- type FileRecord
- type FileSearchResult
- type FileSummary
- type FileTransaction
- func (ft *FileTransaction) DirtyContent() ([]byte, error)
- func (ft *FileTransaction) HasCommit(hash string) (exists bool, err error)
- func (ft *FileTransaction) InsertCommit(buff *bytes.Buffer, c *dotfile.Commit) (int64, error)
- func (ft *FileTransaction) Revision(hash string) ([]byte, error)
- func (ft *FileTransaction) SaveCommit(buff *bytes.Buffer, c *dotfile.Commit) error
- func (ft *FileTransaction) SaveFile(userID int64, alias, path string) error
- func (ft *FileTransaction) SetRevision(hash string) error
- type FileView
- type HTMLTable
- type PageControls
- type ReservedUsernameRecord
- type SessionRecord
- type TempFileRecord
- type UserRecord
- type UserSession
- type UserTheme
Constants ¶
const ( // DefaultLimit is the default limit for paginated queries. DefaultLimit = 100 // MaxLimit is the max limit for paginated queries. MaxLimit = 500 )
Variables ¶
var Connection *sql.DB
Connection is a global database connection. Call Start() to initialize.
Functions ¶
func CheckPassword ¶
CheckPassword checks username and password combination. Tells the user when the password does not match.
func CheckPasswordResetToken ¶
CheckPasswordResetToken checks if the password reset token exists. Returns username for the token on success.
func ClearCommits ¶
ClearCommits deletes all commits for a file except the current.
func DeleteFile ¶
DeleteFile deletes a users file.
func DeleteTempFile ¶
DeleteTempFile deletes a users temp file.
func DeleteUser ¶
DeleteUser deletes a user and their data.
func FileData ¶
func FileData(e Executor, username, alias string) (*dotfile.TrackingData, error)
FileData returns the files dotfile data structure.
func InitOrCommit ¶
InitOrCommit uses the content in a temp file to initialize a file or create a new commit.
func ResetPassword ¶
ResetPassword hashes and sets a new password to the user with the password reset token.
func RotateCLIToken ¶
RotateCLIToken creates a new CLI token for the user.
func SeedReservedUsernames ¶
SeedReservedUsernames sets usernames which are not allowed to be used. This should be called when the service is started.
func SetFileToHash ¶
SetFileToHash sets file to the commit at hash.
func SetPasswordResetToken ¶
SetPasswordResetToken creates and saves a reset token for the user. Returns the newly created token.
func Start ¶
Start opens a connection a sqlite3 database. Creates a new sqlite database with all required tables when not found. Create an in memory database when dbPath is empty.
func UpdateEmail ¶
UpdateEmail updates a users email and sets email_confirmed to false.
func UpdatePassword ¶
UpdatePassword updates a users password. currentPass must match the current hash.
func UpdateTheme ¶
UpdateTheme updates a users theme setting.
func UpdateTimezone ¶
UpdateTimezone checks if the timezone is able to be loaded and updates the user record.
func UserLoginAPI ¶
UserLoginAPI checks a username and CLI token combination.
func ValidateFileNotExists ¶
ValidateFileNotExists validates that no other file for user exists with alias or path.
func ValidateUserExists ¶
ValidateUserExists throws a sql.ErrNoRows when the username does not exist.
Types ¶
type CommitRecord ¶
type CommitRecord struct { ID int64 ForkedFrom *int64 // A commit id. FileID int64 `validate:"required"` Hash string `validate:"required"` // Hash of the uncompressed file. Message string Revision []byte `validate:"required"` // Compressed version of file at hash. Timestamp int64 `validate:"required"` // Unix time to stay synced with local commits. }
CommitRecord models the commits table.
type CommitSummary ¶
type CommitSummary struct { Hash string Message string Current bool Timestamp int64 DateString string ForkedFromUsername *string // The owner of the file that this commit was forked from. }
CommitSummary summarizes a commit.
func CommitList ¶
func CommitList(e Executor, username, alias string, timezone *string) ([]CommitSummary, error)
CommitList gets a summary of all commits for a file.
type CommitView ¶
type CommitView struct { CommitSummary Path string Content []byte }
CommitView is used for an individual commit view.
func UncompressCommit ¶
func UncompressCommit(e Executor, username, alias, hash string, timezone *string) (*CommitView, error)
UncompressCommit gets a commit and uncompresses its contents.
type Executor ¶
type Executor interface { Exec(string, ...interface{}) (sql.Result, error) Query(string, ...interface{}) (*sql.Rows, error) QueryRow(string, ...interface{}) *sql.Row }
Executor is an interface for executing SQL.
type FileContent ¶
FileContent implements file.Getter. It pulls content from temp_files and commits.
func (*FileContent) DirtyContent ¶
func (fc *FileContent) DirtyContent() ([]byte, error)
DirtyContent returns content from the users temp file. Returns nil when UserID is not set - this is so that only file owners can see their temp.
type FileRecord ¶
type FileRecord struct { ID int64 UserID int64 `validate:"required"` Alias string `validate:"required"` // Friendly name for a file: bashrc Path string `validate:"required"` // Where the file lives: ~/.bashrc CurrentCommitID *int64 // The commit that the file is at. }
FileRecord models the files table. It stores the contents of a file at the current revision hash.
Both aliases and paths must be unique for each user.
type FileSearchResult ¶
type FileSearchResult struct { Username string Alias string Path string UpdatedAtString string UpdatedAt time.Time }
FileSearchResult is the result of a file search.
type FileSummary ¶
FileSummary summarizes a file.
func FilesByUsername ¶
func FilesByUsername(e Executor, username string, timezone *string) ([]FileSummary, error)
FilesByUsername returns all of a users files.
type FileTransaction ¶
type FileTransaction struct { FileExists bool FileID int64 CurrentCommitID int64 Hash string Path string Staged *TempFileRecord // contains filtered or unexported fields }
FileTransaction implements dotfile interfaces. This should be created with one of the exported functions not a literal.
func NewFileTransaction ¶
NewFileTransaction loads file information into a file transaction. Exported fields will be zero valued when the file doesn't exist.
func StageFile ¶
StageFile returns a file transaction loaded with a users temp file. Returns an error when the temp file does not exist.
func (*FileTransaction) DirtyContent ¶
func (ft *FileTransaction) DirtyContent() ([]byte, error)
DirtyContent returns the bytes from the users temp file. Returns an error if the temp file is not set.
func (*FileTransaction) HasCommit ¶
func (ft *FileTransaction) HasCommit(hash string) (exists bool, err error)
HasCommit returns whether the file has a commit with hash.
func (*FileTransaction) InsertCommit ¶
InsertCommit saves a new commit without changing the files current revision.
func (*FileTransaction) Revision ¶
func (ft *FileTransaction) Revision(hash string) ([]byte, error)
Revision returns the compressed content at hash.
func (*FileTransaction) SaveCommit ¶
SaveCommit saves a commit to the database. Sets the files current revision to the new commit.
func (*FileTransaction) SaveFile ¶
func (ft *FileTransaction) SaveFile(userID int64, alias, path string) error
SaveFile saves a new file that does yet have any commits. Callers should call SaveCommit in the same transaction.
func (*FileTransaction) SetRevision ¶
func (ft *FileTransaction) SetRevision(hash string) error
SetRevision sets the file to the commit at hash.
type FileView ¶
type FileView struct { FileRecord Content []byte Hash string }
FileView contains a file record and its uncompressed content.
type HTMLTable ¶
type HTMLTable struct { Columns []string Rows []interface{} Controls *PageControls }
HTMLTable contains methods for generating HTML output from rows and columns.
func SearchFiles ¶
func SearchFiles(e Executor, controls *PageControls, timezone *string) (*HTMLTable, error)
SearchFiles looks for files by their alias or path.
type PageControls ¶
PageControls maps url queries to query parameters.
func (*PageControls) Set ¶
func (p *PageControls) Set() error
Set sets private attributes from Values. This must be called before other methods.
query: 'q' page: 'p' order: 'o' order by: 'ob' limit: 'l'
type ReservedUsernameRecord ¶
ReservedUsernameRecord models the reserved_usernames table. It stores usernames that are not allowed to be registered.
type SessionRecord ¶
type SessionRecord struct { ID int64 Session string `validate:"required"` UserID int64 `validate:"required"` IP string CreatedAt time.Time DeletedAt *time.Time }
SessionRecord models the sessions table. It tracks a user's active sessions.
type TempFileRecord ¶
type TempFileRecord struct { ID int64 UserID int64 `validate:"required"` Alias string `validate:"required"` Path string `validate:"required"` Content []byte `validate:"required"` CreatedAt time.Time }
TempFileRecord models the temp_files table. It represents a changed/new file that has not yet been committed. Similar to an untracked or dirty file on the filesystem. This allows users to stage a file and view the result before saving. A User can have a single TempFile.
func TempFile ¶
func TempFile(e Executor, userID int64, alias string) (*TempFileRecord, error)
TempFile finds a user's temp file. Users can only have one temp file at a time so alias can be empty. When alias is present, ensures that temp file exists with alias.
func (*TempFileRecord) Create ¶
func (f *TempFileRecord) Create(e Executor) error
Create inserts or replaces a user's previous temp file. Strips carriage returns from content.
type UserRecord ¶
type UserRecord struct { ID int64 Username string `validate:"alphanum"` Email string `validate:"omitempty,email"` // Not required; users may opt in to enable account recovery. EmailConfirmed bool PasswordHash []byte CLIToken string `validate:"required"` // Allows CLI to write to server. PasswordResetToken *string Theme string Timezone string CreatedAt string }
UserRecord models the user table.
func CreateUser ¶
func CreateUser(e Executor, username, email, password string) (*UserRecord, error)
CreateUser inserts a new user into the users table.