Files
supermarket/lib.sh
T
davidandClaude Opus 5.5 f275f039f9 Phase 1, part 2: Inbox, scheduled publishing and UTM tags
- Inbox: a top-level page, with a count in the menu, for everything
  waiting for approval; Content's drafts are reviewed there. /drafts
  redirects to it.
- Scheduling: publish a draft now or at a time (migration 3 adds
  publish_at). A scheduler in the app publishes due drafts every 30
  seconds. Posts more than 12 hours overdue, or refused before posting,
  go back to the Inbox with the reason. Content gets Scheduled and
  Published tabs.
- UTM tags on links to the company's own websites and product pages,
  added when publishing; drafts stay untagged.
- The container runs in the computer's time zone, so times are shown
  and read correctly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 13:17:38 -07:00

108 lines
3.5 KiB
Bash

#!/bin/bash
#
# Purpose : Paths and helpers shared by install.sh, update.sh and
# hermes/setup.sh. Source this file.
# Used by the scripts that source this file
# shellcheck disable=SC2034
SUPERMARKET_REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COREDESKTOP="$HOME/.local/share/coredesktop"
DATA="${XDG_DATA_HOME:-$HOME/.local/share}/supermarket"
COMPOSE_ENV="$DATA/compose.env"
CORE_CONF="$HOME/.config/coredesktop/core.conf"
OLD_STATE="$HOME/.local/state/coremarketing"
URL="http://localhost:8470"
# The version, worked out the way CoreDesktop's `sibling` shows it: the
# latest tag, or else the date and short hash of the latest commit.
version() {
local tag
if tag="$(git -C "$SUPERMARKET_REPO" describe --tags 2> /dev/null)"; then
sed -E 's/-([0-9]+)-g[0-9a-f]+$/+\1/' <<< "$tag"
return
fi
git -C "$SUPERMARKET_REPO" log -1 --date=format:%Y.%m.%d --format='%cd (%h)' 2> /dev/null || echo dev
}
write_compose_env() {
mkdir -p "$DATA" && chmod 700 "$DATA"
{
printf 'SUPERMARKET_DATA=%s\n' "$DATA"
printf 'SUPERMARKET_UID=%s\n' "$(id -u)"
printf 'SUPERMARKET_GID=%s\n' "$(id -g)"
printf 'SUPERMARKET_VERSION=%s\n' "$(version)"
printf 'SUPERMARKET_REPO=%s\n' "$SUPERMARKET_REPO"
printf 'SUPERMARKET_TZ=%s\n' "$(host_timezone)"
} > "$COMPOSE_ENV"
}
# host_timezone prints this computer's time zone (e.g. America/Denver), so
# the app shows and schedules times in it; UTC if it can't tell.
host_timezone() {
local tz=""
if [ -L /etc/localtime ]; then
tz="$(readlink -f /etc/localtime)"
tz="${tz#*/zoneinfo/}"
fi
if [ -z "$tz" ] || [ "$tz" = "$(readlink -f /etc/localtime)" ]; then
tz="$(cat /etc/timezone 2> /dev/null || true)"
fi
echo "${tz:-UTC}"
}
compose() {
docker compose --project-directory "$SUPERMARKET_REPO" -f "$SUPERMARKET_REPO/compose.yaml" \
--env-file "$COMPOSE_ENV" "$@"
}
# The container can't read ~/.config, so it gets a copy of the Core's
# settings. Without a Core, any old copy goes.
copy_core_conf() {
if [ -f "$CORE_CONF" ]; then
install -m 600 "$CORE_CONF" "$DATA/core.conf"
else
rm -f "$DATA/core.conf"
fi
}
# The LLM servers CoreDesktop's `configure ai-servers` added to opencode, for
# the AI settings page. Docker can't resolve .local names, so they become IP
# addresses.
write_ai_servers() {
local conf="$HOME/.config/opencode/opencode.json" servers name url host ip
if [ ! -f "$conf" ] || ! command -v jq > /dev/null; then
rm -f "$DATA/ai-servers.json"
return 0
fi
servers="[]"
while IFS=$'\t' read -r name url; do
[ -n "$url" ] || continue
host="$(sed -E 's#^[a-z]+://([^/:]+).*#\1#' <<< "$url")"
if [[ "$host" == *.local ]]; then
ip="$(getent ahostsv4 "$host" 2> /dev/null | awk 'NR == 1 { print $1 }')"
[ -n "$ip" ] && url="${url/$host/$ip}"
fi
servers="$(jq -c --arg name "$name" --arg url "$url" '. + [{name: $name, base_url: $url}]' <<< "$servers")"
done < <(jq -r '(.provider // {}) | to_entries[]
| select(.value.npm == "@ai-sdk/openai-compatible" and (.value.options.baseURL // "") != "")
| [.value.name // .key, .value.options.baseURL] | @tsv' "$conf" 2> /dev/null)
printf '%s\n' "$servers" > "$DATA/ai-servers.json"
}
# Wait until the app answers, for up to $1 seconds.
wait_until_up() {
local i
for ((i = 0; i < ${1:-60}; i++)); do
curl -sf -o /dev/null "$URL/healthz" && return 0
sleep 1
done
return 1
}
confirm_yes() {
local answer
read -rp "$1 [Y/n] " answer
[[ -z "$answer" || "$answer" =~ ^[Yy] ]]
}