~mclehman/innsmouth-remote

9588646a5c72baebd94032002293c556d34a7a5c — 0xFORDCOMMA 3 years ago master
Initial commit.
4 files changed, 57 insertions(+), 0 deletions(-)

A .gitignore
A META6.json
A lib/Innsmouth/Remote.pm6
A lib/Innsmouth/Remote.rakumod
A  => .gitignore +1 -0
@@ 1,1 @@
.precomp

A  => META6.json +23 -0
@@ 1,23 @@
{
  "authors": [
    "0xford"
  ],
  "license": "https://opensource.org/licenses/GPL-3.0",
  "production": false,
  "description": "",
  "perl": "v6.*",
  "name": "Innsmouth",
  "version": "2020.01.01",
  "tags": [
    "android",
    "termux",
    "api",
    "termux-api"
  ],
  "provides": {
    "Innsmouth::Remote": "lib/Innsmouth/Remote.rakumod"
  },
  "scripts": {
    "test": "zef test ."
  }
}
\ No newline at end of file

A  => lib/Innsmouth/Remote.pm6 +1 -0
@@ 1,1 @@
Remote.rakumod
\ No newline at end of file

A  => lib/Innsmouth/Remote.rakumod +32 -0
@@ 1,32 @@
unit module Innsmouth::Remote;

use JSON::Fast;

our %state-cache;

sub innsmouth-shell($cmd, Bool :$term = False) is export {
    return shell("ssh innsmouth {$term ?? '-t' !! '' } '$cmd'", :out, :err).out.slurp
}

sub innsmouth-api($endpoint, *@args) is export {
    innsmouth-shell "tmux attach -t termux-api-remote \\; set-buffer \"termux-$endpoint {@args.join(' ')} > ~/.state/$endpoint\n\" \\; paste-buffer \\; detach > /dev/null 2>&1 ; sleep 1", :term;
    return innsmouth-shell "cat ~/.state/$endpoint";
}

sub innsmouth-state($endpoint) is export {
    unless     $endpoint (elem) %state-cache
           and %state-cache{$endpoint}<timestamp>.defined
           and now - %state-cache{$endpoint}<timestamp> < 10 {
        my $response = innsmouth-api $endpoint;
        %state-cache{$endpoint}<value timestamp> = from-json($response), now;
    }
    return %state-cache{$endpoint}<value>;
}

sub innsmouth-query(@targets) is export {
    return (innsmouth-state(@targets.head), |@targets[1..*]).reduce: -> %h, $k { %h{$k} }
}

sub innsmouth-notification(:$title, :$message, :$id = 1) is export {
    innsmouth-api 'notification', "-i $id -t \\\"$title\\\" -c \\\"$message\\\"";
}