From fbec9a17fe6a012ce6f794e5a2d73c6dcec19463 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 18 Oct 2022 23:44:02 +0200 Subject: reaktor2 locations: init --- krebs/2configs/reaktor2.nix | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/krebs/2configs/reaktor2.nix b/krebs/2configs/reaktor2.nix index a7dedf562..2ca84f568 100644 --- a/krebs/2configs/reaktor2.nix +++ b/krebs/2configs/reaktor2.nix @@ -38,6 +38,38 @@ let }; }; + locationsLib = pkgs.writeText "locations.sh" '' + ENDPOINT=http://c.r/poi.json + get_locations() { + curl -fsS "$ENDPOINT" + } + + set_locations() { + curl -fSs --data-binary @- "$ENDPOINT" + } + + set_location() { + [ $# -eq 3 ] || return 1 + get_locations \ + | jq \ + --arg name "$1" \ + --arg latitude "$2" \ + --arg longitude "$3" \ + '.[$name] = { $latitude, $longitude }' \ + | set_locations + } + + get_location() { + [ $# -eq 1 ] || return 1 + get_locations | jq --arg name "$1" '.[$name]' + } + + delete_location() { + [ $# -eq 1 ] || return 1 + get_locations | jq --arg name "$1" 'del(.[$name])' | set_locations + } + ''; + systemPlugin = { plugin = "system"; config = { @@ -75,6 +107,56 @@ let ''; }; } + { + pattern = "^list-locations"; + activate = "match"; + command = { + filename = pkgs.writeDash "list-locations" '' + export PATH=${makeBinPath [ + pkgs.curl + pkgs.jq + ]} + set -efu + set -x + . ${locationsLib} + get_locations | jq -r 'to_entries[]|"\(.key) \(.value.latitude),\(.value.longitude)"' + ''; + }; + } + { + pattern = ''^add-location (\S+) ([0-9.]+),([0-9.]+)$''; + activate = "match"; + arguments = [1 2 3]; + command = { + filename = pkgs.writeDash "add-location" '' + export PATH=${makeBinPath [ + pkgs.curl + pkgs.jq + ]} + set -efu + set -x + . ${locationsLib} + set_location "$1" $2 $3 + ''; + }; + } + { + pattern = ''^delete-location (\S+)$''; + activate = "match"; + arguments = [1]; + command = { + filename = pkgs.writeDash "add-location" '' + export PATH=${makeBinPath [ + pkgs.curl + pkgs.jq + ]} + set -efu + set -x + . ${locationsLib} + delete_location "$1" + ''; + }; + } { pattern = "18@p"; activate = "match"; -- cgit v1.2.3