forked from lmachens/pw4u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
71 lines (59 loc) · 1.99 KB
/
cli.js
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
require("dotenv").config();
const {
setPassword,
getPassword,
deletePasswordByName,
updatePasswordByName,
} = require("./lib/passwords");
const {
askForMasterPassword,
askForNext,
newPassword,
readPassword,
passwordToDelete,
passwordToUpdate,
} = require("./lib/questions");
const { isMasterPasswordCorrect } = require("./lib/validation");
const { connect, close, collection } = require("./lib/database");
async function run() {
console.log("Connection to database...");
await connect(process.env.DB_URL, process.env.DB_NAME);
console.log("Connectet to database!");
const masterPassword = await askForMasterPassword();
const findCursor = await collection("passwords").find({});
const numberOfPasswords = await findCursor.count();
console.log(`You have ${numberOfPasswords} passwords in your Safe`);
if (!(await isMasterPasswordCorrect(masterPassword))) {
console.error("You are not welcome here! 👿 Try again!");
return run();
}
const nextToDo = await askForNext();
console.log(nextToDo);
if (nextToDo === "set") {
const [newPasswordName, newPasswordValue] = await newPassword();
await setPassword(newPasswordName, newPasswordValue);
console.log(`Password ${newPasswordName} set 🎉`);
}
if (nextToDo === "read") {
const passwordName = await readPassword();
const passwordValue = await getPassword(passwordName);
console.log(`Your password is ${passwordValue} 🎉`);
}
if (nextToDo === "delete") {
const deletedPassword = await passwordToDelete();
await deletePasswordByName(deletedPassword);
console.log(`Your password ${deletedPassword} is deleted`);
}
if (nextToDo === "update") {
const [
updatePasswordName,
updateNewPasswordValue,
] = await passwordToUpdate();
await updatePasswordByName(updatePasswordName, updateNewPasswordValue);
console.log(`Your password ${updatePasswordName} is updated`);
}
// masterpassword in dotenv
// encrypt update password value
await close();
}
run();