/
cli.py
executable file
·98 lines (82 loc) · 3.23 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# CLI directly called by running the howdy command
# Import required modules
import sys
import os
import subprocess
import getpass
import argparse
import builtins
# Try to get the original username (not "root") from shell
user = subprocess.check_output("echo $(logname 2>/dev/null || echo $SUDO_USER)", shell=True).decode("ascii").strip()
# If that fails, try to get the direct user
if user == "root" or user == "":
env_user = getpass.getuser().strip()
# If even that fails, error out
if env_user == "":
print("Could not determine user, please use the --user flag")
sys.exit(1)
else:
user = env_user
# 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")
# Add an argument for the command
parser.add_argument("command",
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"])
# Add an argument for the extra arguments of diable and remove
parser.add_argument("argument",
help="Either 0 (enable) or 1 (disable) for the disable command, or the model ID for the remove command.",
nargs="?")
# Add the user flag
parser.add_argument("-U", "--user",
default=user,
help="Set the user account to use.")
# Add the -y flag
parser.add_argument("-y",
help="Skip all questions.",
action="store_true")
# Overwrite the default help message so we can use a uppercase S
parser.add_argument("-h", "--help",
action="help",
default=argparse.SUPPRESS,
help="Show this help message and exit.")
# If we only have 1 argument we print the help text
if len(sys.argv) < 2:
print("current active user: " + user + "\n")
parser.print_help()
sys.exit(0)
# Parse all arguments above
args = parser.parse_args()
# Save the args and user as builtins which can be accessed by the imports
builtins.howdy_args = args
builtins.howdy_user = args.user
# 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)
# 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":
import cli.test