-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurator.js
72 lines (63 loc) · 2.88 KB
/
configurator.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
// Configurator.js created for MeshCentral-data config.json file creation. This will ask the questions to generate the file demo-config.json. simple rename of the file and running of meshcentral should garner a working server with the changes done by the scripts config.json creation.
// License: Apache2 GPL
// Creator: Mike Larios
// Date: 01/01/2024 11:45am PST
const fs = require('fs');
// Check if dependencies are installed
try {
var axios = require('axios');
var prompt = require('prompt-sync')();
} catch (error) {
console.error('Dependencies not installed. Please run "npm install axios prompt-sync" and try again.');
process.exit(1);
}
// Define the schema URL
const schemaUrl = 'https://raw.githubusercontent.com/Ylianst/MeshCentral/master/meshcentral-config-schema.json';
// Function to handle each property
function handleProperty(schema, config, property) {
// Check if the property is an object
if (schema.properties[property].type === 'object') {
// Ask the user a yes/no question about including this property
let answer = prompt(`Do you want to include ${property}? (y/n/yes/no) `).toLowerCase();
// If the user answers 'y' or 'yes', handle each nested property
if (answer === 'y' || answer === 'yes') {
config[property] = {};
for (let nestedProperty in schema.properties[property].properties) {
handleProperty(schema.properties[property], config[property], nestedProperty);
}
} else if (answer === 'n' || answer === 'no') {
// If the user answers 'n' or 'no', prefix the property with an underscore to disable it
config['_' + property] = {};
}
} else {
// Ask the user for the value of the property
let value;
// If the property is a boolean, convert 't', 'true', 'f', or 'false' to their boolean equivalents
if (schema.properties[property].type === 'boolean') {
value = prompt(`Enter a value for ${property} (t/f/true/false): `).toLowerCase();
if (value === 't' || value === 'true') {
value = true;
} else if (value === 'f' || value === 'false') {
value = false;
}
} else {
value = prompt(`Enter a value for ${property}: `);
}
config[property] = value;
}
}
// Fetch the schema
axios.get(schemaUrl).then(response => {
const schema = response.data;
// Initialize an empty config object
let config = {};
// Iterate over each property in the schema
for (let property in schema.properties) {
handleProperty(schema, config, property);
}
// Write the config to a file
fs.writeFileSync('demo-config.json', JSON.stringify(config, null, 2));
console.log('Configuration saved to demo-config.json');
}).catch(error => {
console.error(`Failed to fetch schema: ${error}`);
});