From 3c97e3a831d480148f02e1b85c641a3b34813f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Fiedler?= Date: Wed, 6 Mar 2024 15:13:26 +0100 Subject: [PATCH] =?UTF-8?q?=E0=BC=BC=20=E3=81=A4=20=E2=97=95=5F=E2=97=95?= =?UTF-8?q?=20=E0=BC=BD=E3=81=A4=20initial=20commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 12 +++++++++ .prettierrc.json | 9 +++++++ README.md | 1 + package.json | 20 +++++++++++++++ run.mjs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 .gitignore create mode 100644 .prettierrc.json create mode 100644 README.md create mode 100644 package.json create mode 100644 run.mjs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61c9379 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ + +.DS_Store + +node_modules/ + +.vscode/ + +*.csv + +*.toml + +package-lock.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..b544327 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,9 @@ +{ + "trailingComma": "none", + "tabWidth": 4, + "semi": false, + "singleQuote": true, + "singleAttributePerLine": false, + "html.format.wrapLineLength": 0, + "printWidth": 900 +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..1337d07 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +converts a VereinOnline user CSV export to a FabAccess user.toml diff --git a/package.json b/package.json new file mode 100644 index 0000000..95148fe --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "csv-to-fabaccess-user-toml", + "version": "1.0.0", + "description": "VereinOnline CSV to FabACcess user.toml", + "homepage": "https://www.sternenlabor.de/", + "main": "run.mjs", + "author": "André Fiedler", + "license": "CC0", + "bugs": { + "url": "https://github.com/Sternenlabor/csv-to-fabaccess-user-toml/issues" + }, + "scripts": { + "start": "node run.mjs" + }, + "dependencies": { + "@iarna/toml": "^2.2.5", + "csv-parser": "^3.0.0", + "iconv-lite": "^0.6.3" + } +} diff --git a/run.mjs b/run.mjs new file mode 100644 index 0000000..3a6d7b0 --- /dev/null +++ b/run.mjs @@ -0,0 +1,67 @@ +import csv from 'csv-parser' +import fs from 'fs' +import iconv from 'iconv-lite' +import toml from '@iarna/toml' + +const DEFAULT_PASSWORD = 'secret' + +const users = { + Admin1: { + roles: ['Admin'], + passwd: 'secret' + } +} + +async function readCSV(filePath) { + const results = [] + + const stream = fs + .createReadStream(filePath) + .pipe(iconv.decodeStream('iso-8859-1')) + .pipe( + csv({ + separator: ';' + }) + ) + + for await (const record of stream) { + results.push(record) + } + + return results +} + +function getGroups(u) { + let groups = u.Gruppen.split(', ') + groups = groups.filter((item) => item !== 'Mitglieder-Infos') + return ['User', ...groups] +} + +readCSV('./Mitgliederliste.csv') + .then((results) => { + console.log(results) + let userCounter = 0 + + for (const u of results) { + if (u.Login == '') { + continue + } + + users[u.Login] = { + roles: getGroups(u), + passwd: DEFAULT_PASSWORD + } + + userCounter++ + } + + const tomlContent = toml.stringify(users) + + fs.promises + .writeFile('user.toml', tomlContent) + .then(() => console.log('File created successfully')) + .catch((err) => console.error('Failed to create file', err)) + + console.log('users:', userCounter) + }) + .catch((err) => console.error(err))