From 2695195386271081c92d80d6c4735e5c921b65d0 Mon Sep 17 00:00:00 2001 From: "Info@data-pro.nu" Date: Mon, 2 Nov 2026 17:30:50 +0100 Subject: [PATCH] Added timekeeping script --- solidtime.sh | 266 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 solidtime.sh diff --git a/solidtime.sh b/solidtime.sh new file mode 100644 index 0000000..e43ae8f --- /dev/null +++ b/solidtime.sh @@ -0,0 +1,266 @@ +#!/usr/bin/env bash +# +# solidtime.sh - Start/stop een Solidtime-timer vanaf de commandline. +# +# Gebruik: +# solidtime.sh [on|off|toggle|status] [opties] +# +# on Start een timer (doet niets als er al een timer loopt) +# off Stop de lopende timer +# toggle Stop als er een timer loopt, anders starten (standaard) +# status Toon de lopende timer +# +# Opties (overschrijven de defaults hieronder): +# -t, --token TOKEN API-token +# -p, --project ID Project-ID +# -o, --org ID Organisatie-ID +# -m, --member ID Member-ID (wordt opgezocht als leeg) +# -k, --task ID Taak-ID (optioneel) +# -d, --desc TEKST Omschrijving +# -b, --billable WAARDE true | false | auto (auto = volg instelling project) +# -u, --url URL API-basis-URL +# -h, --help Deze hulp tonen +# +# Vereist: curl en jq +# Linux (Debian/Ubuntu): sudo apt install curl jq +# Termux (Android): pkg install curl jq + +# ============================================================ +# DEFAULTS - vul hier je eigen gegevens in +# ============================================================ +API_URL="https://app.solidtime.io/api/v1" +TOKEN="" +MEMBER_ID="" +PROJECT_ID="" +TASK_ID="" +DESCRIPTION="" +BILLABLE="auto" +# ============================================================ + +set -o pipefail + +ACTION="toggle" + +die() { + echo "Fout: $*" >&2 + exit 1 +} +info() { echo "$*"; } + +usage() { + sed -n '3,22p' "$0" | sed 's/^# \{0,1\}//' + exit 0 +} + +# ---------- Parameters verwerken ---------- +while [[ $# -gt 0 ]]; do + case "$1" in + -t | --token) + TOKEN="$2" + shift 2 + ;; + -p | --project) + PROJECT_ID="$2" + shift 2 + ;; + -o | --org) + ORG_ID="$2" + shift 2 + ;; + -m | --member) + MEMBER_ID="$2" + shift 2 + ;; + -k | --task) + TASK_ID="$2" + shift 2 + ;; + -d | --desc) + DESCRIPTION="$2" + shift 2 + ;; + -b | --billable) + BILLABLE="$2" + shift 2 + ;; + -u | --url) + API_URL="$2" + shift 2 + ;; + -h | --help) usage ;; + *) + case "$(printf '%s' "$1" | tr 'A-Z' 'a-z')" in + on | start) ACTION="on" ;; + off | stop) ACTION="off" ;; + toggle) ACTION="toggle" ;; + status) ACTION="status" ;; + *) die "Onbekende parameter: $1 (zie --help)" ;; + esac + shift + ;; + esac +done + +command -v curl >/dev/null || die "curl is niet geïnstalleerd." +command -v jq >/dev/null || die "jq is niet geïnstalleerd." +[[ -n "$TOKEN" ]] || die "Geen API-token. Vul TOKEN in bovenin het script of gebruik --token." +API_URL="${API_URL%/}" + +BILLABLE=$(printf '%s' "$BILLABLE" | tr 'A-Z' 'a-z') +case "$BILLABLE" in +true | false | auto) ;; +*) die "--billable moet true, false of auto zijn." ;; +esac + +# ---------- API-helper ---------- +# api METHOD PAD [JSON-BODY] -> zet $HTTP_CODE en $BODY +api() { + local method="$1" path="$2" data="$3" out + local args=(-sS -X "$method" "$API_URL$path" + -H "Authorization: Bearer $TOKEN" + -H "Accept: application/json" + -w $'\n%{http_code}') + [[ -n "$data" ]] && args+=(-H "Content-Type: application/json" -d "$data") + out=$(curl "${args[@]}") || die "Kan $API_URL niet bereiken." + HTTP_CODE="${out##*$'\n'}" + BODY="${out%$'\n'*}" +} + +api_ok() { + [[ "$HTTP_CODE" =~ ^2 ]] && return 0 + local msg + msg=$(jq -r '.message // empty' <<<"$BODY" 2>/dev/null) + die "$1 (HTTP $HTTP_CODE)${msg:+: $msg}" +} + +now_utc() { date -u +%Y-%m-%dT%H:%M:%SZ; } + +# ---------- Lopende timer ophalen ---------- +get_active() { + api GET "/users/me/time-entries/active" + if [[ "$HTTP_CODE" == "404" ]]; then + ACTIVE="" + return + fi + api_ok "Ophalen lopende timer mislukt" + ACTIVE=$(jq -c '.data // empty' <<<"$BODY") +} + +# ---------- Organisatie en member-ID bepalen ---------- +resolve_member() { + [[ -n "$ORG_ID" && -n "$MEMBER_ID" ]] && return + api GET "/users/me/memberships" + api_ok "Ophalen lidmaatschappen mislukt" + + if [[ -z "$ORG_ID" ]]; then + local count + count=$(jq '.data | length' <<<"$BODY") + if [[ "$count" -ne 1 ]]; then + echo "Je bent lid van $count organisaties. Kies er een met --org of vul ORG_ID in:" >&2 + jq -r '.data[] | " \(.organization.id) \(.organization.name)"' <<<"$BODY" >&2 + exit 1 + fi + ORG_ID=$(jq -r '.data[0].organization.id' <<<"$BODY") + fi + + MEMBER_ID=$(jq -r --arg org "$ORG_ID" \ + '.data[] | select(.organization.id == $org) | .id' <<<"$BODY") + [[ -n "$MEMBER_ID" ]] || die "Geen lidmaatschap gevonden voor organisatie $ORG_ID." +} + +# ---------- Projectnaam / billable opzoeken ---------- +project_info() { + PROJECT_NAME="" + PROJECT_BILLABLE="false" + [[ -n "$PROJECT_ID" ]] || return + api GET "/organizations/$ORG_ID/projects/$PROJECT_ID" + api_ok "Project $PROJECT_ID niet gevonden" + PROJECT_NAME=$(jq -r '.data.name' <<<"$BODY") + PROJECT_BILLABLE=$(jq -r '.data.is_billable // false' <<<"$BODY") +} + +describe_active() { + local start desc proj + start=$(jq -r '.start' <<<"$ACTIVE") + desc=$(jq -r '.description // ""' <<<"$ACTIVE") + proj=$(jq -r '.project_id // ""' <<<"$ACTIVE") + local since + since=$(date -d "$start" '+%H:%M' 2>/dev/null || echo "$start") + echo "Timer loopt sinds $since${proj:+ (project $proj)}${desc:+ - \"$desc\"}" +} + +# ---------- Acties ---------- +start_timer() { + resolve_member + project_info + + local billable="$BILLABLE" + [[ "$billable" == "auto" ]] && billable="$PROJECT_BILLABLE" + + local body + body=$(jq -n \ + --arg member "$MEMBER_ID" \ + --arg project "$PROJECT_ID" \ + --arg task "$TASK_ID" \ + --arg desc "$DESCRIPTION" \ + --arg start "$(now_utc)" \ + --argjson billable "$billable" \ + '{member_id: $member, start: $start, end: null, billable: $billable, + project_id: (if $project == "" then null else $project end), + task_id: (if $task == "" then null else $task end), + description:(if $desc == "" then null else $desc end), + tags: []}') + + api POST "/organizations/$ORG_ID/time-entries" "$body" + api_ok "Starten van de timer mislukt" + info "Timer gestart${PROJECT_NAME:+ op \"$PROJECT_NAME\"}${DESCRIPTION:+ - \"$DESCRIPTION\"}." +} + +stop_timer() { + local id org + id=$(jq -r '.id' <<<"$ACTIVE") + org=$(jq -r '.organization_id // empty' <<<"$ACTIVE") + [[ -n "$org" ]] || { + resolve_member + org="$ORG_ID" + } + + api PUT "/organizations/$org/time-entries/$id" "{\"end\": \"$(now_utc)\"}" + api_ok "Stoppen van de timer mislukt" + + local secs + secs=$(jq -r '.data.duration // empty' <<<"$BODY") + if [[ -n "$secs" ]]; then + printf 'Timer gestopt na %du%02dm.\n' $((secs / 3600)) $((secs % 3600 / 60)) + else + info "Timer gestopt." + fi +} + +get_active + +case "$ACTION" in +on) + if [[ -n "$ACTIVE" ]]; then + info "Er loopt al een timer; er is niets gestart." + describe_active + else + start_timer + fi + ;; +off) + if [[ -n "$ACTIVE" ]]; then + stop_timer + else info "Er loopt geen timer."; fi + ;; +toggle) + if [[ -n "$ACTIVE" ]]; then + stop_timer + else start_timer; fi + ;; +status) + if [[ -n "$ACTIVE" ]]; then + describe_active + else info "Er loopt geen timer."; fi + ;; +esac