Skip to content

Commit

Permalink
Added support for SSH options.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-amdroid committed Oct 1, 2023
1 parent 597e55d commit 0d0416a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 30 deletions.
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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**
Expand Down
4 changes: 0 additions & 4 deletions box.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"chmod": "0755",
"files": ["smt.php", "composer.json", "README.md"],
"directories": ["src", "includes"],
"finder": [
Expand All @@ -16,8 +15,5 @@
]
}
],
"main": "smt",
"output": "smt.phar",
"stub": true,
"exclude-composer-files": false
}
11 changes: 7 additions & 4 deletions includes/bootstrap.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -557,15 +560,15 @@ 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'];
}
}
}
// 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'];
}
Expand Down
1 change: 0 additions & 1 deletion includes/nix.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 0 additions & 4 deletions smt.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
$app->setDispatcher($dispatcher);

$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {

global $preferences;
$input = $event->getInput();

Expand All @@ -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
Expand All @@ -126,9 +124,7 @@

// TODO: find proper way to exit without running default command after
// return 0;

}

});

$app->addCommands($commands);
Expand Down
13 changes: 11 additions & 2 deletions src/Command/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<comment>' . $default_title . '</comment>]: ', $default_title);
$title = $helper->ask($input, $output, $title_question);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CdCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/MountCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/Command/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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'];
};
Expand Down

0 comments on commit 0d0416a

Please sign in to comment.