diff --git a/README.md b/README.md index 2c10b4f..7e97557 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ linux-user ========== Node module for Linux user and group control. -Use Node to manage Linux user easily. All APIs do what you think. +Use Node to manage Linux user easily. All APIs do what you think. Promise and +`async\await` out of the box. **Zero dependences!** **The module must be running on Linux and as root user !** @@ -81,12 +82,25 @@ linuxUser.getUsers(function (err, users) { ### Promises -This project works with promises right out of the box! Just grade the promise function +This project works with promises right out of the box! Just grab the promise +function. ```js -var linuxUser = require('linux-sys-user').promise; +var linuxUser = require('linux-sys-user').promise(); ``` +This will NodeJS's `util.promisify` by default. You can pass your own promisify +function like, like bluebird: + +```js +var bluebird = require('bluebird'); +var linuxUser = require('linux-sys-user').promise(bluebird.promisify); + +``` + +** `bluebird` is NOT included with this package! ** If you are using a older +version of NodeJS( less then 8 ) you will need something like it. + This will work with `.then()`, `.catch()` and the `async`/`await` pattern. ```js @@ -139,8 +153,8 @@ console.log(user); missing. ``` - * `expiredate` *String* The date on which the user account will be disabled. - The date is specified in the format `YYYY-MM-DD`.. + * `expiredate` *String* The date on which the user account will be + disabled. The date is specified in the format `YYYY-MM-DD`.. ``` If not specified, useradd will use the default expiry date @@ -182,8 +196,8 @@ console.log(user); * `selinux_user` *String* The SELinux user for the user's login. ``` - The default is to leave this field blank, which causes the system to select - the default SELinux user. + The default is to leave this field blank, which causes the system to + select the default SELinux user. ``` * callback function(err, userInfo) diff --git a/index.js b/index.js index b6fddc3..1c23396 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,16 @@ 'use strict'; -var bluebird= require('bluebird'); -var lib = require('./lib/user'); - -lib.promise = (function(lib) { - var out = {}; +// ensure process is running on linux +if(process.platform !== 'linux') { + throw new Error('linux-user must be running on Linux'); +} - Object.keys(lib).forEach(function(key){ - out[key] = bluebird.promisify(lib[key]); - }); +// ensure process is running as root +if(!(process.getuid && process.getuid() === 0)) { + throw new Error('linux-user must be running as root user'); +} - return out; -})(lib); +var lib = require('./lib/user'); +lib.promises = require('./lib/promise') module.exports = lib; diff --git a/lib/promise.js b/lib/promise.js new file mode 100644 index 0000000..11647e7 --- /dev/null +++ b/lib/promise.js @@ -0,0 +1,16 @@ + +var promise = function(promise_fn) { + var lib = require('./user'); + + promise_fn = promise_fn || util.promisify; + + var out = {}; + + Object.keys(lib).forEach(function(key){ + out[key] = promise_fn(lib[key]); + }); + + return out; +} + +module.exports = promise; diff --git a/lib/user.js b/lib/user.js index 7f38e90..13e2fad 100644 --- a/lib/user.js +++ b/lib/user.js @@ -5,22 +5,12 @@ var exec = require('child_process').exec; var spawn = require('child_process').spawn; var fs = require('fs'); -if(process.platform !== 'linux') { - throw new Error('linux-user must be running on Linux'); -} - -// ensure process is running as root -if(!(process.getuid && process.getuid() === 0)) { - throw new Error('linux-user must be running as root user'); -} - var validUsernameRegex = /^([a-z_][a-z0-9_-]{0,30})$/; function validate (username) { - return validUsernameRegex.test(username); + return validUsernameRegex.test(username); } -exports.validateUsername = validate; var user_arg_map = { shell: function(SHELL){ @@ -296,3 +286,6 @@ exports.addSSHtoUser = function (user, key, callback){ }); }); }; + + +exports.validateUsername = validate; diff --git a/package-lock.json b/package-lock.json index 30677bf..b9dd25b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "linux-sys-user", - "version": "0.6.2", + "version": "0.6.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -40,11 +40,6 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, - "bluebird": { - "version": "3.5.5", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz", - "integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==" - }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", diff --git a/package.json b/package.json index 8452fbb..dbd4918 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "linux-sys-user", - "version": "0.6.3", + "version": "1.0.0", "description": "Node module for Linux user and group control.", "author": [ { @@ -43,7 +43,5 @@ "type": "git", "url": "https://github.com/wmantly/linux-user.git" }, - "dependencies": { - "bluebird": "^3.5.5" - } + "dependencies": {} }