-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (97 loc) · 3.18 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const fs = require("fs");
const provisioner = require("./actions/provisioner");
const welcomer = require("./actions/welcomer");
const occsettings = require("./actions/occ-settings");
const inquirer = require("inquirer");
const program = require("commander");
program.version("1.0.0");
/**
* Reads the given file and parses it as JSON. We could use some JSON schema validation here,
* but to avoid dependencies we treat schema validation negligible.
*/
function readJSON(path) {
try {
const users = JSON.parse(fs.readFileSync(path));
if (!Array.isArray(users)) {
return Promise.reject("JSON object must be an array of users!");
} else {
return Promise.resolve(users);
}
} catch (e) {
console.error(`Could not read or parse user JSON at ${path}`);
return Promise.reject(e);
}
}
/**
* Prompt for the Nextcloud user credentials.
* @returns {Promise<*>}
*/
async function getCredentials() {
return await inquirer
.prompt([
{
type: "input",
name: "username",
message: "Your Nextcloud username"
},
{
type: "password",
message: "Your Nextcloud password",
name: "password",
mask: '*'
}
]);
}
/**
* Adds new users to the Nextcloud instance.
*/
program.command("add <users.json> <nextcloud-url>")
.option("-d, --dry-run", "Dry run without sending out requests to your Nextcloud instance")
.action(function (userJson, nextcloudUrl, cmd) {
if (cmd.dryRun === true) {
console.log(`This is a dry run and will send any request to ${nextcloudUrl}`);
}
readJSON(userJson)
.then((users) => {
getCredentials().then(({username, password}) => {
provisioner.provision(users, nextcloudUrl, username, password, (cmd.dryRun === true));
});
})
.catch((e) => {
console.error(e);
});
});
/**
* Sends out welcome mails.
*/
program.command("welcome <users.json> <nextcloud-url>")
.option("-d, --dry-run", "Dry run without sending out requests to your Nextcloud instance")
.action(function (userJson, nextcloudUrl, cmd) {
if (cmd.dryRun === true) {
console.log(`This is a dry run and will send any request to ${nextcloudUrl}`);
}
readJSON(userJson)
.then((users) => {
getCredentials().then(({username, password}) => {
welcomer.sendEmails(users, nextcloudUrl, username, password, (cmd.dryRun === true));
});
})
.catch((e) => {
console.error(e);
});
});
/**
* Prints the setting commands to the console.
*/
program.command("setting <users.json> <key> <value>")
.action(function (userJson, key, value) {
readJSON(userJson)
.then((users) => {
occsettings.printCommand(users, key, value);
})
.catch((e) => {
console.error(e);
});
});
program.parse(process.argv);