This commit is contained in:
anki-code 2020-03-13 23:51:30 +03:00
parent d65516fb8b
commit 41a895bbaa
3 changed files with 39 additions and 36 deletions

View file

@ -1,7 +1,7 @@
import sys, os
global_settings = {
'XXH_VERSION': '0.5.5'
'XXH_VERSION': '0.5.6'
}
if __name__ == "__main__":
@ -17,4 +17,4 @@ if __name__ == "__main__":
else:
sys.exit(1)
else:
print(global_settings)
print(global_settings)

36
xxh
View file

@ -346,7 +346,6 @@ class Xxh:
def main(self):
self.create_xxh_env()
argp = argparse.ArgumentParser(description=f"Your favorite shell wherever you go through the ssh.\n{self.d2F0Y2ggLW4uMiB4eGggLWg()}", formatter_class=RawTextHelpFormatter, prefix_chars='-+')
argp.add_argument('--version', '-V', action='version', version=f"xonssh-xxh/{self.local_xxh_version}")
argp.add_argument('-p', dest='ssh_port', help="Port to connect to on the remote host.")
@ -360,14 +359,14 @@ class Xxh:
argp.add_argument('+if','++install-force', default=False, action='store_true', help="Removing the host xxh package and install xxh again.")
argp.add_argument('+iff','++install-force-full', default=False, action='store_true', help="Removing the host xxh home and install xxh again. All installed packages on the host (e.g. pip packages) will be lost.")
argp.add_argument('+xc','++xxh-config', default=self.config_file, help=f"Xxh config file in yaml. Default: " + self.config_file)
argp.add_argument('+e','++env', dest='env', metavar='NAME=VAL ...', action='append', help="Setting environment variables")
argp.add_argument('+eb','++envb', dest='envb', metavar='NAME=BASE64 ...', action='append', help="Setting environment variables base64 encoded")
argp.add_argument('+E','++env-mode', action='store_true', help="Environment mode in case `source xxh.sh`")
argp.add_argument('+e','++env', dest='env', metavar='NAME=VAL ...', action='append', help="Setting environment variables if supported by shell entrypoint.")
argp.add_argument('+eb','++envb', dest='envb', metavar='NAME=BASE64 ...', action='append', help="Setting environment variables base64 encoded if supported by shell entrypoint.")
argp.add_argument('+lh','++local-xxh-home', default=self.local_xxh_home, help=f"Local xxh home path. Default: {self.local_xxh_home}")
argp.add_argument('+hh','++host-xxh-home', default=self.host_xxh_home, help=f"Host xxh home path. Default: {self.host_xxh_home}")
argp.add_argument('+hhr','++host-xxh-home-remove', action='store_true', help=f"Remove xxh home on host after disconnect")
argp.add_argument('+hf','++host-execute-file', help=f"Execute script file placed on host and exit.")
argp.add_argument('+hc','++host-execute-command', help=f"Execute command on host and exit.")
argp.add_argument('+hhr','++host-xxh-home-remove', action='store_true', help=f"Remove xxh home on host after disconnect.")
argp.add_argument('+hf','++host-execute-file', help=f"Execute script file placed on host and exit. If supported by shell entrypoint.")
argp.add_argument('+hc','++host-execute-command', help=f"Execute command on host and exit. If supported by shell entrypoint.")
argp.add_argument('+heb','++host-execute-bash', dest='host_execute_bash', metavar='BASE64', action='append', help="Bash command will be executed before shell entrypoint (base64 encoded) if supported by shell entrypoint.")
argp.add_argument('+s','++shell', default=self.shell, help="Xxh shell: " + self.shells()['available_help'])
argp.add_argument('+ss','++shell-source', default=self.shell_source, help=f"(future) Custom source of xxh-shell: git url or local path")
argp.add_argument('+v','++verbose', default=False, action='store_true', help="Verbose mode.")
@ -477,6 +476,9 @@ class Xxh:
env_args = self.prepare_env_args(opt.env)
env_args += self.prepare_env_args(opt.envb, to_base64=False)
for heb in opt.host_execute_bash:
env_args += ['-b', heb]
opt.install = True if opt.install_force or opt.install_force_full else opt.install
self.local_xxh_home = pf"{opt.local_xxh_home}"
@ -651,24 +653,6 @@ class Xxh:
elif self.verbose:
host_entrypoint_verbose = ['-v', '1']
if opt.env_mode:
# TODO: This PoC code should be in `xxh.zsh`. Every shell should has own `xxh.shell`
# which should be "sourced". While "sourcing" the script should prepare
# and pass `-e` arguments to xxh. See how it works in `xxh.xsh`.
xxh_env = self.get_xxh_env()
if xxh_env:
host_xxh_plugins_dir = host_xxh_home / 'xxh/plugins'
for local_plugin_dir in local_plugins_dir.glob(f'*-{short_shell_name}-*'):
local_plugin_env = local_plugin_dir / 'env'
if local_plugin_env.exists():
with open(local_plugin_env) as f:
plugin_envs = f.read().split('\n')
for e in plugin_envs:
if e in xxh_env:
if self.vverbose:
self.eprint(f'Plugin {local_plugin_dir.name} environment: {e}={xxh_env[e]}')
env_args += ['-e', "%s=%s" % ( e, self.b64e(xxh_env[e]) ) ]
@(self.sshpass) ssh @(self.ssh_arg_v) @(self.ssh_arguments) @(host) -t bash @(str(host_xxh_home/'xxh/shells'/self.shell/'build/entrypoint.sh')) @(host_execute_file) @(host_execute_command) @(host_entrypoint_verbose) @(env_args)
if opt.host_xxh_home_remove:
@ -693,4 +677,4 @@ if __name__ == '__main__':
from xonssh_xxh.settings import global_settings
xxh = Xxh(package_dir_path=pf"{xonssh_xxh.__file__}".parent, version=global_settings['XXH_VERSION'])
xxh.main()
xxh.main()

35
xxh.zsh
View file

@ -4,11 +4,30 @@
#
# Usage in zsh: source xxh.zsh [ordinary xxh arguments]
#
d=`declare -p 2>/dev/null`
if [ ! $d ]; then
echo "\nThis entrypoint is to allow xxh getting current environment variables"
echo "and pass some of them to xxh session to seamless transition to host.\n"
echo "Usage in zsh: source xxh.zsh [ordinary xxh arguments]\n"
else
XXH_SH_ENV=$d xxh +E +s xxh-shell-zsh "$@"
fi
local_xxh_home=~/.xxh
eargs=""
for pluginenv_file in $local_xxh_home/xxh/plugins/*-zsh-*/env; do
if [[ -f $pluginenv_file ]]; then
plugin_name=$(basename `dirname $pluginenv_file` | tr a-z A-Z | sed 's/-/_/g')
if [[ $XXH_VERBOSE == '1' || $XXH_VERBOSE == '2' ]]; then
echo Load plugin env $pluginenv_file
fi
for l in `cat $pluginenv_file`
do
d=`declare -p $l | base64 --wrap=0`
dd="export $plugin_name"_EXE_"$l=$d"
ddd=`echo $dd | base64 --wrap=0`
if [[ $XXH_VERBOSE == '2' ]]; then
echo Prepare plugin env $pluginenv_file: name=$l, declare=$d
echo Prepare plugin env $pluginenv_file bash: $dd
fi
eargs="$eargs +heb $ddd"
done
fi
done
./xxh "$@" +s xxh-shell-zsh ${(z)eargs}