howdy/src/cli/disable.py
arifer612 fcd3bedb00
Changed final print statement to reflect changes
Arguments for the `disable` command are either `{0, false}`, or `{1, true}`. However, only if the argument is `1` will the print statement print "Howdy has been disabled". It should, by right, also show if the argument was `true`. To fix it, I noticed that the 2 possible sets of arguments fall into either of 2 groups characterised by the variable `out_value`. I thus suggest that the variable be used in the conditional for the final print statement.
2020-11-11 21:33:13 +08:00

45 lines
1.4 KiB
Python

# Set the disable flag
# Import required modules
import sys
import os
import builtins
import fileinput
import configparser
# Get the absolute filepath
config_path = os.path.dirname(os.path.abspath(__file__)) + "/../config.ini"
# Read config from disk
config = configparser.ConfigParser()
config.read(config_path)
# Check if enough arguments have been passed
if builtins.howdy_args.argument is None:
print("Please add a 0 (enable) or a 1 (disable) as an argument")
sys.exit(1)
# Translate the argument to the right string
if builtins.howdy_args.argument == "1" or builtins.howdy_args.argument.lower() == "true":
out_value = "true"
elif builtins.howdy_args.argument == "0" or builtins.howdy_args.argument.lower() == "false":
out_value = "false"
else:
# Of it's not a 0 or a 1, it's invalid
print("Please only use false (enable) or true (disable) as an argument")
sys.exit(1)
# Don't do anything when the state is already the requested one
if out_value == config.get("core", "disabled"):
print("The disable option has already been set to " + out_value)
sys.exit(1)
# Loop though the config file and only replace the line containing the disable config
for line in fileinput.input([config_path], inplace=1):
print(line.replace("disabled = " + config.get("core", "disabled"), "disabled = " + out_value), end="")
# Print what we just did
if out_value == "true":
print("Howdy has been disabled")
else:
print("Howdy has been enabled")