summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-07-23 00:04:54 +0200
committertv <tv@krebsco.de>2017-07-23 01:35:52 +0200
commit854311b004c947ec825f04df9f5fb8fda777e5dc (patch)
tree31a77bd3b3677058dd358ebd778b705cb3833f78
parenta942b33f04f2dc6a10023f86a3a1775eda9186c9 (diff)
shell: add parse-target command
-rw-r--r--shell.nix40
1 files changed, 19 insertions, 21 deletions
diff --git a/shell.nix b/shell.nix
index 58b956c04..0a8d8dde2 100644
--- a/shell.nix
+++ b/shell.nix
@@ -49,6 +49,24 @@ let
"$1"
'';
+ # usage: parse-target [USER@]HOST[:PORT][/PATH]
+ cmds.parse-target = pkgs.writeDash "cmds.parse-target" ''
+ set -efu
+ script=${pkgs.writeText "cmds.parse-target.jq" ''
+ def when(c; f): if c then f else . end;
+ def capturesDef(i; v): .captures[i].string | when(. == null; v);
+ $target | match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | {
+ user: capturesDef(0; "root"),
+ host: capturesDef(1; env.system),
+ port: capturesDef(2; "22"),
+ path: capturesDef(3; "/var/src"),
+ } | . + {
+ local: (.user == env.LOGNAME and .host == env.HOSTNAME),
+ }
+ ''}
+ exec ${pkgs.jq}/bin/jq -enrf "$script" --arg target "$1" \
+ '';
+
init.args = pkgs.writeText "init.args" /* sh */ ''
args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \
-o s:t:u: \
@@ -74,7 +92,7 @@ let
export target
export user
- export target_object="$(${init.env.parsetarget} $target)"
+ export target_object="$(parse-target "$target")"
export target_user="$(echo $target_object | ${pkgs.jq}/bin/jq -r .user)"
export target_host="$(echo $target_object | ${pkgs.jq}/bin/jq -r .host)"
export target_port="$(echo $target_object | ${pkgs.jq}/bin/jq -r .port)"
@@ -88,26 +106,6 @@ let
fi
fi
'' // {
- parsetarget = pkgs.writeDash "init.env.parsetarget" ''
- set -efu
- exec ${pkgs.jq}/bin/jq \
- -enr \
- --arg target "$1" \
- -f ${init.env.parsetarget.jq}
- '' // {
- jq = pkgs.writeText "init.env.parsetarget.jq" ''
- def when(c; f): if c then f else . end;
- def capturesDef(i; v): .captures[i].string | when(. == null; v);
- $target | match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | {
- user: capturesDef(0; "root"),
- host: capturesDef(1; env.system),
- port: capturesDef(2; "22"),
- path: capturesDef(3; "/var/src"),
- } | . + {
- local: (.user == env.LOGNAME and .host == env.HOSTNAME),
- }
- '';
- };
populate = pkgs.writeDash "init.env.populate" ''
set -efu
_source=$(get-source "$source")