Documentation
¶
Overview ¶
Package appdata resolves where the application keeps its per-user data: the SQLite database, the copies of the prompt library and the launch defaults kept beside it, generated launch scripts, and logs. It also writes and reads those files, readable only by the user, and the request to start over that the next start carries out (RequestReset).
The location follows each operating system's convention. The resolver itself is a pure function of the OS name, the home directory, and the environment, so every branch is testable on any host even though only macOS is supported at run time today. A run may name another location with DataDirFlag (FromArgs), as a sandbox to test the app on fresh data does.
Index ¶
- Constants
- Variables
- func ClearResetRequest(path string) error
- func Dir(goos, home string, getenv func(key string) string) (string, error)
- func FromArgs(args []string) (string, error)
- func ReadFile(path string, limit int64) ([]byte, error)
- func RehearsalDir(goos, home string, getenv func(key string) string) (string, error)
- func RequestReset(path string, by ResetBy, now time.Time) error
- func WriteFile(path string, data []byte) error
- type Paths
- type ResetBy
- type ResetRequest
Constants ¶
const ( // PrivateDirPerm is the mode of every directory the application creates. PrivateDirPerm fs.FileMode = 0o700 // PrivateFilePerm is the mode of every file the application writes. PrivateFilePerm fs.FileMode = 0o600 )
Permissions for everything the application writes: its data is private to the user.
const DataDirFlag = "--data-dir"
DataDirFlag runs the app on the data directory it names, as --data-dir=<absolute path>, in place of the user's own: a sandbox, such as the one magex app:fresh opens, whose database, copies, and logs all stay in that directory, so the user's own data is never touched.
const ResetRequestVersion = 1
ResetRequestVersion is the version of the reset request RequestReset writes.
Variables ¶
var ( // ErrUnsupportedOS is returned for an operating system with no known data location. ErrUnsupportedOS = errors.New("unsupported operating system") // ErrNoHomeDir is returned when the home directory is needed but empty. ErrNoHomeDir = errors.New("home directory is not set") // ErrNoAppDataEnv is returned on Windows when %AppData% is not set. ErrNoAppDataEnv = errors.New("APPDATA environment variable is not set") )
var ( // ErrTooLarge is returned by ReadFile for a file larger than the limit it was given. ErrTooLarge = errors.New("the file is too large") // ErrNotAFile is returned by ReadFile for a path that is not a regular file. ErrNotAFile = errors.New("not a file") )
var ErrDataDirFlag = errors.New("--data-dir needs an absolute path, as --data-dir=/path/to/folder")
ErrDataDirFlag is returned for a DataDirFlag that names no absolute path.
var ErrNoResetVersion = errors.New("the reset request has no version")
ErrNoResetVersion is returned for a reset request with no version.
Functions ¶
func ClearResetRequest ¶ added in v0.3.0
ClearResetRequest removes the request to start over at path, once it was carried out or could not be read. A request that is not there is already cleared.
func Dir ¶
Dir returns the per-user data directory for the given OS, home directory, and environment lookup:
- darwin: <home>/Library/Application Support/<DataDirName>
- linux: $XDG_DATA_HOME/<Slug>, else <home>/.local/share/<Slug>
- windows: %AppData%/<DataDirName>
Any other OS returns ErrUnsupportedOS.
func FromArgs ¶ added in v0.3.0
FromArgs returns the data directory args name with DataDirFlag, cleaned, or "" when they name none. A flag with no value, or with a path that is not absolute, is ErrDataDirFlag: a relative path would depend on where the app happened to start.
func ReadFile ¶ added in v0.2.0
ReadFile reads the regular file at path, refusing one larger than limit bytes with ErrTooLarge before reading it. A missing file is an error matching fs.ErrNotExist.
func RehearsalDir ¶ added in v0.3.1
RehearsalDir returns the data directory of a rehearsal build, one magex app:rehearse makes to try an update end to end on one Mac: beside the per-user data directory Dir returns, named as it is: brand.RehearsalSlug on Linux, whose is named by the slug, and brand.RehearsalDataDirName elsewhere. An update opens the new version with no arguments, so a rehearsal finds its data here on its own, and never reads or writes the user's.
func RequestReset ¶ added in v0.3.0
RequestReset writes a request to start over to path, readable only by the user, replacing any request there, and creating its folder when the app never made it: the next start of the app carries it out.
Types ¶
type Paths ¶
type Paths struct {
Root string // the per-user data directory
DB string // the SQLite database file
Prompts string // the copy of the prompt library kept beside the database
LaunchDefaults string // the copy of the launch defaults kept beside the database
Autopilot string // the copy of the Autopilot routines kept beside the database
AutopilotLock string // the lock that lets one copy of the app run Autopilot's engine on this data
AgentRuns string // each background agent run's own folder (its prompt and settings), named by its session ID
Backups string // the copies of the user's setup an import or a reset saves before it changes anything
Launch string // generated launch scripts and prompt files
Logs string // log files
ResetRequest string // the request to start over that the next start carries out (see RequestReset)
Updates string // the app's own updates, downloaded and checked, each in a folder named for its version
}
Paths are the application's data locations, all under one root directory.
func ResolveRehearsal ¶ added in v0.3.1
ResolveRehearsal returns the data locations of a rehearsal build for the current user on the current OS (see RehearsalDir).
type ResetRequest ¶ added in v0.3.0
type ResetRequest struct {
// Version is ResetRequestVersion for a request this build wrote.
Version int `json:"version"`
// RequestedAt is when the user asked.
RequestedAt time.Time `json:"requestedAt"`
// By is where they asked.
By ResetBy `json:"by"`
}
ResetRequest is the user's request to start over, which the next start of the app carries out before anything opens the database: it sets the database aside and starts a fresh one, bringing back the prompts, the launch defaults, and the user's own scripts.
func ReadResetRequest ¶ added in v0.3.0
func ReadResetRequest(path string) (ResetRequest, bool, error)
ReadResetRequest reads the request to start over at path, and reports whether there is one. A file that is not a request, as one a newer build wrote in a form this build can't read, is an error, and holds no request.