From 73c0c911d4aaeb4cf7eac5bb81fac65797ebf062 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 13 Nov 2018 10:40:49 +0000 Subject: [PATCH] Move logic for adding a server to the database out to utils.cli and add a --add-server flag for start.py, to add new server --- src/utils/__init__.py | 2 +- src/utils/cli.py | 19 +++++++++++++++++++ start.py | 30 ++++++++++++------------------ 3 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 src/utils/cli.py diff --git a/src/utils/__init__.py b/src/utils/__init__.py index d29197df..4b807550 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,5 +1,5 @@ import decimal, io, re, typing -from src.utils import consts, irc, http, parse +from src.utils import cli, consts, irc, http, parse TIME_SECOND = 1 TIME_MINUTE = TIME_SECOND*60 diff --git a/src/utils/cli.py b/src/utils/cli.py new file mode 100644 index 00000000..98d6be92 --- /dev/null +++ b/src/utils/cli.py @@ -0,0 +1,19 @@ +from src import Database + +def bool_input(s: str): + result = input("%s (Y/n): " % s) + return not result or result[0].lower() in ["", "y"] + +def add_server(database: Database.Database): + alias = input("alias: ") + hostname = input("hostname: ") + port = int(input("port: ")) + tls = bool_input("tls?") + password = input("password?: ") + ipv4 = bool_input("ipv4?") + nickname = input("nickname: ") + username = input("username: ") + realname = input("realname: ") + bindhost = input("bindhost?: ") + database.servers.add(alias, hostname, port, password, ipv4, tls, bindhost, nickname, username, realname) + diff --git a/start.py b/start.py index a928824f..ebe638ad 100755 --- a/start.py +++ b/start.py @@ -2,11 +2,7 @@ import argparse, os, sys, time from src import Cache, Config, Database, EventManager, Exports, IRCBot -from src import Logging, ModuleManager, Timers - -def bool_input(s): - result = input("%s (Y/n): " % s) - return not result or result[0].lower() in ["", "y"] +from src import Logging, ModuleManager, Timers, utils directory = os.path.dirname(os.path.realpath(__file__)) @@ -25,15 +21,24 @@ arg_parser.add_argument("--log", "-l", help="Location of the main log file", default=os.path.join(directory, "logs", "bot.log")) +arg_parser.add_argument("--add-server", "-a", + help="Add a new server", action="store_true") + arg_parser.add_argument("--verbose", "-v", action="store_true") args = arg_parser.parse_args() log_level = "debug" if args.verbose else "info" log = Logging.Log(log_level, args.log) +database = Database.Database(log, args.database) + +if args.add_server: + print("Adding a new server") + utils.cli.add_server(database) + sys.exit(0) + cache = Cache.Cache() config = Config.Config(args.config) -database = Database.Database(log, args.database) events = events = EventManager.EventHook(log) exports = exports = Exports.Exports() timers = Timers.Timers(database, events, log) @@ -69,18 +74,7 @@ if len(server_configs): else: try: if bool_input("no servers found, add one?"): - alias = input("alias: ") - hostname = input("hostname: ") - port = int(input("port: ")) - tls = bool_input("tls?") - password = input("password?: ") - ipv4 = bool_input("ipv4?") - nickname = input("nickname: ") - username = input("username: ") - realname = input("realname: ") - bindhost = input("bindhost?: ") - bot.database.servers.add(alias, hostname, port, password, ipv4, - tls, bindhost, nickname, username, realname) + utils.cli.add_server(database) except KeyboardInterrupt: print() pass