Add some cli
This commit is contained in:
parent
126e0e99c7
commit
c31c801f56
4 changed files with 103 additions and 10 deletions
1
app.js
1
app.js
|
@ -5,6 +5,7 @@ let bodyParser = require('body-parser');
|
|||
let session = require("express-session");
|
||||
let fs = require("fs");
|
||||
let addUser = require("./user").addUser;
|
||||
require("./cli");
|
||||
|
||||
let indexRoute = require("./routes/index");
|
||||
let uploadRoute = require("./routes/upload");
|
||||
|
|
36
cli.js
Normal file
36
cli.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
let yargs = require("yargs");
|
||||
let user = require("./user");
|
||||
|
||||
yargs.scriptName("node cli.js")
|
||||
.command("adduser [username] [password]", "Add user", (yargs) => {
|
||||
yargs.positional("username", {
|
||||
describe: "Username for the new user",
|
||||
type: "string"
|
||||
}).positional("password", {
|
||||
describe: "The user password",
|
||||
type: "string"
|
||||
});
|
||||
}, async (argv) => {
|
||||
if (!("username" in argv))
|
||||
argv.username = await user.getUsername();
|
||||
else if (argv.username in user.getFile()) {
|
||||
console.error("Invalid username !");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!("password" in argv))
|
||||
argv.password = await user.getPassword();
|
||||
user.setUser(argv.username, argv.password);
|
||||
process.exit(0);
|
||||
})
|
||||
.command("deluser <username>", "Remove user", (yargs) => {
|
||||
yargs.positional("username", {
|
||||
type: "string",
|
||||
describe: "The user to remove"
|
||||
});
|
||||
}, async (argv) => {
|
||||
process.exit(user.delUser(argv.username));
|
||||
})
|
||||
.help()
|
||||
.alias("h", "help")
|
||||
.argv;
|
||||
|
|
@ -11,7 +11,8 @@
|
|||
"morgan": "^1.10.0",
|
||||
"multer": "^1.4.2",
|
||||
"password-hash": "^1.2.2",
|
||||
"pug": "^2.0.4"
|
||||
"pug": "^2.0.4",
|
||||
"yargs": "^15.3.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "node app.js"
|
||||
|
|
73
user.js
73
user.js
|
@ -1,18 +1,66 @@
|
|||
let readline = require("readline")
|
||||
let fs = require("fs");
|
||||
let passwordHash = require('password-hash');
|
||||
|
||||
|
||||
async function addUser() {
|
||||
let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false});
|
||||
function getFile() {
|
||||
if (fs.existsSync("users.json")) {
|
||||
file = JSON.parse(fs.readFileSync("users.json"));
|
||||
} else {
|
||||
fs.writeFileSync("users.json", "{}");
|
||||
file = {};
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
function getReadLine() {
|
||||
return readline.createInterface({input: process.stdin, output: process.stdout, terminal: false});
|
||||
}
|
||||
|
||||
async function getUsername() {
|
||||
let rl = getReadLine();
|
||||
let username;
|
||||
let password;
|
||||
file = JSON.parse(fs.readFileSync("users.json"));
|
||||
do {
|
||||
let file = getFile();
|
||||
while (true) {
|
||||
username = await new Promise(resolve => rl.question("Username: ", resolve));
|
||||
} while (username in file || ["", null].indexOf(username) >= 0);
|
||||
password = passwordHash.generate(await new Promise(resolve => rl.question("Password: ", resolve)));
|
||||
if (username in file || ["", null].indexOf(username) >= 0)
|
||||
console.error("Invalid username !")
|
||||
else
|
||||
break;
|
||||
}
|
||||
rl.close();
|
||||
return username;
|
||||
}
|
||||
|
||||
async function getPassword() {
|
||||
let rl = getReadLine();
|
||||
let password = await new Promise(resolve => rl.question("Password: ", resolve));
|
||||
rl.close();
|
||||
return passwordHash.generate(password);
|
||||
}
|
||||
|
||||
function setUser(username, password) {
|
||||
let file = getFile();
|
||||
file[username] = password;
|
||||
fs.writeFileSync("users.json", JSON.stringify(file));
|
||||
console.log("User " + username + " created");
|
||||
}
|
||||
|
||||
async function addUser() {
|
||||
setUser(await getUsername(), await getPassword());
|
||||
}
|
||||
|
||||
function delUser(username) {
|
||||
let file = JSON.parse(fs.readFileSync("users.json"));
|
||||
if (username in file) {
|
||||
delete file[username];
|
||||
fs.writeFileSync("users.json", JSON.stringify(file));
|
||||
console.log("User " + username + " deleted");
|
||||
return 0;
|
||||
} else {
|
||||
console.error("Username not found !");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
function isAuth(req, res, next) {
|
||||
|
@ -24,6 +72,13 @@ function isAuth(req, res, next) {
|
|||
}
|
||||
|
||||
|
||||
module.exports.addUser = addUser;
|
||||
module.exports.isAuth = isAuth;
|
||||
module.exports = {
|
||||
getFile: getFile,
|
||||
getUsername: getUsername,
|
||||
getPassword: getPassword,
|
||||
setUser: setUser,
|
||||
addUser: addUser,
|
||||
delUser: delUser,
|
||||
isAuth: isAuth
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue