Skip to content

Commit

Permalink
Implemented .ssh/config indexing
Browse files Browse the repository at this point in the history
Unfortunately I had to make iTerm2 a hard dependency. Please check the README.md for more info on that. If you know another way to implement this without depending on one specific terminal, feel free to PR!
  • Loading branch information
mrusme committed Mar 1, 2019
1 parent 5b7b0bd commit 57694af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ SSH (LaunchBar Action)

This **SSH** action allows you quickly connect to previously used ssh connections. Therefor it reads your `.zsh_history` and your `.bash_history` and gives you a list of all available ssh connections. Fuzzy search on that list is available as well.

## Requirements

Because it seems to be impossible to teach MacOS's `ssh://` URL functionality to open hosts defined within `.ssh/config`, this action unfortunately depends on [iTerm2](https://www.iterm2.com) and its integration into LaunchBar. Make sure you have iTerm2 installed and the LaunchBar Action `Run iTerm Command` is available to you. You can check that by invoking LaunchBar and type `Run`.

## Installation

Clone/download this repository's master and double-click *SSH.lnbaction*. LaunchBar will then prompt you, whether you want to install it.
Expand Down
8 changes: 6 additions & 2 deletions SSH.lbaction/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
<key>CFBundleName</key>
<string>SSH</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<string>1.1</string>
<key>LBUpdateURL</key>
<string>https://raw.githubusercontent.com/twostairs/lbaction-ssh/master/SSH.lbaction/Contents/Info.plist</string>
<key>LBDownloadURL</key>
<string></string>
<key>LBDescription</key>
<dict>
<key>LBAuthor</key>
Expand All @@ -23,7 +27,7 @@
<key>LBTwitter</key>
<string>@mrusme</string>
<key>LBWebsiteURL</key>
<string>https://twostairs.co</string>
<string>https://github.com/twostairs/lbaction-ssh</string>
</dict>
<key>LBRequirements</key>
<string></string>
Expand Down
Binary file added SSH.lbaction/Contents/Resources/ssh-icon-fav.icns
Binary file not shown.
37 changes: 29 additions & 8 deletions SSH.lbaction/Contents/Scripts/default.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// LaunchBar Action Script
var SUGGESTIONS = [];
var UPDATED_AT = Date.now();
var USER_HOME_URL = 'file:///Users/' + encodeURIComponent(LaunchBar.userName);

function run()
{
}

function buildSuggestion(title, subtitle, command) {
function buildSuggestion(title, subtitle, command, icon) {
return {
'title' : title,
'subtitle': subtitle,
'icon' : 'ssh-icon.icns',
'icon' : icon,
'action': 'ssh',
'actionArgument': command,
'actionRunsInBackground': false,
Expand Down Expand Up @@ -42,17 +43,16 @@ function fuzzysearch (needle, haystack) {
}

function sshFromShellHistories() {
var userHomeUrl = 'file:///Users/' + encodeURIComponent(LaunchBar.userName);
var histories = [];

try {
histories.push(File.readText(File.pathForFileURL(userHomeUrl + '/.bash_history'), 'utf-8'));
histories.push(File.readText(File.pathForFileURL(USER_HOME_URL + '/.bash_history'), 'utf-8'));
} catch(exception) {
LaunchBar.log('Bash history not available: ' + exception);
}

try {
histories.push(File.readText(File.pathForFileURL(userHomeUrl + '/.zsh_history'), 'ascii'));
histories.push(File.readText(File.pathForFileURL(USER_HOME_URL + '/.zsh_history'), 'ascii'));
} catch(exception) {
LaunchBar.log('ZSH history not available: ' + exception);
}
Expand All @@ -65,14 +65,35 @@ function sshFromShellHistories() {

while((regexMatch = regex.exec(history)) !== null) {
var command = regexMatch[1];
commands.push(buildSuggestion(command, command, command));
commands.push(buildSuggestion(command, command, command, 'ssh-icon.icns'));
}

return commands.reverse();
}

function sshFromConfig() {
var config = "";

try {
config = File.readText(File.pathForFileURL(USER_HOME_URL + '/.ssh/config'), 'utf-8');
} catch(exception) {
LaunchBar.log('.ssh/config not available: ' + exception);
}

var regex = RegExp(/Host (.*)\n/gm);
var regexMatch = [];
var commands = [];

while((regexMatch = regex.exec(config)) !== null) {
var command = regexMatch[1];
commands.push(buildSuggestion(command, command, command, 'ssh-icon-fav.icns'));
}

return commands;
}

function updateIndex() {
SUGGESTIONS = SUGGESTIONS.concat(sshFromShellHistories());
SUGGESTIONS = [].concat(sshFromConfig(), sshFromShellHistories());
}

function runWithString(argument)
Expand All @@ -85,7 +106,7 @@ function runWithString(argument)
}

function ssh(argument) {
LaunchBar.openURL('ssh://' + argument);
LaunchBar.performAction('Run iTerm Command', 'ssh ' + argument);
}

updateIndex();

0 comments on commit 57694af

Please sign in to comment.