0
0
Fork 0
mirror of https://github.com/boltgolt/howdy.git synced 2024-10-17 10:06:53 +02:00
howdy/src/cli.py

99 lines
3.2 KiB
Python
Raw Normal View History

2018-02-08 01:11:47 +01:00
#!/usr/bin/env python3
2018-02-13 23:22:13 +01:00
# CLI directly called by running the howdy command
# Import required modules
2018-02-08 01:11:47 +01:00
import sys
2018-02-13 22:03:03 +01:00
import os
2018-04-13 14:54:06 +02:00
import subprocess
import getpass
import argparse
2018-04-13 21:19:28 +02:00
import builtins
2018-04-13 14:54:06 +02:00
2018-04-13 21:19:28 +02:00
# Try to get the original username (not "root") from shell
2018-04-13 14:54:06 +02:00
user = subprocess.check_output("echo $(logname 2>/dev/null || echo $SUDO_USER)", shell=True).decode("ascii").strip()
2018-04-13 21:19:28 +02:00
# If that fails, try to get the direct user
2018-04-13 14:54:06 +02:00
if user == "root" or user == "":
env_user = getpass.getuser().strip()
2018-04-13 21:19:28 +02:00
# If even that fails, error out
if env_user == "":
2018-04-13 14:54:06 +02:00
print("Could not determine user, please use the --user flag")
sys.exit(1)
else:
user = env_user
2018-04-13 21:19:28 +02:00
# Basic command setup
parser = argparse.ArgumentParser(description="Command line interface for Howdy face authentication.",
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False,
prog="howdy",
epilog="For support please visit\nhttps://github.com/Boltgolt/howdy")
2018-04-13 14:54:06 +02:00
2018-04-13 21:19:28 +02:00
# Add an argument for the command
2018-04-13 14:54:06 +02:00
parser.add_argument("command",
2018-04-13 21:19:28 +02:00
help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove or test.",
metavar="command",
choices=["add", "clear", "config", "disable", "list", "remove", "test"])
2018-04-13 14:54:06 +02:00
2018-04-13 21:19:28 +02:00
# Add an argument for the extra arguments of diable and remove
2018-04-13 14:54:06 +02:00
parser.add_argument("argument",
2018-04-13 21:19:28 +02:00
help="Either 0 (enable) or 1 (disable) for the disable command, or the model ID for the remove command.",
nargs="?")
2018-04-13 14:54:06 +02:00
2018-04-13 21:19:28 +02:00
# Add the user flag
2018-04-13 14:54:06 +02:00
parser.add_argument("-U", "--user",
2018-04-13 21:19:28 +02:00
default=user,
2018-04-13 14:54:06 +02:00
help="Set the user account to use.")
2018-04-13 21:19:28 +02:00
# Add the -y flag
2018-04-13 14:54:06 +02:00
parser.add_argument("-y",
help="Skip all questions.",
2018-04-13 21:19:28 +02:00
action="store_true")
2018-04-13 14:54:06 +02:00
2018-04-13 21:19:28 +02:00
# Overwrite the default help message so we can use a uppercase S
2018-04-13 14:54:06 +02:00
parser.add_argument("-h", "--help",
2018-04-13 21:19:28 +02:00
action="help",
default=argparse.SUPPRESS,
2018-04-13 14:54:06 +02:00
help="Show this help message and exit.")
2018-04-13 21:19:28 +02:00
# If we only have 1 argument we print the help text
2018-04-13 14:54:06 +02:00
if len(sys.argv) < 2:
2018-04-13 15:27:52 +02:00
print("current active user: " + user + "\n")
parser.print_help()
sys.exit(0)
2018-04-13 14:54:06 +02:00
2018-04-13 21:19:28 +02:00
# Parse all arguments above
2018-04-13 14:54:06 +02:00
args = parser.parse_args()
2018-04-13 21:19:28 +02:00
# Save the args and user as builtins which can be accessed by the imports
builtins.howdy_args = args
builtins.howdy_user = args.user
2018-04-13 22:40:01 +02:00
# Check if we have rootish rights
# This is this far down the file so running the command for help is always possible
if os.getenv("SUDO_USER") is None:
print("Please run this command with sudo")
sys.exit(1)
2018-04-13 21:19:28 +02:00
# Beond this point the user can't change anymore, if we still have root as user we need to abort
if args.user == "root":
print("Can't run howdy commands as root, please run this command with the --user flag")
sys.exit(1)
# Execute the right command
if args.command == "add":
import cli.add
elif args.command == "clear":
import cli.clear
elif args.command == "config":
import cli.config
elif args.command == "disable":
import cli.disable
elif args.command == "list":
import cli.list
elif args.command == "remove":
import cli.remove
elif args.command == "test":
2018-02-15 19:04:53 +01:00
import cli.test