pricedb/update-rates

42 lines
1.3 KiB
Text
Raw Permalink Normal View History

2020-12-22 21:53:32 +01:00
#!/bin/bash
# Copyright 2020 Midgard
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Configure me
SSHARGS="myserver.example.org"
SYMBOLS="USD GBP"
SERVERSIDE_PATH_TO_RATES="/usr/local/lib/pricedb/rates"
set -euo pipefail
pricedb="$HOME/ledger/pricedb.ledger"
cd "$(dirname "$pricedb")"
last_date="$(tail -n1 "$pricedb" | sed 's/P \([0-9 :-]*\) .*/\1/')"
printf 'Fetching all exchange rates after %s\n' "$last_date"
values="$(ssh $SSHARGS "$SERVERSIDE_PATH_TO_RATES '$last_date' $SYMBOLS")"
2020-12-23 23:39:04 +01:00
if [ $? -eq 0 ] && [ -n "$values" ]; then
2020-12-22 21:53:32 +01:00
printf '%s\n' "$values" >> "$pricedb"
printf '%s\n' "$values"
fi
git diff --quiet "$pricedb" && exit
git diff --cached --quiet || { echo "Not adding to Git because there are staged changes"; exit; }
git add "$pricedb"
git commit -m "Update pricedb"