{"modulePath":"github.com/intentdriven/abcd","version":"v0.17.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package shellpath\n\nPackage 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:\n\n  - 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;\n  - it never adds the folder twice: a startup file that already adds it (or a PATH that already reaches it) is left alone;\n  - 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;\n  - it adds exactly one line, ending in Marker, and records it in abcd's home, so Remove takes out that line and nothing else.\n\nWhich file each shell reads (Plan documents the choice):\n\n  - 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.\n  - 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.\n  - bash on Linux: ~/.bashrc, which the non-login shell a terminal opens reads.\n\n## Constants\n\n```go\nconst Marker = \"# added by abcd ahoy install\"\n```\nMarker 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.\n\n```go\nconst MaxFileBytes = 256 \u003c\u003c 10\n```\nMaxFileBytes is the largest startup file abcd edits. A file it cannot read whole is a file it cannot vouch for leaving otherwise unchanged.\n\n## Variables\n\n```go\nvar (\n\tErrNotOffered = errors.New(\"the plan offers no edit\")\n\tErrChanged    = errors.New(\"the file changed since the question was asked, so nothing was written\")\n)\n```\nErrors Apply and Remove return; the text completes \"…: \\\u003ctext\u003e\".\n\n## Functions\n\n```go\nfunc ExportLine(home, dir string) string\n```\nExportLine 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).\n\n## Types\n\n```go\ntype Applied struct {\n\tFile   string `json:\"file\"`\n\tLine   string `json:\"line\"`\n\tBackup string `json:\"backup,omitempty\"` // the copy of the file as it was; \"\" when Apply created it\n\t// RecordNote is set when the line was added but its record could not be\n\t// kept: Remove still finds the line by its marker.\n\tRecordNote string `json:\"record_note,omitempty\"`\n\t// PruneNote is set when older copies could not be pruned.\n\tPruneNote string `json:\"prune_note,omitempty\"`\n}\n```\nApplied is what Apply did.\n\n```go\nfunc Apply(env Env, p Plan) (Applied, error)\n```\nApply adds the plan's line to its file, on the person's yes. The file is judged and read again first, and refused when it is no longer the file the person was shown.\n\n```go\ntype Env struct {\n\tHome    string // the person's home directory, absolute\n\tShell   string // $SHELL, the login shell\n\tZDotDir string // $ZDOTDIR, where zsh looks for its startup files\n\tPath    string // $PATH\n\tGOOS    string // runtime.GOOS\n}\n```\nEnv is the part of the environment a plan reads. CurrentEnv fills it from the process; a test builds its own.\n\n```go\nfunc CurrentEnv() Env\n```\nCurrentEnv reads Env from the process environment.\n\n```go\ntype Plan struct {\n\tState  State  `json:\"state\"`\n\tShell  string `json:\"shell,omitempty\"` // \"zsh\" or \"bash\"; \"\" for another shell\n\tFile   string `json:\"file,omitempty\"`  // the file to edit, or the one that already adds the folder\n\tLine   string `json:\"line\"`            // the marked line abcd adds\n\tExport string `json:\"export\"`          // the same line without the marker, for the checklist\n\tReason string `json:\"reason,omitempty\"`\n\tCreate bool   `json:\"create,omitempty\"` // File does not exist yet; Apply creates it\n\t// contains filtered or unexported fields\n}\n```\nPlan is what Plan found: the file and line to show the person before asking, or why there is nothing to ask.\n\n```go\nfunc PlanFor(env Env, dir string) Plan\n```\nPlanFor decides, reading only, whether abcd may add dir to the PATH in the startup file the person's login shell reads, and which file and line that is. It never writes.\n\n```go\ntype Removed struct {\n\tRemoved bool   `json:\"removed\"`\n\tFile    string `json:\"file,omitempty\"`\n\tBackup  string `json:\"backup,omitempty\"`\n\tNote    string `json:\"note\"`\n}\n```\nRemoved is what Remove did.\n\n```go\nfunc Remove(env Env, dir string) (Removed, error)\n```\nRemove 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.\n\n```go\ntype RestoreError struct {\n\tFile   string // the startup file\n\tBackup string // the copy of it as it was\n\tCause  string // why putting it back failed\n}\n```\nRestoreError 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.\n\n```go\nfunc (e *RestoreError) Error() string\n```\n\n```go\ntype State string\n```\nState is what a plan found.\n\n```go\nconst (\n\t// OnPath: the PATH already reaches the folder; there is nothing to do.\n\tOnPath State = \"on_path\"\n\t// AlreadyAdded: a startup file already adds the folder (abcd's own line\n\t// or the person's); nothing is edited, and a new terminal picks it up.\n\tAlreadyAdded State = \"already_added\"\n\t// Offer: abcd may add its line to File, on the person's yes.\n\tOffer State = \"offer\"\n\t// Checklist: abcd will not edit the file (Reason says why); the line is\n\t// the person's to add.\n\tChecklist State = \"checklist\"\n)\n```\n\n","path":"github.com/intentdriven/abcd/internal/core/shellpath","name":"shellpath","synopsis":"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).","isRedistributable":true}
