diff --git a/Makefile b/Makefile index 325368e..d5a346f 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ DOC_FILES = client.md \ persistent_search.md \ server.md -JS_FILES := $(shell find lib test -name '*.js') +JS_FILES := $(shell find lib test -name '*.js') $(shell find bin -name 'ldapjs-*') JSL_CONF_NODE = tools/jsl.node.conf JSL_FILES_NODE = $(JS_FILES) JSSTYLE_FILES = $(JS_FILES) diff --git a/bin/ldapjs-add b/bin/ldapjs-add index 2560052..14ce5b2 100755 --- a/bin/ldapjs-add +++ b/bin/ldapjs-add @@ -4,9 +4,8 @@ var fs = require('fs'); var path = require('path'); -var url = require('url'); -var nopt = require('nopt'); +var dashdash = require('dashdash'); var ldap = require('../lib/index'); var Logger = require('bunyan'); @@ -14,51 +13,74 @@ var Logger = require('bunyan'); ///--- Globals -nopt.typeDefs.DN = { - type: ldap.DN, - validate: function(data, k, val) { - data[k] = ldap.parseDN(val); +dashdash.addOptionType({ + name: 'ldap.DN', + takesArg: true, + helpArg: 'LDAP_DN', + parseArg: function (option, optstr, arg) { + return ldap.parseDN(arg); } -}; +}); -var opts = { - 'debug': Number, - 'binddn': ldap.DN, - 'file': String, - 'insecure': Boolean, - 'password': String, - 'url': url -}; -var shortOpts = { - 'd': ['--debug'], - 'D': ['--binddn'], - 'f': ['--file'], - 'i': ['--insecure'], - 'w': ['--password'], - 'u': ['--url'] -}; +var opts = [ + { + names: ['url', 'u'], + type: 'string', + help: 'LDAP server URL', + helpArg: 'URL', + default: 'ldap://127.0.0.1:389' + }, + { + names: ['binddn', 'D'], + type: 'ldap.DN', + help: 'Bind DN', + default: '' + }, + { + names: ['password', 'w'], + type: 'string', + help: 'Bind password', + helpArg: 'PASSWD', + default: '' + }, + { + names: ['file', 'f'], + type: 'string', + help: 'Input file', + helpArg: 'FILE' + }, + { + names: ['insecure', 'i'], + type: 'bool', + env: 'LDAPJS_TLS_INSECURE', + help: 'Disable SSL certificate verification', + default: false + }, + { + names: ['debug', 'd'], + type: 'integer', + help: 'Set debug level <0-2>', + helpArg: 'LEVEL' + }, + { + names: ['help', 'h'], + type: 'bool', + help: 'Print this help and exit.' + } +]; +var parser = dashdash.createParser({options: opts}); ///--- Helpers function usage(code, message) { - var _opts = ''; - Object.keys(shortOpts).forEach(function(k) { - if (!Array.isArray(shortOpts[k])) - return; - var longOpt = shortOpts[k][0].replace('--', ''); - var type = opts[longOpt].name || 'string'; - if (type && type === 'boolean') type = ''; - type = type.toLowerCase(); - - _opts += ' [--' + longOpt + ' ' + type + ']'; - }); - _opts += ' [JSON]'; + var _opts = parser.help(); var msg = (message ? message + '\n' : '') + - 'usage: ' + path.basename(process.argv[1]) + _opts; + 'usage: ' + path.basename(process.argv[1]) + + ' [JSON]\n' + _opts; process.stderr.write(msg + '\n'); process.exit(code); @@ -82,7 +104,7 @@ var logLevel = 'info'; var parsed; try { - parsed = nopt(opts, shortOpts, process.argv, 2); + parsed = parser.parse(process.argv); if (parsed.file) { parsed.file = JSON.parse(fs.readFileSync(parsed.file, 'utf8')); if (!Array.isArray(parsed.file)) @@ -92,29 +114,22 @@ try { usage(1, e.toString()); } -if (parsed.insecure === undefined && - process.env.LDAPJS_TLS_INSECURE !== undefined) { - if (process.env.LDAPJS_TLS_INSECURE === '0') { - parsed.insecure = false; - } else { - parsed.insecure = true; - } -} - if (parsed.help) usage(0); if (!parsed.file) { parsed.file = []; - parsed.argv.remain.forEach(function(a) { - var o =JSON.parse(a); + parsed._args.forEach(function (a) { + var o = JSON.parse(a); if (Array.isArray(o)) { - o.forEach(function(i) { + o.forEach(function (i) { parsed.file.push(o); }); return; } parsed.file.push(o); }); + if (parsed.file.length === 0) + parsed.file = null; } if (!parsed.file) @@ -122,12 +137,6 @@ if (!parsed.file) if (parsed.debug) logLevel = (parsed.debug > 1 ? 'trace' : 'debug'); -if (!parsed.url) - parsed.url = 'ldap://127.0.0.1:389'; -if (!parsed.binddn) - parsed.binddn = ''; -if (!parsed.password) - parsed.password = ''; var log = new Logger({ name: 'ldapjs', @@ -144,11 +153,11 @@ var client = ldap.createClient({ } }); -client.on('error', function(err) { +client.on('error', function (err) { perror(err); }); -client.bind(parsed.binddn, parsed.password, function(err, res) { +client.bind(parsed.binddn, parsed.password, function (err, res) { if (err) perror(err); @@ -161,7 +170,7 @@ client.bind(parsed.binddn, parsed.password, function(err, res) { client.unbind(function () { return; }); } - parsed.file.forEach(function(entry) { + parsed.file.forEach(function (entry) { var dn = entry.dn; delete entry.dn; client.add(dn, entry, callback); diff --git a/bin/ldapjs-compare b/bin/ldapjs-compare index 570c341..3eaeb69 100755 --- a/bin/ldapjs-compare +++ b/bin/ldapjs-compare @@ -4,9 +4,8 @@ var fs = require('fs'); var path = require('path'); -var url = require('url'); -var nopt = require('nopt'); +var dashdash = require('dashdash'); var ldap = require('../lib/index'); var Logger = require('bunyan'); @@ -14,55 +13,79 @@ var Logger = require('bunyan'); ///--- Globals -nopt.typeDefs.DN = { - type: ldap.DN, - validate: function(data, k, val) { - data[k] = ldap.parseDN(val); +dashdash.addOptionType({ + name: 'ldap.DN', + takesArg: true, + helpArg: 'LDAP_DN', + parseArg: function (option, optstr, arg) { + return ldap.parseDN(arg); } -}; +}); -var opts = { - 'attribute': String, - 'debug': Number, - 'binddn': ldap.DN, - 'file': String, - 'insecure': Boolean, - 'password': String, - 'url': url, - 'value': String, -}; -var shortOpts = { - 'a': ['--attribute'], - 'd': ['--debug'], - 'D': ['--binddn'], - 'f': ['--file'], - 'i': ['--insecure'], - 'w': ['--password'], - 'u': ['--url'], - 'v': ['--value'] -}; +var opts = [ + { + names: ['url', 'u'], + type: 'string', + help: 'LDAP server URL', + helpArg: 'URL', + default: 'ldap://127.0.0.1:389' + }, + { + names: ['binddn', 'D'], + type: 'ldap.DN', + help: 'Bind DN', + default: '' + }, + { + names: ['password', 'w'], + type: 'string', + help: 'Bind password', + helpArg: 'PASSWD', + default: '' + }, + { + names: ['attribute', 'a'], + type: 'string', + help: 'Comparison attribute', + helpArg: 'ATTR' + }, + { + names: ['value', 'v'], + type: 'string', + help: 'Comparison value', + helpArg: 'VAL' + }, + { + names: ['insecure', 'i'], + type: 'bool', + env: 'LDAPJS_TLS_INSECURE', + help: 'Disable SSL certificate verification', + default: false + }, + { + names: ['debug', 'd'], + type: 'integer', + help: 'Set debug level <0-2>', + helpArg: 'LEVEL' + }, + { + names: ['help', 'h'], + type: 'bool', + help: 'Print this help and exit.' + } +]; +var parser = dashdash.createParser({options: opts}); ///--- Helpers function usage(code, message) { - var _opts = ''; - Object.keys(shortOpts).forEach(function(k) { - if (!Array.isArray(shortOpts[k])) - return; - var longOpt = shortOpts[k][0].replace('--', ''); - var type = opts[longOpt].name || 'string'; - if (type && type === 'boolean') type = ''; - type = type.toLowerCase(); - - _opts += ' [--' + longOpt + ' ' + type + ']'; - }); - _opts += ' DN'; + var _opts = parser.help(); var msg = (message ? message + '\n' : '') + - 'usage: ' + path.basename(process.argv[1]) + _opts; + 'usage: ' + path.basename(process.argv[1]) + ' \n' + _opts; process.stderr.write(msg + '\n'); process.exit(code); @@ -86,44 +109,29 @@ var logLevel = 'info'; var parsed; try { - parsed = nopt(opts, shortOpts, process.argv, 2); + parsed = parser.parse(process.argv); } catch (e) { usage(1, e.toString()); } -if (parsed.insecure === undefined && - process.env.LDAPJS_TLS_INSECURE !== undefined) { - if (process.env.LDAPJS_TLS_INSECURE === '0') { - parsed.insecure = false; - } else { - parsed.insecure = true; - } -} - if (parsed.help) usage(0); -if (parsed.argv.remain.length < 1) +if (parsed._args.length < 1) usage(1, 'DN required'); try { - parsed.argv.remain.forEach(function(dn) { + parsed._args.forEach(function (dn) { ldap.parseDN(dn); }); } catch (e) { usage(1, e.toString()); } -if (!parsed.attribute || typeof(parsed.value) !== 'string') +if (!parsed.attribute || typeof (parsed.value) !== 'string') usage(1, 'attribute and value required'); if (parsed.debug) logLevel = (parsed.debug > 1 ? 'trace' : 'debug'); -if (!parsed.url) - parsed.url = 'ldap://127.0.0.1:389'; -if (!parsed.binddn) - parsed.binddn = ''; -if (!parsed.password) - parsed.password = ''; var log = new Logger({ name: 'ldapjs', @@ -140,24 +148,24 @@ var client = ldap.createClient({ } }); -client.on('error', function(err) { +client.on('error', function (err) { perror(err); }); -client.bind(parsed.binddn, parsed.password, function(err, res) { +client.bind(parsed.binddn, parsed.password, function (err, res) { if (err) perror(err); var finished = 0; - parsed.argv.remain.forEach(function(dn) { - client.compare(dn, parsed.attribute, parsed.value, function(err, match) { + parsed._args.forEach(function (dn) { + client.compare(dn, parsed.attribute, parsed.value, function (err, match) { if (err) perror(err); process.stdout.write(match + '\n'); - if (++finished === parsed.argv.remain.length) { - client.unbind(function() { + if (++finished === parsed._args.length) { + client.unbind(function () { return; }); } diff --git a/bin/ldapjs-delete b/bin/ldapjs-delete index 7dd5cb9..1f5d069 100755 --- a/bin/ldapjs-delete +++ b/bin/ldapjs-delete @@ -4,9 +4,8 @@ var fs = require('fs'); var path = require('path'); -var url = require('url'); -var nopt = require('nopt'); +var dashdash = require('dashdash'); var ldap = require('../lib/index'); var Logger = require('bunyan'); @@ -14,49 +13,67 @@ var Logger = require('bunyan'); ///--- Globals -nopt.typeDefs.DN = { - type: ldap.DN, - validate: function(data, k, val) { - data[k] = ldap.parseDN(val); +dashdash.addOptionType({ + name: 'ldap.DN', + takesArg: true, + helpArg: 'LDAP_DN', + parseArg: function (option, optstr, arg) { + return ldap.parseDN(arg); } -}; +}); -var opts = { - 'debug': Number, - 'binddn': ldap.DN, - 'insecure': Boolean, - 'password': String, - 'url': url -}; -var shortOpts = { - 'd': ['--debug'], - 'D': ['--binddn'], - 'i': ['--insecure'], - 'w': ['--password'], - 'u': ['--url'] -}; +var opts = [ + { + names: ['url', 'u'], + type: 'string', + help: 'LDAP server URL', + helpArg: 'URL', + default: 'ldap://127.0.0.1:389' + }, + { + names: ['binddn', 'D'], + type: 'ldap.DN', + help: 'Bind DN', + default: '' + }, + { + names: ['password', 'w'], + type: 'string', + help: 'Bind password', + helpArg: 'PASSWD', + default: '' + }, + { + names: ['insecure', 'i'], + type: 'bool', + env: 'LDAPJS_TLS_INSECURE', + help: 'Disable SSL certificate verification', + default: false + }, + { + names: ['debug', 'd'], + type: 'integer', + help: 'Set debug level <0-2>', + helpArg: 'LEVEL' + }, + { + names: ['help', 'h'], + type: 'bool', + help: 'Print this help and exit.' + } +]; +var parser = dashdash.createParser({options: opts}); ///--- Helpers function usage(code, message) { - var _opts = ''; - Object.keys(shortOpts).forEach(function(k) { - if (!Array.isArray(shortOpts[k])) - return; - var longOpt = shortOpts[k][0].replace('--', ''); - var type = opts[longOpt].name || 'string'; - if (type && type === 'boolean') type = ''; - type = type.toLowerCase(); - - _opts += ' [--' + longOpt + ' ' + type + ']'; - }); - _opts += ' DN'; + var _opts = parser.help(); var msg = (message ? message + '\n' : '') + - 'usage: ' + path.basename(process.argv[1]) + _opts; + 'usage: ' + path.basename(process.argv[1]) + ' \n' + _opts; process.stderr.write(msg + '\n'); process.exit(code); @@ -80,26 +97,17 @@ var logLevel = 'info'; var parsed; try { - parsed = nopt(opts, shortOpts, process.argv, 2); + parsed = parser.parse(process.argv); } catch (e) { usage(1, e.toString()); } -if (parsed.insecure === undefined && - process.env.LDAPJS_TLS_INSECURE !== undefined) { - if (process.env.LDAPJS_TLS_INSECURE === '0') { - parsed.insecure = false; - } else { - parsed.insecure = true; - } -} - if (parsed.help) usage(0); -if (parsed.argv.remain.length < 1) +if (parsed._args.length < 1) usage(1, 'DN required'); try { - parsed.argv.remain.forEach(function(dn) { + parsed._args.forEach(function (dn) { ldap.parseDN(dn); }); } catch (e) { @@ -108,12 +116,6 @@ try { if (parsed.debug) logLevel = (parsed.debug > 1 ? 'trace' : 'debug'); -if (!parsed.url) - parsed.url = 'ldap://127.0.0.1:389'; -if (!parsed.binddn) - parsed.binddn = ''; -if (!parsed.password) - parsed.password = ''; var log = new Logger({ name: 'ldapjs', @@ -130,11 +132,11 @@ var client = ldap.createClient({ } }); -client.on('error', function(err) { +client.on('error', function (err) { perror(err); }); -client.bind(parsed.binddn, parsed.password, function(err, res) { +client.bind(parsed.binddn, parsed.password, function (err, res) { if (err) perror(err); @@ -143,11 +145,11 @@ client.bind(parsed.binddn, parsed.password, function(err, res) { if (err) perror(err); - if (++finished === parsed.argv.remain.length) + if (++finished === parsed._args.length) client.unbind(function () { return; }); } - parsed.argv.remain.forEach(function(dn) { + parsed._args.forEach(function (dn) { client.del(dn, callback); }); }); diff --git a/bin/ldapjs-modify b/bin/ldapjs-modify index 1199338..85ce9e2 100755 --- a/bin/ldapjs-modify +++ b/bin/ldapjs-modify @@ -4,9 +4,8 @@ var fs = require('fs'); var path = require('path'); -var url = require('url'); -var nopt = require('nopt'); +var dashdash = require('dashdash'); var ldap = require('../lib/index'); var Logger = require('bunyan'); @@ -14,57 +13,85 @@ var Logger = require('bunyan'); ///--- Globals -nopt.typeDefs.DN = { - type: ldap.DN, - validate: function(data, k, val) { - data[k] = ldap.parseDN(val); +dashdash.addOptionType({ + name: 'ldap.DN', + takesArg: true, + helpArg: 'LDAP_DN', + parseArg: function (option, optstr, arg) { + return ldap.parseDN(arg); } -}; +}); -var opts = { - 'attribute': String, - 'debug': Number, - 'binddn': ldap.DN, - 'file': String, - 'insecure': Boolean, - 'password': String, - 'type': String, - 'url': url, - 'value': Array, -}; -var shortOpts = { - 'a': ['--attribute'], - 'd': ['--debug'], - 'D': ['--binddn'], - 'f': ['--file'], - 'i': ['--insecure'], - 'w': ['--password'], - 't': ['--type'], - 'u': ['--url'], - 'v': ['--value'] -}; +var opts = [ + { + names: ['url', 'u'], + type: 'string', + help: 'LDAP server URL', + helpArg: 'URL', + default: 'ldap://127.0.0.1:389' + }, + { + names: ['binddn', 'D'], + type: 'ldap.DN', + help: 'Bind DN', + default: '' + }, + { + names: ['password', 'w'], + type: 'string', + help: 'Bind password', + helpArg: 'PASSWD', + default: '' + }, + { + names: ['attribute', 'a'], + type: 'string', + help: 'Attribute to modify', + helpArg: 'ATTR' + }, + { + names: ['value', 'v'], + type: 'string', + help: 'Desired value', + helpArg: 'VAL' + }, + { + names: ['type', 't'], + type: 'string', + help: 'Attribute type', + helpArg: 'TYPE' + }, + { + names: ['insecure', 'i'], + type: 'bool', + env: 'LDAPJS_TLS_INSECURE', + help: 'Disable SSL certificate verification', + default: false + }, + { + names: ['debug', 'd'], + type: 'integer', + help: 'Set debug level <0-2>', + helpArg: 'LEVEL' + }, + { + names: ['help', 'h'], + type: 'bool', + help: 'Print this help and exit.' + } +]; +var parser = dashdash.createParser({options: opts}); ///--- Helpers function usage(code, message) { - var _opts = ''; - Object.keys(shortOpts).forEach(function(k) { - if (!Array.isArray(shortOpts[k])) - return; - var longOpt = shortOpts[k][0].replace('--', ''); - var type = opts[longOpt].name || 'string'; - if (type && type === 'boolean') type = ''; - type = type.toLowerCase(); - - _opts += ' [--' + longOpt + ' ' + type + ']'; - }); - _opts += ' DN'; + var _opts = parser.help(); var msg = (message ? message + '\n' : '') + - 'usage: ' + path.basename(process.argv[1]) + _opts; + 'usage: ' + path.basename(process.argv[1]) + ' \n' + _opts; process.stderr.write(msg + '\n'); process.exit(code); @@ -88,27 +115,18 @@ var logLevel = 'info'; var parsed; try { - parsed = nopt(opts, shortOpts, process.argv, 2); + parsed = parser.parse(process.argv); } catch (e) { usage(1, e.toString()); } -if (parsed.insecure === undefined && - process.env.LDAPJS_TLS_INSECURE !== undefined) { - if (process.env.LDAPJS_TLS_INSECURE === '0') { - parsed.insecure = false; - } else { - parsed.insecure = true; - } -} - if (parsed.help) usage(0); -if (parsed.argv.remain.length < 1) +if (parsed._args.length < 1) usage(1, 'DN required'); try { - parsed.argv.remain.forEach(function(dn) { + parsed._args.forEach(function (dn) { ldap.parseDN(dn); }); } catch (e) { @@ -122,12 +140,6 @@ if (!parsed.attribute || !Array.isArray(parsed.value)) if (parsed.debug) logLevel = (parsed.debug > 1 ? 'trace' : 'debug'); -if (!parsed.url) - parsed.url = 'ldap://127.0.0.1:389'; -if (!parsed.binddn) - parsed.binddn = ''; -if (!parsed.password) - parsed.password = ''; var log = new Logger({ name: 'ldapjs', @@ -144,18 +156,18 @@ var client = ldap.createClient({ } }); -client.on('error', function(err) { +client.on('error', function (err) { perror(err); }); -client.bind(parsed.binddn, parsed.password, function(err, res) { +client.bind(parsed.binddn, parsed.password, function (err, res) { if (err) perror(err); var finished = 0; var mod = {}; mod[parsed.attribute] = []; - parsed.value.forEach(function(v) { + parsed.value.forEach(function (v) { mod[parsed.attribute].push(v); }); var change = new ldap.Change({ @@ -167,14 +179,14 @@ client.bind(parsed.binddn, parsed.password, function(err, res) { if (err) perror(err); - if (++finished === parsed.argv.remain.length) { - client.unbind(function() { + if (++finished === parsed._args.length) { + client.unbind(function () { return; }); } } - parsed.argv.remain.forEach(function(dn) { + parsed._args.forEach(function (dn) { client.modify(dn, change, callback); }); }); diff --git a/bin/ldapjs-search b/bin/ldapjs-search index 3e3243a..07a5516 100755 --- a/bin/ldapjs-search +++ b/bin/ldapjs-search @@ -3,9 +3,8 @@ // Copyright 2011 Mark Cavage. All rights reserved. var path = require('path'); -var url = require('url'); -var nopt = require('nopt'); +var dashdash = require('dashdash'); var ldap = require('../lib/index'); var Logger = require('bunyan'); @@ -13,69 +12,126 @@ var Logger = require('bunyan'); ///--- Globals -nopt.typeDefs.DN = { - type: ldap.DN, - validate: function(data, k, val) { - data[k] = ldap.parseDN(val); +dashdash.addOptionType({ + name: 'ldap.DN', + takesArg: true, + helpArg: 'LDAP_DN', + parseArg: function (option, optstr, arg) { + return ldap.parseDN(arg); } -}; +}); -nopt.typeDefs.Filter = { - type: ldap.Filter, - validate: function(data, k, val) { - data[k] = ldap.parseFilter(val); +dashdash.addOptionType({ + name: 'ldap.Filter', + takesArg: true, + helpArg: 'LDAP_FILTER', + parseArg: function (option, optstr, arg) { + return ldap.parseFilter(arg); } -}; +}); + +dashdash.addOptionType({ + name: 'ldap.scope', + takesArg: true, + helpArg: 'SCOPE', + parseArg: function (option, optstr, arg) { + if (!/^base|one|sub$/.test(arg)) { + throw new TypeError('Scope must be '); + } + } +}); -var opts = { - 'debug': Number, - 'base': ldap.DN, - 'binddn': ldap.DN, - 'control': Array, - 'insecure': Boolean, - 'password': String, - 'persistent': Boolean, - 'paged': Number, - 'scope': String, - 'timeout': Number, - 'url': url -}; - -var shortOpts = { - 'c': ['--control'], - 'd': ['--debug'], - 'b': ['--base'], - 'D': ['--binddn'], - 'i': ['--insecure'], - 'w': ['--password'], - 'p': ['--persistent'], - 'g': ['--paged'], - 's': ['--scope'], - 't': ['--timeout'], - 'u': ['--url'] -}; +var opts = [ + { + names: ['url', 'u'], + type: 'string', + help: 'LDAP server URL', + helpArg: 'URL', + default: 'ldap://127.0.0.1:389' + }, + { + names: ['binddn', 'D'], + type: 'ldap.DN', + help: 'Bind DN', + default: '' + }, + { + names: ['password', 'w'], + type: 'string', + help: 'Bind password', + helpArg: 'PASSWD', + default: '' + }, + { + names: ['base', 'b'], + type: 'ldap.DN', + help: 'Base DN of search', + default: '' + }, + { + names: ['scope', 's'], + type: 'ldap.scope', + help: 'Search scope ', + helpArg: 'SCOPE', + default: 'sub' + }, + { + names: ['timeout', 't'], + type: 'integer', + help: 'Search timeout', + helpArg: 'SECS' + }, + { + names: ['persistent', 'p'], + type: 'bool', + help: 'Enable persistent search control', + default: false + }, + { + names: ['paged', 'g'], + type: 'number', + help: 'Enable paged search result control', + helpArg: 'PAGE_SIZE' + }, + { + names: ['control', 'c'], + type: 'arrayOfString', + help: 'Send addition control OID', + helpArg: 'OID', + default: [] + }, + { + names: ['insecure', 'i'], + type: 'bool', + env: 'LDAPJS_TLS_INSECURE', + help: 'Disable SSL certificate verification', + default: false + }, + { + names: ['debug', 'd'], + type: 'integer', + help: 'Set debug level <0-2>', + helpArg: 'LEVEL' + }, + { + names: ['help', 'h'], + type: 'bool', + help: 'Print this help and exit.' + } +]; +var parser = dashdash.createParser({options: opts}); ///--- Helpers function usage(code, message) { - var _opts = ''; - Object.keys(shortOpts).forEach(function(k) { - if (!Array.isArray(shortOpts[k])) - return; - var longOpt = shortOpts[k][0].replace('--', ''); - var type = opts[longOpt].name || 'string'; - if (type && type === 'boolean') type = ''; - type = type.toLowerCase(); - - _opts += ' [--' + longOpt + ' ' + type + ']'; - }); - _opts += ' filter [attributes...]'; + var _opts = parser.help(); var msg = (message ? message + '\n' : '') + - 'usage: ' + path.basename(process.argv[1]) + _opts; + 'usage: ' + path.basename(process.argv[1]) + + ' [attributes]\n' + _opts; process.stderr.write(msg + '\n'); process.exit(code); @@ -98,27 +154,18 @@ function perror(err) { var parsed; try { - parsed = nopt(opts, shortOpts, process.argv, 2); + parsed = parser.parse(process.argv); } catch (e) { usage(1, e.toString()); } -if (parsed.insecure === undefined && - process.env.LDAPJS_TLS_INSECURE !== undefined) { - if (process.env.LDAPJS_TLS_INSECURE === '0') { - parsed.insecure = false; - } else { - parsed.insecure = true; - } -} - if (parsed.help) usage(0); -if (parsed.argv.remain.length < 1) +if (parsed._args.length < 1) usage(1, 'filter required'); try { - ldap.parseFilter(parsed.argv.remain[0]); + ldap.parseFilter(parsed._args[0]); } catch (e) { usage(1, e.message); } @@ -127,18 +174,6 @@ var logLevel = 'info'; if (parsed.debug) logLevel = (parsed.debug > 1 ? 'trace' : 'debug'); -if (!parsed.url) - parsed.url = 'ldap://127.0.0.1:389'; -if (!parsed.binddn) - parsed.binddn = ''; -if (!parsed.password) - parsed.password = ''; -if (!parsed.base) - parsed.base = ''; -if (!parsed.control) - parsed.control = []; -if (!parsed.persistent) - parsed.persistent = false; var log = new Logger({ name: 'ldapjs', @@ -156,21 +191,21 @@ var client = ldap.createClient({ } }); -client.on('error', function(err) { +client.on('error', function (err) { perror(err); }); -client.on('timeout', function(req) { +client.on('timeout', function (req) { process.stderr.write('Timeout reached\n'); process.exit(1); }); -client.bind(parsed.binddn, parsed.password, function(err, res) { +client.bind(parsed.binddn, parsed.password, function (err, res) { if (err) perror(err); var controls = []; - parsed.control.forEach(function(c) { + parsed.control.forEach(function (c) { controls.push(new ldap.Control({ type: c, criticality: true @@ -193,23 +228,23 @@ client.bind(parsed.binddn, parsed.password, function(err, res) { } var req = { scope: parsed.scope || 'sub', - filter: parsed.argv.remain[0], - attributes: parsed.argv.remain.length > 1 ? parsed.argv.remain.slice(1) : [] + filter: parsed._args[0], + attributes: parsed._args.length > 1 ? parsed._args.slice(1) : [] }; - client.search(parsed.base, req, controls, function(err, res) { + client.search(parsed.base, req, controls, function (err, res) { if (err) perror(err); - res.on('searchEntry', function(entry) { + res.on('searchEntry', function (entry) { process.stdout.write(JSON.stringify(entry.object, null, 2) + '\n'); }); - res.on('error', function(err) { + res.on('error', function (err) { perror(err); }); - res.on('end', function(res) { + res.on('end', function (res) { if (res.status !== 0) process.stderr.write(ldap.getMessage(res.status) + '\n'); - client.unbind(function() { + client.unbind(function () { return; }); }); diff --git a/package.json b/package.json index 88122c9..7c4e869 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "asn1": "0.2.1", "assert-plus": "0.1.5", "bunyan": "0.22.1", - "nopt": "2.1.1", + "dashdash": "1.6.0", "pooling": "0.4.6" }, "optionalDependencies": {