Documentation
¶
Overview ¶
Package shellpath is the one place abcd edits a person's shell settings: the line that puts the folder abcd installs into on the PATH (itd-2610240919030303, adr-2610241022330872). It never prints and never asks; `abcd ahoy install` asks the question and the setup flow lists the line when the answer is no. What it does on a yes is held to one bar:
- it edits only the zsh or bash startup file the person's login shell reads in a new terminal, and only one it can read whole: a regular file, not a link, owned by the person, writable by no one else, and within MaxFileBytes;
- it never adds the folder twice: a startup file that already adds it (or a PATH that already reaches it) is left alone;
- it keeps a copy of the file in abcd's backups folder first (no copy, no write), replaces the file atomically, reads it back, and puts it back from the copy when it does not say what was written;
- it adds exactly one line, ending in Marker, and records it in abcd's home, so Remove takes out that line and nothing else.
Which file each shell reads (Plan documents the choice):
- zsh: ${ZDOTDIR:-$HOME}/.zshrc, which every interactive zsh reads, the login shell a macOS terminal opens and the non-login one most Linux terminals open alike; .zprofile is read by login shells only.
- bash on macOS: the login file bash reads, the first of .bash_profile, .bash_login and .profile that exists, else a new .bash_profile; a macOS terminal opens a login shell, which never reads .bashrc, and creating .bash_profile beside an existing .profile would stop bash reading .profile.
- bash on Linux: ~/.bashrc, which the non-login shell a terminal opens reads.
Index ¶
Constants ¶
const Marker = "# added by abcd ahoy install"
Marker ends the one line abcd adds, so the line can be found and removed again and a person reading the file can tell who put it there.
const MaxFileBytes = 256 << 10
MaxFileBytes is the largest startup file abcd edits. A file it cannot read whole is a file it cannot vouch for leaving otherwise unchanged.
Variables ¶
var ( ErrNotOffered = errors.New("the plan offers no edit") ErrChanged = errors.New("the file changed since the question was asked, so nothing was written") )
Errors Apply and Remove return; the text completes "…: <text>".
Functions ¶
func ExportLine ¶
ExportLine is the line that puts dir on the PATH, in the form a person pastes: $HOME stands for the home directory so the line reads the same on every account. The folder comes first, ahead of the PATH it had, so the abcd abcd installs is the one that answers rather than an older copy in a system folder (the shadowing ahoy reports as symlink.shadowed).
Types ¶
type Applied ¶
type Applied struct {
File string `json:"file"`
Line string `json:"line"`
Backup string `json:"backup,omitempty"` // the copy of the file as it was; "" when Apply created it
// RecordNote is set when the line was added but its record could not be
// kept: Remove still finds the line by its marker.
RecordNote string `json:"record_note,omitempty"`
// PruneNote is set when older copies could not be pruned.
PruneNote string `json:"prune_note,omitempty"`
}
Applied is what Apply did.
type Env ¶
type Env struct {
Home string // the person's home directory, absolute
Shell string // $SHELL, the login shell
ZDotDir string // $ZDOTDIR, where zsh looks for its startup files
Path string // $PATH
GOOS string // runtime.GOOS
}
Env is the part of the environment a plan reads. CurrentEnv fills it from the process; a test builds its own.
type Plan ¶
type Plan struct {
State State `json:"state"`
Shell string `json:"shell,omitempty"` // "zsh" or "bash"; "" for another shell
File string `json:"file,omitempty"` // the file to edit, or the one that already adds the folder
Line string `json:"line"` // the marked line abcd adds
Export string `json:"export"` // the same line without the marker, for the checklist
Reason string `json:"reason,omitempty"`
Create bool `json:"create,omitempty"` // File does not exist yet; Apply creates it
// contains filtered or unexported fields
}
Plan is what Plan found: the file and line to show the person before asking, or why there is nothing to ask.
type Removed ¶
type Removed struct {
Removed bool `json:"removed"`
File string `json:"file,omitempty"`
Backup string `json:"backup,omitempty"`
Note string `json:"note"`
}
Removed is what Remove did.
func Remove ¶
Remove takes out the line abcd added and nothing else, for `abcd ahoy uninstall`. The file comes from the record abcd kept; with none, every startup file the person's shell reads is searched for abcd's exact line for dir. A line the person has since edited is no longer abcd's and is left.
type RestoreError ¶
type RestoreError struct {
File string // the startup file
Backup string // the copy of it as it was
Cause string // why putting it back failed
}
RestoreError is the one failure that leaves a startup file other than as it was: it did not read back as written, and putting it back from the copy failed too, so the person has a step to take by hand. Its fields are in the tilde form.
func (*RestoreError) Error ¶
func (e *RestoreError) Error() string
type State ¶
type State string
State is what a plan found.
const ( // OnPath: the PATH already reaches the folder; there is nothing to do. OnPath State = "on_path" // AlreadyAdded: a startup file already adds the folder (abcd's own line // or the person's); nothing is edited, and a new terminal picks it up. AlreadyAdded State = "already_added" // Offer: abcd may add its line to File, on the person's yes. Offer State = "offer" // Checklist: abcd will not edit the file (Reason says why); the line is // the person's to add. Checklist State = "checklist" )