-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpin-cushion
executable file
·121 lines (97 loc) · 3.28 KB
/
pin-cushion
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
var Pinboard = require('pinboard.js')
, minimist = require('minimist')
, lo = require('lodash')
, YAML = require('js-yaml')
, wrap = require('monowrap')
, Configstore = require('configstore')
, node_debug = require('debug')
, package = require('./package.json')
var config = new Configstore(package.name)
, flags = require('minimist')(process.argv.slice(2), {
'--' : true
, 'alias' : { 'D': 'debug' }
, 'boolean': [ 'debug' ]
})
var args = flags._.concat(flags['--'])
, verb = lo.camelCase(args[0])
, debugging = flags.debug
, noArgs, API, dumpYAML
if (debugging) {
node_debug.enable('pin-cushion')
delete flags.debug
}
debug = node_debug('pin-cushion')
debug('-- verb: %j', verb)
debug('-- args: %j', args)
debug('-- flags: %j', flags)
if (flags.auth != null) {
if (!lo.isString(flags.auth) || !lo.includes(flags.auth, ':')) {
console.error("Usage: %s --auth 'username:DEADBEEF'", require('path').basename(process.argv[1]))
console.error(" (API token obtained at <https://pinboard.in/settings/password>.)")
process.exit(10)
}
var auth = flags.auth.split(':')
config.set('username', auth[0])
config.set('token', auth[1])
console.error('-- Authentication information recorded!')
process.exit(0)
}
function usage(API){
console.error("Usage: %s [verb] [arguments]", require('path').basename(process.argv[1]))
console.error(" %s --auth 'username:DEADBEEF'", require('path').basename(process.argv[1]))
if (null != API) {
var verbs = Object.keys(API.__proto__).map(function(key){
return lo.kebabCase(key).split('-').join('/') })
console.error(wrap(
"Verbs: " + verbs.join(', ')
, { top: 1, width: process.stdout.columns } ))
}
console.error(wrap(
"API token from <https://pinboard.in/settings/password>; more usage examples at " +
"<http://ell.io/tt$pin-cushion#readme>; and full API documentation at " +
"<https://pinboard.in/api/>!"
, { top: 1, width: process.stdout.columns } ))
process.exit(10)
}
if (flags.format == null) {
dumpYAML = true
flags.format = 'json'
}
// --- ---- /!\ ---- ----
try {
API = new Pinboard({ auth: {
type: 'token',
username: config.get('username'),
token: config.get('token') } })
} catch (e) {
if (e.message === "`params.auth.username` is required for authentication. Please provide it.") {
if (args.length < 1) usage()
console.error("-- Error: You must authenticate with `--auth` first!")
process.exit(11)
}
}
if (args.length < 1) usage(API)
if (null == API[verb]) {
console.error("-- Error: '%s' verb is unknown to `pinboard.js`!", verb)
process.exit(12)
}
API[verb](flags, function callback(err, response, body){
debug('Response!')
debug('-- err: %j', err)
debug('-- response: %j', response)
debug('-- body: %s', body)
if (err != null) {
console.error('-- Error: %s', err)
process.exit(9)
}
if (response.statusCode < 200 || response.statusCode > 299) {
console.error('API error: %s', body)
process.exit(Math.floor(response.statusCode / 100))
}
if (dumpYAML)
body = YAML.safeDump(JSON.parse(body), {
lineWidth: (process.stdout.columns || 80) - 2
})
console.log(body)
})