From 1b66b67c454a230b030aa94147077487b40be407 Mon Sep 17 00:00:00 2001
From: Chakshu Gautam <chaks.gautam@gmail.com>
Date: Mon, 13 Mar 2023 12:34:53 +0530
Subject: [PATCH] Create cli.js

---
 central-config/cli.js | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 central-config/cli.js

diff --git a/central-config/cli.js b/central-config/cli.js
new file mode 100644
index 0000000..950f88f
--- /dev/null
+++ b/central-config/cli.js
@@ -0,0 +1,42 @@
+// Copyright 2017 ODK Central Developers
+// See the NOTICE file at the top-level directory of this distribution and at
+// https://github.com/getodk/central-backend/blob/master/NOTICE.
+// This file is part of ODK Central. It is subject to the license terms in
+// the LICENSE file found in the top-level directory of this distribution and at
+// https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central,
+// including this file, may be copied, modified, propagated, or distributed
+// except according to the terms contained in the LICENSE file.
+//
+// This script is our primary administrative utility, providing a packaged way
+// for people deploying this server to run basic tasks like creating users and
+// resetting their passwords. As much as possible, this file itself tries only
+// to parse the command-line input and delegate the actual work to tasks that
+// are already defined.
+
+const { run } = require('../task/task');
+const { createUser, promoteUser, setUserPassword } = require('../task/account');
+
+// gets a password interactively if not supplied in cli args.
+const prompt = require('prompt');
+const withPassword = (f) => {
+  prompt.start();
+  prompt.get([{ name: 'password', hidden: true, replace: '*' }], (_, { password }) => f(password));
+};
+
+// command line nonsense (i'm not a huge fan of this library).
+const cli = require('cli');
+const cliArgs = {
+  email: [ 'u', 'For user create and set password commands, supplies the email.', 'email' ]
+};
+const cliCommands = [ 'user-create', 'user-promote', 'user-set-password' ];
+cli.parse(cliArgs, cliCommands);
+
+// map commands to tasks.
+cli.main((args, options) => {
+  if (cli.command === 'user-create')
+    withPassword((password) => run(createUser(options.email, password)));
+  else if (cli.command === 'user-promote')
+    run(promoteUser(options.email));
+  else if (cli.command === 'user-set-password')
+    withPassword((password) => run(setUserPassword(options.email, password)));
+});
-- 
GitLab