diff --git a/README.md b/README.md index 825494d..96db9d8 100644 --- a/README.md +++ b/README.md @@ -62,21 +62,24 @@ Usage Connection properties: -| Property | Description | -| ---------- | --------------------- | -| `id` | Connection ID | -| `title` | Title | -| `server` | Server | -| `port` | Port | -| `user` | Username | -| `password` | Password | -| `key` | Path to key file | -| `mount` | Mount directory | -| `remote` | Remote directory | -| `options` | List of SSHFS options | +| Property | Description | +|---------------|-----------------------| +| `id` | Connection ID | +| `title` | Title | +| `server` | Server | +| `port` | Port | +| `user` | Username | +| `password` | Password | +| `key` | Path to key file | +| `mount` | Mount directory | +| `remote` | Remote directory | +| `options` | List of SSHFS options | +| `ssh_options` | List of SSH options | Connections can be stored in YAML file in `~/.config/smt/stm.yml` (global) or in `stm.yml` in current directory. +`ssh_options` are not prompted during `add` command, so they need to be added to config file manually. + Example of config file: ~~~language-yaml @@ -90,7 +93,12 @@ connections: key: ~/.ssh/id_rsa mount: ~/mnt/msrv remote: /var/www - options: { } + options: + - some_option + - SomeAnotherOption=yes + ssh_options: + - '-o SomeOption=100' + - '-f SomeFlag 200' ~~~ **Mount connection** diff --git a/box.json b/box.json index 27d816c..8c7a436 100644 --- a/box.json +++ b/box.json @@ -1,5 +1,4 @@ { - "chmod": "0755", "files": ["smt.php", "composer.json", "README.md"], "directories": ["src", "includes"], "finder": [ @@ -16,8 +15,5 @@ ] } ], - "main": "smt", - "output": "smt.phar", - "stub": true, "exclude-composer-files": false } diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 19cd8c3..e906c31 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -31,6 +31,9 @@ $default_preferences['default_options'] = [ 'follow_symlinks', 'StrictHostKeyChecking=no', ]; +$default_preferences['default_ssh_options'] = [ + '-o ServerAliveInterval=60', +]; $env_preferences['os_functions_inc'] = init(); require $env_preferences['os_functions_inc']; @@ -452,7 +455,7 @@ function gen_connection_settings_table($cid, $connection_settings, $output) { ]); foreach ($connection_settings as $key => $value) { - if ($key == 'options') { + if (in_array($key, ['options', 'ssh_options'])) { $table->addRow([ $key, implode(',', $value), @@ -497,7 +500,7 @@ function get_cid($mount_point) { if ($mount_point == $connection_settings['mount']) { return $cid; } - elseif (substr($connection_settings['mount'], 0, 1) == '~') { + elseif (str_starts_with($connection_settings['mount'], '~')) { $absolute_path = $preferences['home_path'] . substr($connection_settings['mount'], 1); if ($mount_point == $absolute_path) { return $cid; @@ -557,7 +560,7 @@ function validate_answer_as_connection($answer, $connections_data) { // Answer looks like a number. if (filter_var($answer, FILTER_VALIDATE_INT)) { - foreach ($connections_data as $connection => $connection_data) { + foreach ($connections_data as $connection_data) { if ($answer == $connection_data['n']) { $cid = $connection_data['cid']; } @@ -565,7 +568,7 @@ function validate_answer_as_connection($answer, $connections_data) { } // Answer looks like a cid. else { - foreach ($connections_data as $connection => $connection_data) { + foreach ($connections_data as $connection_data) { if ($answer == $connection_data['cid']) { $cid = $connection_data['cid']; } diff --git a/includes/nix.inc b/includes/nix.inc index 56a940a..01c4289 100644 --- a/includes/nix.inc +++ b/includes/nix.inc @@ -2,7 +2,6 @@ $default_preferences['unmount_cmd'] = 'fusermount -u'; $default_preferences['mounts_list_type'] = 'fuse.sshfs'; -// $default_preferences['editor'] = 'editor'; $default_preferences['editor'] = '$EDITOR'; $default_preferences['terminal'] = 'gnome-terminal'; diff --git a/smt.php b/smt.php index a1e0f4b..604fbc4 100644 --- a/smt.php +++ b/smt.php @@ -88,7 +88,6 @@ $app->setDispatcher($dispatcher); $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) { - global $preferences; $input = $event->getInput(); @@ -107,7 +106,6 @@ // TODO: find proper way to exit without running default command after // return 0; - } // Seems like something broken after overriding help command @@ -126,9 +124,7 @@ // TODO: find proper way to exit without running default command after // return 0; - } - }); $app->addCommands($commands); diff --git a/src/Command/AddCommand.php b/src/Command/AddCommand.php index c233623..3a24252 100644 --- a/src/Command/AddCommand.php +++ b/src/Command/AddCommand.php @@ -91,14 +91,23 @@ protected function execute(InputInterface $input, OutputInterface $output) { $remote = new Question('Remote directory []: '); $connection_settings['remote'] = $helper->ask($input, $output, $remote); - // Options. + // Mount Options. $prompt_options = ($output->isVerbose()) ? 'Mount options, separated by comma []: ' : 'Mount options []: '; $options_question = new Question($prompt_options); $options = $helper->ask($input, $output, $options_question); - $options = explode(',', $options ? $options : ''); + $options = explode(',', $options ?: ''); $options = array_map('trim', $options); $connection_settings['options'] = array_filter($options); + // TODO: Consider prompting for SSH options. + // SSH Options. + // $prompt_ssh_options = ($output->isVerbose()) ? 'SSH options, separated by comma []: ' : 'SSH options []: '; + // $ssh_options_question = new Question($prompt_ssh_options); + // $ssh_options = $helper->ask($input, $output, $ssh_options_question); + // $ssh_options = explode(',', $ssh_options ?: ''); + // $ssh_options = array_map('trim', $ssh_options); + // $connection_settings['ssh_options'] = array_filter($ssh_options); + // Title. $title_question = new Question('Connection title [' . $default_title . ']: ', $default_title); $title = $helper->ask($input, $output, $title_question); diff --git a/src/Command/CdCommand.php b/src/Command/CdCommand.php index 0d3c474..79fa729 100644 --- a/src/Command/CdCommand.php +++ b/src/Command/CdCommand.php @@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $connection_settings = get_connection_settings($cid); if (isset($connection_settings['mount'])) { - if (substr($connection_settings['mount'], 0, 1) == '~') { + if (str_starts_with($connection_settings['mount'], '~')) { $path = $preferences['home_path'] . substr($connection_settings['mount'], 1); } else { diff --git a/src/Command/MountCommand.php b/src/Command/MountCommand.php index 31b67da..7b0780e 100644 --- a/src/Command/MountCommand.php +++ b/src/Command/MountCommand.php @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $connection_settings = get_connection_settings($cid); // Check existing of mount point and create if needed. - if (substr($connection_settings['mount'], 0, 1) == '~') { + if (str_starts_with($connection_settings['mount'], '~')) { $mount_dir = $preferences['home_path'] . substr($connection_settings['mount'], 1); } else { diff --git a/src/Command/SshCommand.php b/src/Command/SshCommand.php index e7db3d8..1f04aa9 100644 --- a/src/Command/SshCommand.php +++ b/src/Command/SshCommand.php @@ -19,6 +19,8 @@ protected function configure() { } protected function execute(InputInterface $input, OutputInterface $output) { + global $preferences; + $helper = $this->getHelper('question'); $cid = cid_resolver($input, $output, $helper); @@ -48,11 +50,20 @@ protected function execute(InputInterface $input, OutputInterface $output) { } $cmd .= 'ssh '; + // SSH options. + $ssh_options = $preferences['default_ssh_options']; + if (isset($connection_settings['ssh_options'])) { + $ssh_options = array_merge($ssh_options, $connection_settings['ssh_options']); + } + $cmd .= implode(' ', $ssh_options) . ' '; + + // User and server. if (isset($connection_settings['user'])) { $cmd .= $connection_settings['user'] . '@'; } $cmd .= $connection_settings['server']; + // Port. if ($connection_settings['port']) { $cmd .= ' -p ' . $connection_settings['port']; };