Documentation ¶
Index ¶
- Constants
- Variables
- func BashEscape(str string) string
- func CommandsDispatch(env Env, args []string) error
- func IgnoredEnv(key string) bool
- func Main(env Env, args []string, modBashPath string, modStdlib string, ...) error
- type Cmd
- type Config
- type Env
- type EnvDiff
- type FileTime
- type FileTimes
- func (times *FileTimes) Check() (err error)
- func (times *FileTimes) CheckOne(path string) (err error)
- func (times *FileTimes) Marshal() string
- func (times *FileTimes) NewTime(path string, modtime int64, exists bool) (err error)
- func (times *FileTimes) Unmarshal(from string) error
- func (times *FileTimes) Update(path string) (err error)
- type HookContext
- type RC
- type Shell
- type ShellExport
Constants ¶
const ( DIRENV_CONFIG = "DIRENV_CONFIG" DIRENV_BASH = "DIRENV_BASH" DIRENV_DEBUG = "DIRENV_DEBUG" DIRENV_DIR = "DIRENV_DIR" DIRENV_FILE = "DIRENV_FILE" DIRENV_WATCHES = "DIRENV_WATCHES" DIRENV_DIFF = "DIRENV_DIFF" DIRENV_DUMP_FILE_PATH = "DIRENV_DUMP_FILE_PATH" )
nolint
const ( ACK = 6 TAB = 9 LF = 10 CR = 13 US = 31 SPACE = 32 AMPERSTAND = 38 SINGLE_QUOTE = 39 PLUS = 43 NINE = 57 QUESTION = 63 UPPERCASE_Z = 90 OPEN_BRACKET = 91 BACKSLASH = 92 UNDERSCORE = 95 CLOSE_BRACKET = 93 BACKTICK = 96 LOWERCASE_Z = 122 TILDA = 126 DEL = 127 )
nolint
Variables ¶
var CmdAllow = &Cmd{ Name: "allow", Desc: "Grants direnv permission to load the given .envrc or .env file.", Args: []string{"[PATH_TO_RC]"}, Aliases: []string{"permit", "grant"}, Action: actionWithConfig(cmdAllowAction), }
CmdAllow is `direnv allow [PATH_TO_RC]`
var CmdApplyDump = &Cmd{ Name: "apply_dump", Desc: "Accepts a filename containing `direnv dump` output and generates a series of bash export statements to apply the given env", Args: []string{"FILE"}, Private: true, Action: actionSimple(cmdApplyDumpAction), }
CmdApplyDump is `direnv apply_dump FILE`
var CmdCurrent = &Cmd{ Name: "current", Desc: "Reports whether direnv's view of a file is current (or stale)", Args: []string{"PATH"}, Private: true, Action: actionSimple(cmdCurrentAction), }
CmdCurrent is `direnv current`
var CmdDeny = &Cmd{ Name: "block", Desc: "Revokes the authorization of a given .envrc or .env file.", Args: []string{"[PATH_TO_RC]"}, Aliases: []string{"deny", "revoke"}, Action: actionWithConfig(cmdDenyAction), }
CmdDeny is `direnv deny [PATH_TO_RC]`
var CmdDotEnv = &Cmd{ Name: "dotenv", Desc: "Transforms a .env file to evaluatable `export KEY=PAIR` statements", Args: []string{"[SHELL]", "[PATH_TO_DOTENV]"}, Private: true, Action: actionSimple(cmdDotEnvAction), }
CmdDotEnv is `direnv dotenv [SHELL [PATH_TO_DOTENV]]` Transforms a .env file to evaluatable `export KEY=PAIR` statements.
See: https://github.com/bkeepers/dotenv and https://github.com/ddollar/foreman
var CmdDump = &Cmd{ Name: "dump", Desc: "Used to export the inner bash state at the end of execution", Args: []string{"[SHELL]", "[FILE]"}, Private: true, Action: actionSimple(cmdDumpAction), }
CmdDump is `direnv dump`
var CmdEdit = &Cmd{ Name: "edit", Desc: `Opens PATH_TO_RC or the current .envrc or .env into an $EDITOR and allow the file to be loaded afterwards.`, Args: []string{"[PATH_TO_RC]"}, Action: actionWithConfig(cmdEditAction), }
CmdEdit is `direnv edit [PATH_TO_RC]`
var CmdExec = &Cmd{ Name: "exec", Desc: "Executes a command after loading the first .envrc or .env found in DIR", Args: []string{"DIR", "COMMAND", "[...ARGS]"}, Action: actionWithConfig(cmdExecAction), }
CmdExec is `direnv exec DIR <COMMAND> ...`
var CmdExport = &Cmd{ Name: "export", Desc: "loads an .envrc or .env and prints the diff in terms of exports", Args: []string{"SHELL"}, Private: true, Action: cmdWithWarnTimeout(actionWithConfig(exportCommand)), }
CmdExport is `direnv export $0`
var CmdFetchURL = &Cmd{ Name: "fetchurl", Desc: "Fetches a given URL into direnv's CAS", Args: []string{"<url>", "[<integrity-hash>]"}, Action: actionWithConfig(cmdFetchURL), }
CmdFetchURL is `direnv fetchurl <url> [<integrity-hash>]`
var CmdHelp = &Cmd{ Name: "help", Desc: "shows this help", Args: []string{"[SHOW_PRIVATE]"}, Aliases: []string{"--help"}, Action: actionSimple(func(env Env, args []string) (err error) { var showPrivate = len(args) > 1 fmt.Printf(`direnv v%s Usage: direnv COMMAND [...ARGS] Available commands ------------------ `, version) for _, cmd := range CmdList { var opts string if len(cmd.Args) > 0 { opts = " " + strings.Join(cmd.Args, " ") } if cmd.Private { if showPrivate { fmt.Printf("*%s%s:\n %s\n", cmd.Name, opts, cmd.Desc) } } else { fmt.Printf("%s%s:\n", cmd.Name, opts) for _, alias := range cmd.Aliases { if alias[0:1] != "-" { fmt.Printf("%s%s:\n", alias, opts) } } fmt.Printf(" %s\n", cmd.Desc) } } if showPrivate { fmt.Println("* = private commands") } return }), }
CmdHelp is `direnv help`
var CmdHook = &Cmd{ Name: "hook", Desc: "Used to setup the shell hook", Args: []string{"SHELL"}, Action: actionSimple(cmdHookAction), }
CmdHook is `direnv hook $0`
var CmdList []*Cmd
CmdList contains the list of all direnv sub-commands
var CmdPrune = &Cmd{
Name: "prune",
Desc: "removes old allowed files",
Action: actionWithConfig(cmdPruneAction),
}
CmdPrune is `direnv prune`
var CmdReload = &Cmd{ Name: "reload", Desc: "triggers an env reload", Action: actionWithConfig(func(env Env, args []string, config *Config) error { foundRC, err := config.FindRC() if err != nil { return err } if foundRC == nil { return fmt.Errorf(".envrc not found") } return foundRC.Touch() }), }
CmdReload is `direnv reload`
var CmdShowDump = &Cmd{ Name: "show_dump", Desc: "Show the data inside of a dump for debugging purposes", Args: []string{"DUMP"}, Private: true, Action: actionSimple(cmdShowDumpAction), }
CmdShowDump is `direnv show_dump`
var CmdStatus = &Cmd{ Name: "status", Desc: "prints some debug status information", Action: actionWithConfig(func(env Env, args []string, config *Config) error { fmt.Println("direnv exec path", config.SelfPath) fmt.Println("DIRENV_CONFIG", config.ConfDir) fmt.Println("bash_path", config.BashPath) fmt.Println("disable_stdin", config.DisableStdin) fmt.Println("warn_timeout", config.WarnTimeout) fmt.Println("whitelist.prefix", config.WhitelistPrefix) fmt.Println("whitelist.exact", config.WhitelistExact) loadedRC := config.LoadedRC() foundRC, err := config.FindRC() if err != nil { return err } if loadedRC != nil { formatRC("Loaded", loadedRC) } else { fmt.Println("No .envrc or .env loaded") } if foundRC != nil { formatRC("Found", foundRC) } else { fmt.Println("No .envrc or .env found") } return nil }), }
CmdStatus is `direnv status`
var CmdStdlib = &Cmd{ Name: "stdlib", Desc: "Displays the stdlib available in the .envrc execution context", Action: actionWithConfig(func(env Env, args []string, config *Config) error { fmt.Println(getStdlib(config)) return nil }), }
CmdStdlib is `direnv stdlib`
var CmdVersion = &Cmd{ Name: "version", Desc: "prints the version or checks that direnv is older than VERSION_AT_LEAST.", Args: []string{"[VERSION_AT_LEAST]"}, Aliases: []string{"--version"}, Action: actionSimple(func(env Env, args []string) error { semVersion := ensureVPrefixed(version) if len(args) > 1 { atLeast := ensureVPrefixed(args[1]) if !semver.IsValid(atLeast) { return fmt.Errorf("%s is not a valid semver version", atLeast) } cmp := semver.Compare(semVersion, atLeast) if cmp < 0 { return fmt.Errorf("current version %s is older than the desired version %s", semVersion, atLeast) } } else { fmt.Println(version) } return nil }), }
CmdVersion is `direnv version`
var CmdWatch = &Cmd{ Name: "watch", Desc: "Adds a path to the list that direnv watches for changes", Args: []string{"SHELL", "PATH..."}, Private: true, Action: actionSimple(cmdWatchAction), }
CmdWatch is `direnv watch SHELL [PATH...]`
var CmdWatchDir = &Cmd{ Name: "watch-dir", Desc: "Recursively adds a directory to the list that direnv watches for changes", Args: []string{"SHELL", "DIR"}, Private: true, Action: actionSimple(watchDirCommand), }
CmdWatchDir is `direnv watch-dir SHELL PATH`
var CmdWatchList = &Cmd{ Name: "watch-list", Desc: "Pipe pairs of `mtime path` to stdin to build a list of files to watch.", Args: []string{"[SHELL]"}, Private: true, Action: actionSimple(watchListCommand), }
CmdWatchList is `direnv watch-list`
var Editors = [][]string{
{"subl", "-w"},
{"mate", "-w"},
{"open", "-t", "-W"},
{"nano"},
{"vim"},
{"emacs"},
}
Editors contains a list of known editors and how to start them.
var IgnoredKeys = map[string]bool{ "DIRENV_CONFIG": true, "DIRENV_BASH": true, "DIRENV_IN_ENVRC": true, "COMP_WORDBREAKS": true, "PS1": true, "OLDPWD": true, "PWD": true, "SHELL": true, "SHELLOPTS": true, "SHLVL": true, "_": true, }
IgnoredKeys is list of keys we don't want to deal with
Functions ¶
func BashEscape ¶
https://github.com/solidsnack/shell-escape/blob/master/Text/ShellEscape/Bash.hs
A Bash escaped string. The strings are wrapped in @$\'...\'@ if any bytes within them must be escaped; otherwise, they are left as is. Newlines and other control characters are represented as ANSI escape sequences. High bytes are represented as hex codes. Thus Bash escaped strings will always fit on one line and never contain non-ASCII bytes.
func CommandsDispatch ¶
CommandsDispatch is called by the main() function to dispatch to a sub-command
func IgnoredEnv ¶
IgnoredEnv returns true if the key should be ignored in environment diffs.
Types ¶
type Cmd ¶
type Cmd struct { Name string Desc string Args []string Aliases []string Private bool Action action }
Cmd represents a direnv sub-command
type Config ¶
type Config struct { Env Env WorkDir string // Current directory ConfDir string CacheDir string DataDir string SelfPath string BashPath string RCFile string TomlPath string DisableStdin bool StrictEnv bool LoadDotenv bool WarnTimeout time.Duration WhitelistPrefix []string WhitelistExact map[string]bool }
Config represents the direnv configuration and state.
func LoadConfig ¶
LoadConfig opens up the direnv configuration from the Env.
func (*Config) EnvFromRC ¶
EnvFromRC loads an RC from a specified path and returns the new environment
type Env ¶
Env is a map representation of environment variables.
func GetEnv ¶
func GetEnv() Env
GetEnv turns the classic unix environment variables into a map of key->values which is more handy to work with.
NOTE: We don't support having two variables with the same name. I've never seen it used in the wild but according to POSIX it's allowed.
func LoadEnvJSON ¶
LoadEnvJSON unmarshals the env back from a JSON string
func (Env) CleanContext ¶
func (env Env) CleanContext()
CleanContext removes all the direnv-related environment variables. Call this after reverting the environment, otherwise direnv will just be amnesic about the previously-loaded environment.
func (Env) Copy ¶
Copy returns a fresh copy of the env. Because the env is a map under the hood, we want to get a copy whenever we mutate it and want to keep the original around.
func (Env) Fetch ¶
Fetch tries to get the value associated with the given 'key', or returns the provided default if none is set.
Note that empty environment variables are considered to be set.
type EnvDiff ¶
EnvDiff represents the diff between two environments
func BuildEnvDiff ¶
BuildEnvDiff analyses the changes between 'e1' and 'e2' and builds an EnvDiff out of it.
func LoadEnvDiff ¶
LoadEnvDiff unmarshalls a gzenv string back into an EnvDiff.
func (*EnvDiff) Patch ¶
Patch applies the diff to the given env and returns a new env with the changes applied.
type FileTime ¶
type FileTime struct { Path string `json:"path"` Modtime int64 `json:"modtime"` Exists bool `json:"exists"` }
FileTime represents a single recorded file status
type FileTimes ¶
type FileTimes struct {
// contains filtered or unexported fields
}
FileTimes represent a record of all the known files and times
func NewFileTimes ¶
func NewFileTimes() (times FileTimes)
NewFileTimes creates a new empty FileTimes
func (*FileTimes) NewTime ¶
NewTime add the file on path, with modtime and exists flag to the list of known files.
type HookContext ¶
type HookContext struct { // SelfPath is the unescaped absolute path to direnv SelfPath string }
HookContext are the variables available during hook template evaluation
type RC ¶
type RC struct {
// contains filtered or unexported fields
}
RC represents the .envrc or .env file
func RCFromPath ¶
RCFromPath inits the RC from a given path
type Shell ¶
type Shell interface { // Hook is the string that gets evaluated into the host shell config and // setups direnv as a prompt hook. Hook() (string, error) // Export outputs the ShellExport as an evaluatable string on the host shell Export(e ShellExport) string // Dump outputs and evaluatable string that sets the env in the host shell Dump(env Env) string }
Shell is the interface that represents the interaction with the host shell.
var Bash Shell = bash{}
Bash shell instance
var Elvish Shell = elvish{}
Elvish add support for the elvish shell
var Fish Shell = fish{}
Fish adds support for the fish shell as a host
var GitHubActions Shell = gha{}
GitHubActions shell instance
var GzEnv Shell = gzenvShell(0)
GzEnv is not a real shell. used for internal purposes.
var JSON Shell = jsonShell{}
JSON is not really a shell but it fits. Useful to add support to editor and other external tools that understand JSON as a format.
var Tcsh Shell = tcsh{}
Tcsh adds support for the tickle shell
var Vim Shell = vim{}
Vim adds support for vim. Not really a shell but it's handly.
var Zsh Shell = zsh{}
Zsh adds support for the venerable Z shell.
func DetectShell ¶
DetectShell returns a Shell instance from the given target.
target is usually $0 and can also be prefixed by `-`
type ShellExport ¶
ShellExport represents environment variables to add and remove on the host shell.
func (ShellExport) Add ¶
func (e ShellExport) Add(key, value string)
Add represents the addition of a new environment variable
func (ShellExport) Remove ¶
func (e ShellExport) Remove(key string)
Remove represents the removal of a given `key` environment variable.
Source Files ¶
- cmd_allow.go
- cmd_apply_dump.go
- cmd_current.go
- cmd_deny.go
- cmd_dotenv.go
- cmd_dump.go
- cmd_edit.go
- cmd_exec.go
- cmd_export.go
- cmd_fetchurl.go
- cmd_help.go
- cmd_hook.go
- cmd_prune.go
- cmd_reload.go
- cmd_show_dump.go
- cmd_status.go
- cmd_stdlib.go
- cmd_version.go
- cmd_watch.go
- cmd_watch_dir.go
- cmd_watch_list.go
- commands.go
- config.go
- const.go
- env.go
- env_diff.go
- file_times.go
- log.go
- look_path.go
- mod.go
- rc.go
- shell.go
- shell_bash.go
- shell_elvish.go
- shell_fish.go
- shell_gha.go
- shell_gzenv.go
- shell_json.go
- shell_tcsh.go
- shell_vim.go
- shell_zsh.go
- stdlib.go