Created
July 19, 2024 01:57
-
-
Save austinlparker/33ad126e29b89c1497d0d6951e1383a5 to your computer and use it in GitHub Desktop.
A fish function that helps you synchronize .env files to and from a 1Password vault.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env fish | |
# env-helper | |
# you'll need the 1Password CLI installed to use this | |
# change the vault name to whichever vault you want to use | |
function confirm | |
while true | |
read -p 'echo "Confirm (y/n):"' -l confirm | |
switch $confirm | |
case Y y | |
return 0 | |
case '' N n | |
return 1 | |
end | |
end | |
end | |
function push_env | |
if test (count $argv) -lt 1 | |
echo "Usage: push_env <name> [--debug]" | |
return 1 | |
end | |
set name $argv[1] | |
set debug_flag (contains -- "--debug" $argv) | |
if not test -f .env | |
echo ".env not found!" | |
return 1 | |
end | |
if op document get "$name" --vault="dev" > /dev/null 2>&1 | |
echo "A document with the name $name already exists in 1Password. Do you want to overwrite it?" | |
if not confirm | |
echo "Push operation cancelled." | |
return 1 | |
end | |
if test $debug_flag | |
op document delete "$name" --vault="dev" | |
else | |
op document delete "$name" --vault="dev" > /dev/null | |
end | |
end | |
if test $debug_flag | |
op document create .env --title="$name" --vault="dev" | |
else | |
op document create .env --title="$name" --vault="dev" > /dev/null | |
end | |
if test $status -eq 0 | |
echo ".env file pushed to 1Password vault as $name" | |
else | |
echo "Failed to push .env file to 1Password vault." | |
return 1 | |
end | |
end | |
function pull_env | |
if test (count $argv) -lt 1 | |
echo "Usage: pull_env <name> [--debug]" | |
return 1 | |
end | |
set name $argv[1] | |
set debug_flag (contains -- "--debug" $argv) | |
if test -f .env | |
echo "A file named .env already exists. Do you want to overwrite it?" | |
if not confirm | |
echo "Pull operation cancelled." | |
return 1 | |
end | |
end | |
rm .env | |
if test $debug_flag | |
op document get "$name" --vault="dev" --output .env | |
else | |
op document get "$name" --vault="dev" --output .env > /dev/null | |
end | |
if test $status -eq 0 | |
echo ".env file pulled from 1Password vault and saved as .env" | |
else | |
echo "Failed to pull .env file from 1Password vault." | |
return 1 | |
end | |
end | |
function env-helper | |
if test (count $argv) -lt 1 | |
echo "Usage: env-helper <command> <args>" | |
echo "Available commands: push, pull" | |
return 1 | |
end | |
set command $argv[1] | |
set -e argv[1] | |
switch $command | |
case "push" | |
push_env $argv | |
case "pull" | |
pull_env $argv | |
case '*' | |
echo "Unknown command: $command" | |
echo "Available commands: push, pull" | |
return 1 | |
end | |
end | |
env-helper $argv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment