forked from derimagia/sshconfig2iterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
77 lines (58 loc) · 1.72 KB
/
cli.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
'use strict';
process.bin = process.title = 'sshconfig2iterm';
var sshConfig = require('ssh-config');
var config = require('minimist')(process.argv.slice(2));
var fs = require('fs');
var outputLocation = process.env.HOME + '/Library/Application Support/iTerm2/DynamicProfiles/profiles.json';
fs.readFile(process.env.HOME + '/.ssh/config', function (err, data) {
if (err) {
throw err;
}
sshConfig = sshConfig.parse(data.toString());
var output = {
Profiles: [],
};
for (var i = 0, len = sshConfig.length; i < len; i++) {
var section = sshConfig[i];
var host = section.Host;
var comment = null;
var commentIndex = section.Host.indexOf('#');
if (commentIndex !== -1) {
host = section.Host.substr(0, commentIndex);
comment = section.Host.substr(commentIndex + 1).trim();
}
var host = host.trim();
// Skip Wildcard Entries
if (host.indexOf('*') != -1) continue;
// Use the Comment as a name if it exists.
var name = comment ? comment : host;
if (section['#Label']) {
name = section['#Label'];
}
var itermObj = {
'Guid' : host,
'Name' : name,
'ShortCut' : '',
'Custom Command' : 'Yes',
'Command' : 'ssh ' + host,
}
if (section['#Tags']) {
itermObj.Tags = section['#Tags'].split(',').map(function (tag) {
return tag.trim();
});
}
output.Profiles.push(itermObj);
}
var json = JSON.stringify(output, null, ' ');
fs.writeFile(outputLocation, json, function(err) {
if(err) {
throw err;
}
if (config.save) {
console.log("Saved %d entires into your iTerm2 Profile.\n", output.Profiles.length);
} else {
console.log(json);
}
});
});