-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't choke if the active app isn't installed #6
- Loading branch information
Showing
7 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.alfredversionchecked | ||
|
||
# Created by https://www.gitignore.io/api/python | ||
|
||
### Python ### | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#!/usr/bin/osascript -l JavaScript | ||
|
||
ObjC.import('stdlib') | ||
|
||
Array.prototype.contains = function(val) { | ||
for (var i; i < this.length; i++) { | ||
if (this[i] === val) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
app = Application('Shimo') | ||
help = `shimo.js <command> [<name>] | ||
Usage: | ||
shimo.js (connect|disconnect) <name> | ||
shimo.js list | ||
shimo.js -h | ||
Options: | ||
-h, --help Show this message and quit | ||
` | ||
|
||
// Return true if specified VPN is connected. | ||
function isActive(name) { | ||
for (var i=0; i < app.connections.length; i++) { | ||
var conn = app.connections[i] | ||
if (conn.name() == name) { | ||
return conn.state() == 'Connected' | ||
} | ||
} | ||
return false | ||
} | ||
|
||
|
||
// Show CLI help. | ||
function showHelp(errMsg) { | ||
var status = 0 | ||
if (errMsg) { | ||
console.log(`error: ${errMsg}`) | ||
status = 1 | ||
} | ||
console.log(help) | ||
$.exit(status) | ||
} | ||
|
||
|
||
// Parse command-line flags. | ||
function parseArgs(argv) { | ||
if (argv.length == 0 || argv.contains('-h') || argv.contains('--help')) { | ||
showHelp() | ||
} | ||
|
||
var command = argv[0], | ||
opts = {command: command, name: ''} | ||
|
||
if (command === 'list') { | ||
return opts | ||
} | ||
|
||
if (command === 'connect' || command === 'disconnect') { | ||
if (argv.length < 2) { | ||
showHelp('no VPN name specified') | ||
} | ||
opts.name = argv[1] | ||
return opts | ||
} | ||
|
||
showHelp(`unknown command: ${command}`) | ||
} | ||
|
||
|
||
// Connect to specified VPN. | ||
function connect(name) { | ||
if (isActive(name)) { | ||
console.log(`VPN ${name} is already connected`) | ||
return | ||
} | ||
app.connect(name) | ||
} | ||
|
||
|
||
// Disconnect specified VPN. | ||
function disconnect(name) { | ||
if (!isActive(name)) { | ||
console.log(`VPN ${name} is not connected`) | ||
return | ||
} | ||
app.disconnect(name) | ||
} | ||
|
||
// Return JSON mapping of connections to connected state. | ||
function list() { | ||
var accounts = {} | ||
|
||
for (var i=0; i < app.accounts().length; i++) { | ||
var acc = app.accounts[i] | ||
accounts[acc.name()] = acc.connected() | ||
} | ||
|
||
return JSON.stringify(connections) | ||
} | ||
|
||
|
||
function run(argv) { | ||
opts = parseArgs(argv) | ||
switch (opts.command) { | ||
case 'connect': | ||
return connect(opts.name) | ||
case 'disconnect': | ||
return disconnect(opts.name) | ||
case 'list': | ||
return list() | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters