#!/usr/bin/env bash
set -x

mise use sops age
age="$(mise x -- age-keygen 2>&1)"
age_pub="$(echo "$age" | grep "# public key:" | awk '{print $4}')"
MISE_SOPS_AGE_KEY="$(echo "$age" | grep "AGE-SECRET-KEY")"
export MISE_SOPS_AGE_KEY

# json
echo '{ "SECRET": "mysecret" }' >.env.json
mise x -- sops encrypt -i --age "$age_pub" .env.json
assert "mise set _.file=.env.json"
assert_contains "mise env" "export SECRET=mysecret"

# yaml
age_pub="$(mise x -- age-keygen -o ~/age.txt 2>&1 | awk '{print $3}')"
mise settings set sops.age_key_file "~/age.txt"
echo 'SECRET: mysecret' >.env.yaml
mise x -- sops encrypt -i --age "$age_pub" .env.yaml
export MISE_SOPS_AGE_KEY=
assert "mise set _.file=.env.yaml"
assert_contains "mise env" "export SECRET=mysecret"

# sops
mise settings set sops.rops 0
assert_contains "mise env" "export SECRET=mysecret"
