13个版本

0.3.10 2023年6月12日
0.3.9 2023年4月30日
0.3.6 2022年8月12日
0.3.5 2022年7月28日
0.1.0 2022年7月21日

#67 in 配置

MIT许可证

41KB
864

quickenv:一个不干扰的环境管理器

direnv是一个按项目加载/卸载环境变量的管理器。它通过挂钩到您的shell并在执行cd时执行名为.envrc的shell脚本来实现。它将此shell脚本生成的环境变量加载到您的当前shell中。例如,它对于自动激活virtualenvs非常有用。

不幸的是,direnv在使用时可能有些“干扰”。首先,它在您的shell中运行自己的代码。这本身在终端响应速度方面并不明显,但人们最终编写的各种.envrc有时是。由于是任意代码,direnv没有可靠的方法来缓存.envrc的执行,因此每次您在项目之间切换时都会运行。

quickenvdirenv的替代品。它与现有的.envrc一起工作,因此是一个即插即用的替代品,但您与quickenv交互以及它如何加载环境变量是根本不同的。

  • quickenv不会挂钩到您的shell。它只需要添加到您的PATH中。
  • quickenv在更改目录时不会加载.envrc。相反,您需要使用quickenv reload按项目初始化quickenv,并在.envrc更改时重新运行该命令。
  • quickenv甚至不会将环境变量加载到您的shell中。相反,它创建模拟二进制文件,这些二进制文件将调度到正确的可执行文件。

quickenv深受volta的启发,volta通过提供最常见的命令的“模拟”二进制文件(例如yarnnpmnode)来实现nodejs的版本管理。

安装

quickenv还在开发中。也就是说,我每天都在工作中使用它

下载最新版本

# Into your bashrc/zshrc. This should be at the front of your PATH, such that
# quickenv can shim/shadow binaries effectively.
export PATH=$HOME/.quickenv/bin/:$PATH

# You can remove "direnv hook" from your bashrc/zshrc, but the tool needs to
# stay installed.

一些说明

  • quickenv目前假设direnv在您的路径中,以便加载其“标准库”。

  • quickenv目前还没有预编译的二进制文件。您需要安装Rust并使用Rust的包管理器Cargo安装它。

  • quickenv假设POSIX环境。

从源代码安装

cargo install quickenv  # latest stable release
cargo install --git https://github.com/untitaker/quickenv  # latest git SHA

用法

我们将查看sentry,因为那是我在使用的.envrc之一。请注意,Sentry的.envrc仅在MacOS上工作。

git clone https://github.com/getsentry/sentry
cd sentry

# Execute the .envrc and cache the resulting environment variables in ~/.quickenv/envs/.
# Sentry will prompt you to create a virtualenv, install dependencies via homebrew, etc.
# Re-run this command manually everytime the .envrc changes.
quickenv reload

# As part of executing the .envrc, a virtualenv has been created at './.venv/'.
# There are multiple commands available in '.venv/bin/', such as 'pytest' (a test
# runner), or 'sentry' (the main application).

# 'quickenv shim' makes those commands available in your shell.
quickenv shim

# These commands will now run with the virtualenv enabled.
sentry devserver --workers
pytest tests/sentry/

高级用法

# Alternatively you can shim commands explicitly. Be careful: Any command you
# missed (such as 'python' or 'pip') would run outside of the virtualenv!
quickenv shim sentry pytest

# You can also run commands within the current .envrc without shimming them.
quickenv exec -- pytest

# Your git hooks don't execute in the virtualenv for some reason? Just replace
# git with a binary that itself loads the virtualenv.
quickenv shim git

# Actually activate the virtualenv in your current shell. `quickenv vars`
# prints all the extra environment variables with which each shimmed binary runs.
set -o allexport
eval "$(quickenv vars)"
set +o allexport

# Or alternatively, substitute your shell with one where your .envrc is loaded
exec quickenv exec $SHELL

# Or shim 'bash', so that when you open a subshell, the virtualenv is activated.
quickenv shim bash

# Or shim 'make', so your Makefile runs in the virtualenv.
quickenv shim make

# Curious which binary is actually being executed?
quickenv which make
# /home/user/.quickenv/bin/make

# Or for general debugging, increase the log level:
QUICKENV_LOG=debug make
# [DEBUG quickenv] argv[0] is "make"
# [DEBUG quickenv] attempting to launch shim
# [DEBUG quickenv] abspath of self is /home/user/.quickenv/bin/make
# [DEBUG quickenv] removing own entry from PATH: /home/user/.quickenv/bin
# [DEBUG quickenv] execvp /usr/bin/make
# ...

许可证

许可协议为MIT,请参阅LICENSE

依赖关系

~8–18MB
~256K SLoC