This repository was archived by the owner on Aug 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshorturl
More file actions
executable file
·57 lines (51 loc) · 1.34 KB
/
shorturl
File metadata and controls
executable file
·57 lines (51 loc) · 1.34 KB
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
#!/usr/bin/env node
try {
var shorturl = require('shorturl');
} catch(e) {
var shorturl = require('./index');
}
var opts = require('optimist')
.usage("Usage: shorturl [options] <longurl>")
.boolean('debug')
.describe('service', "URL shortening service (such as: bit.ly, goo.gl, is.gd)")
.describe('user', "User name for services which require it")
.describe('key', "API key for services which require it")
.describe('debug', "Display troubleshooting information")
.default('service', 'is.gd')
.default('debug', false)
.alias('d', 'debug')
.check(function(argv) {
return ( argv._.length == 1 && /^https?:/.test(argv._[0]) );
})
.argv,
longurl = opts._[0],
params = {};
// Service-specific parameter building
switch ( opts.service ) {
case 'bit.ly':
params.login = opts.user;
params.apiKey = opts.key;
break;
case 'goo.gl':
params.key = opts.key;
break;
}
// DEBUG
if ( opts.debug ) {
console.log('\nargv[]: ' + require('util').inspect(opts));
console.log('\nparams: '+require('util').inspect(params));
}
shorturl(longurl, opts.service, params, function(result) {
// DEBUG
if ( opts.debug ) {
console.log('\nresult: ' + require('util').inspect(arguments));
return;
}
if ( result instanceof Error ) {
console.log('Error: ' + result.message);
process.exit(1);
} else {
console.log(result);
}
});
/* vim: set ft=javascript: */