dotenvx
dotenvの後継ツール。後継といっても名ばかりではなく、実際にdotenvの作者が後継として作っている。
BSD3項ライセンス。
https://dotenvx.com
主要な追加機能:
環境ごとの複数ファイルのサポート
暗号化のサポート
Secp256k1による公開鍵暗号化を利用するようだ
単体のツールで動作するようになった
.env内部で変数展開やプロセス展開ができる
暗号化に対応した次世代dotenvツールdotenvxを使う
ちょっと使ってみる
インストール
公式サイトからはbrewとcurlで入れる方法が提供されている。npmからも利用できる
とりあえずcurlで入れる
code:curl.sh
curl -fsS https://dotenvx.sh | sh
INSTALLATION_FAILED the installation directory /usr/local/bin is not writable by the current user
? run as root [curl -sfS https://dotenvx.sh | sudo sh] or choose a writable directory like your current directory [curl -sfS "https://dotenvx.sh?directory=." | sh]
! こういうのドメインクラックされたり死んだりしやすいから普通にshじゃなくてcomとかnetにしてほしいんだけどな。
rootで走らせろと抜かしおった。うーん・・・
信じて入れる
shの部分をsudo shにする
code:dotenv help.sh
% dotenvx help
Usage: @dotenvx/dotenvx options command
a better dotenv–from the creator of dotenv
Options:
-l, --log-level <level> set log level (default: "info")
-q, --quiet sets log level to error
-v, --verbose sets log level to verbose
-d, --debug sets log level to debug
-V, --version output the version number
-h, --help display help for command
Commands:
run options inject env at runtime dotenvx run -- yourcommand
get options key return a single environment variable
set options <KEY> <value> set a single environment variable
encrypt options convert .env file(s) to encrypted .env file(s)
pro 🏆 pro
ext command args... 🔌 extensions
help command display help for command
proなる謎のオプションがある。small business向けに稼ぐつもりらしい
とりあえずsetしてみる
code:set.sh
% dotenvx set HELLO world
missing .env file (/home/windymelt/temp/dotenvx/.env)
? add one with echo "HELLO=World" > .env and re-run dotenvx set
最初は自分で作ってくれということらしい。touch .envする。
したら暗号化されてしまった。
code:set2.sh
% cat .env
#/-------------------DOTENV_PUBLIC_KEY--------------------/
#/ public-key encryption for .env files /
#/ how it works(https://dotenvx.com/encryption) /
#/----------------------------------------------------------/
DOTENV_PUBLIC_KEY="031301ef36ea06a00d97ef24eb2e9eb731350f6688cdae558e1ac10049b2bbe187"
# .env
HELLO="encrypted:BA2gchsfZOSQqXPRNGzT/CxniRQsZLq2+rndiyHvUlRk7//pUlPk8BDAgKlhwzbHcSIRqDnuUQMM3+BBGIZtJPbOIzhhLt7O75R/zEsEZ/AdhMIxJi3SgIGjsOgnYernjoqPzArX"
暗号化は指示したときにのみ発動してほしい気もするけどまあいいか。
! setするときに -p するとplainに、-eするとenctyptedになる(-eがデフォルト)
getすると得られる。
code:get.sh
% dotenvx get HELLO
world
コマンドを走らせるにはdotenvx run -- コマンドする
code:script.scala.sc
//> using scala 3.4.1
println("Hello, dotenvx!")
println(s"Value of FOO is: ${sys.env.get("FOO")}")
code:run.sh
% dotenvx run -- scala-cli script.scala.sc
dotenvx@1.5.0 injecting env (3) from .env
Hello, dotenvx!
Value of FOO is: Some(bar)
あたりまえだが普通に動いた。
複数プロファイルを利用してみる。
.env: ENV="dev"
.env.prod: ENV="prod"
println(s"Environment is: ${sys.env.get("ENV")}")を実行させる
code:env.sh
% dotenvx run -- scala-cli script.scala.sc
dotenvx@1.5.0 injecting env (4) from .env
Environment is: Some(dev)
% dotenvx run -f .env.prod -- scala-cli script.scala.sc
dotenvx@1.5.0 injecting env (1) from .env.prod
Environment is: Some(PROD)
注意すべき点として、-fはrunの後に置く必要がある。
疑問
dotenvxの中身なに
file $(which dotenvx)したら静的バイナリだった。GJ
設定の共用ってできる?
複数回-fしたら先勝ちで上書きされる
--overloadするとそのぎゃく
マイグレーションどうするか
チームで設定を共有できるか
社でよくやる運用: 1Passwordを使ってop injectしている
それはそのままで.envの種類を分けるのはどうか
docker-composeとnpmで破綻しなければ使えるのではないか
主に嬉しいのは.envを切り替えられる点
切り替え忘れするとダルいのでこれを防ぐ手段があるか
nextが使っている環境変数やNODE_ENVなどとの整合性を取れるか
https://dotenvx.com/docs/advanced/run-convention