Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add environ format #973

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

feat: add environ format #973

wants to merge 1 commit into from

Conversation

zimbatm
Copy link
Member

@zimbatm zimbatm commented Jul 22, 2022

Adds a direnv export environ command that outputs the diff in
<key>(=<value)?\0 format. It's a simple format that is quite close to
/proc/<pid>/environ on Linux. Each line is NUL-terminated. If the = is
not present on a line, consider that the should be unset.

The idea was to find a format that could eventually replace the evals in
the hooks in the various shells.

@zimbatm
Copy link
Member Author

zimbatm commented Jul 22, 2022

Here is a new hook that replaces `eval "$(direnv hook bash)", for example:

_direnv_hook() {
  local previous_exit_status=$? line;
  trap -- '' SIGINT;
  while IFS= read -d '' -r line; do
    if [[ "$line" = *"="* ]]; then
      echo export "$line";
    else
      echo unset "$line";
    fi;
  done < <(direnv export environ);
  trap - SIGINT;
  return $previous_exit_status;
};
if ! [[ "${PROMPT_COMMAND:-}" =~ _direnv_hook ]]; then
  PROMPT_COMMAND="_direnv_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi

@zimbatm
Copy link
Member Author

zimbatm commented Jul 22, 2022

Same for zsh:

_direnv_hook() {
  trap -- '' SIGINT;
  local line;
  while IFS= read -d '' -r line; do
    if [[ "$line" = *"="* ]]; then
      echo export "$line";
    else
      echo unset "$line";
    fi;
  done < <(direnv export environ);
  trap - SIGINT;
}
typeset -ag precmd_functions;
if [[ -z "${precmd_functions[(r)_direnv_hook]+1}" ]]; then
  precmd_functions=( _direnv_hook ${precmd_functions[@]} )
fi
typeset -ag chpwd_functions;
if [[ -z "${chpwd_functions[(r)_direnv_hook]+1}" ]]; then
  chpwd_functions=( _direnv_hook ${chpwd_functions[@]} )
fi

Adds a `direnv export environ` command that outputs the diff in
`<key>(=<value)?\0` format. It's a simple format that is quite close to
/proc/<pid>/environ on Linux. Each line is NUL-terminated. If the `=` is
not present on a line, consider that the <key> should be unset.

The idea was to find a format that could eventually replace the evals in
the hooks in the various shells.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant