Skip to content

Instantly share code, notes, and snippets.

@hg
Created July 19, 2024 13:28
Show Gist options
  • Select an option

  • Save hg/dd38ab4fe9ea1fcfad139a00db521dd4 to your computer and use it in GitHub Desktop.

Select an option

Save hg/dd38ab4fe9ea1fcfad139a00db521dd4 to your computer and use it in GitHub Desktop.
quickly create / list / delete purelymail.com aliases
#!/bin/bash
set -euo pipefail
readonly token=purely-mail-token
readonly url=https://purelymail.com/api/v0
readonly domain=mail-domain.com
readonly target=name@$domain
request() {
curl --silent --fail --location \
--header "Purelymail-Api-Token: $token" \
--json @- \
$url/"$1"
}
list() {
request listRoutingRules <<<'{}'
}
list_table() {
list |
jq --raw-output '.result.rules[] | "\(.id) \(if .matchUser == "" then "*" else .matchUser end) \(.domainName) \(.targetAddresses[])"' |
column --table --table-columns ID,ALIAS,DOMAIN,TARGET --table-right ALIAS
}
create() {
local user="$1"
request createRoutingRule <<EOF | jq .
{
"domainName": "$domain",
"prefix": false,
"matchUser": "$user",
"targetAddresses": ["$target"]
}
EOF
}
remove() {
local user id
user="$1"
id=$(list | jq --arg user "$user" '.result.rules.[] | select(.matchUser == $user).id')
if [[ -z $id ]]; then
echo >&2 "Alias $user not found"
exit 1
fi
request deleteRoutingRule <<<"{ \"routingRuleId\": $id }" | jq .
}
usage() {
echo "usage: $0 add alias | rm alias | ls"
exit 1
}
if [[ $# == 0 || $# -gt 2 ]]; then
usage
fi
case "$1" in
add | new) create "$2" ;;
rm | del | remove | delete) remove "$2" ;;
ls | list) list_table ;;
*) usage ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment