shell: add --default=TARGET option to parse-target
This commit is contained in:
parent
dc0ca967cb
commit
13d1fe582d
49
shell.nix
49
shell.nix
@ -49,22 +49,39 @@ let
|
|||||||
"$1"
|
"$1"
|
||||||
'';
|
'';
|
||||||
|
|
||||||
# usage: parse-target [USER@]HOST[:PORT][/PATH]
|
# usage: parse-target [--default=TARGET] TARGET
|
||||||
|
# TARGET = [USER@]HOST[:PORT][/PATH]
|
||||||
cmds.parse-target = pkgs.writeDash "cmds.parse-target" ''
|
cmds.parse-target = pkgs.writeDash "cmds.parse-target" ''
|
||||||
set -efu
|
set -efu
|
||||||
script=${pkgs.writeText "cmds.parse-target.jq" ''
|
args=$(${pkgs.utillinux}/bin/getopt -n "$0" -s sh \
|
||||||
def when(c; f): if c then f else . end;
|
-o d: \
|
||||||
def capturesDef(i; v): .captures[i].string | when(. == null; v);
|
-l default: \
|
||||||
$target | match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | {
|
-- "$@")
|
||||||
user: capturesDef(0; "root"),
|
if \test $? != 0; then exit 1; fi
|
||||||
host: capturesDef(1; env.system),
|
eval set -- "$args"
|
||||||
port: capturesDef(2; "22"),
|
default_target=
|
||||||
path: capturesDef(3; "/var/src"),
|
while :; do case $1 in
|
||||||
} | . + {
|
-d|--default) default_target=$2; shift 2;;
|
||||||
local: (.user == env.LOGNAME and .host == env.HOSTNAME),
|
--) shift; break;;
|
||||||
}
|
esac; done
|
||||||
''}
|
target=$1; shift
|
||||||
exec ${pkgs.jq}/bin/jq -enrf "$script" --arg target "$1" \
|
for arg; do echo "$0: bad argument: $arg" >&2; done
|
||||||
|
if \test $# != 0; then exit 2; fi
|
||||||
|
exec ${pkgs.jq}/bin/jq \
|
||||||
|
-enr \
|
||||||
|
--arg default_target "$default_target" \
|
||||||
|
--arg target "$target" \
|
||||||
|
-f ${pkgs.writeText "cmds.parse-target.jq" ''
|
||||||
|
def parse: match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | {
|
||||||
|
user: .captures[0].string,
|
||||||
|
host: .captures[1].string,
|
||||||
|
port: .captures[2].string,
|
||||||
|
path: .captures[3].string,
|
||||||
|
};
|
||||||
|
def sanitize: with_entries(select(.value != null));
|
||||||
|
($default_target | parse) + ($target | parse | sanitize) |
|
||||||
|
. + { local: (.user == env.LOGNAME and .host == env.HOSTNAME) }
|
||||||
|
''}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
# usage: quote [ARGS...]
|
# usage: quote [ARGS...]
|
||||||
@ -104,7 +121,9 @@ let
|
|||||||
export target
|
export target
|
||||||
export user
|
export user
|
||||||
|
|
||||||
export target_object="$(parse-target "$target")"
|
default_target=root@$system:22/var/src
|
||||||
|
|
||||||
|
export target_object="$(parse-target "$target" -d "$default_target")"
|
||||||
export target_user="$(echo $target_object | ${pkgs.jq}/bin/jq -r .user)"
|
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_host="$(echo $target_object | ${pkgs.jq}/bin/jq -r .host)"
|
||||||
export target_port="$(echo $target_object | ${pkgs.jq}/bin/jq -r .port)"
|
export target_port="$(echo $target_object | ${pkgs.jq}/bin/jq -r .port)"
|
||||||
|
Loading…
Reference in New Issue
Block a user