#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: 2021 Birger Schacht
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# IntelMQ-API adduser command
import argparse
import getpass
import os
import sys

import session.session
import session.config

session_config: session.config.Config = session.config.Config(os.environ.get("FODY_SESSION_CONFIG"))

parser = argparse.ArgumentParser(description='Add a user account to the Fody session store.')
parser.add_argument('--user', required=True, help='The username of the account.', type=str)
parser.add_argument('--password', required=False, help='The password of the account.', type=str)

args = parser.parse_args()

if session_config.session_store is None:
    print("Could not add user- no session store configured in configuration!", file=sys.stderr)
    exit(1)

session_store = session.session.SessionStore(str(session_config.session_store), session_config.session_duration)

if args.password is None:
    password = getpass.getpass()
else:
    password = args.password
session_store.add_user(args.user, password)
print(f"Added user {args.user} to fody session file.")