Skip to content
Permalink
36be704eb7
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 121 lines (97 sloc) 3.28 KB
#!/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)
})