-
Notifications
You must be signed in to change notification settings - Fork 3
/
bax.fish
46 lines (44 loc) · 2.08 KB
/
bax.fish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
set -g bax_version 1.0.0
complete -xc bax -n __fish_use_subcommand -a --help -d "Show usage help"
complete -xc bax -n __fish_use_subcommand -a --version -d "$bax_version"
function bax --description "Run a bash command and replay env changes in fish"
switch "$argv"
case {,-}-h{elp,} ""
echo "usage: bax <bash commands> Run a bash command"
echo " bax --help Show this help"
echo " bax --version Show the current version"
case {,-}-v{ersion,}
echo "bax version $bax_version"
case \*
set -l delim "@@@@bax@@@@"
command bash -c "
$argv
status=\$?
[ \$status -eq 0 ] && echo $delim && alias && echo $delim && command awk '
BEGIN {
for (k in ENVIRON)
if (gsub(/\n/, \"\\\n\", ENVIRON[k]) >= 0)
print k, ENVIRON[k]
}
' || echo $delim \$status
" | command awk -v delim="$delim" '
$0 != delim || !++i {
if ($1 == delim) exit $2
else if (i < 2) print (i ? $0 : gsub(/\$/, "\\\$") >= 0 ? "echo \"" $0 "\"" : "")
else env[$1] = (gsub(/\\\n/, "\n") >= 0 ? substr($0, length($1) + 2) : "")
}
END {
for (k in env)
if (k !~ /^(_|SHLVL|PS1|XPC_SERVICE_NAME|AWK(LIB)?PATH)$|^BASH_FUNC/\
&& !(k in ENVIRON && ENVIRON[k] == env[k]))
print (\
k == "PATH" && gsub(/:/, "\" \"", env[k]) >= 0\
? "set PATH \"" env[k] "\"" : k == "PWD"\
? "cd \"" env[k] "\"" : "set -gx " k " \"" env[k] "\""\
)
if (!length(env)) print "exit " $2
else for (k in ENVIRON) if (!(k in env)) print "set -e " k
}
' | source
end
end