Skip to content

Commit

Permalink
Add --[no-]validation flag for tljh-config
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdnbradford committed Mar 20, 2024
1 parent 1f7d6d1 commit 48fe440
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions tljh/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def remove_item_from_config(config, property_path, value):
return config_copy


def validate_config(config):
def validate_config(config, validate):
"""
Validate changes to the config with tljh-config against the schema
"""
Expand All @@ -165,8 +165,13 @@ def validate_config(config):
try:
jsonschema.validate(instance=config, schema=config_schema)
except jsonschema.exceptions.ValidationError as e:
print(e.message)
exit()
if validate:
print(
f"Config validation error: {e.message}.\n"
"You can still apply this change without validation by re-running your command with the --no-validate flag.\n"
"If you think this validation error is incorrect, please report it to https://github.com/jupyterhub/the-littlest-jupyterhub/issues."
)
exit()


def show_config(config_path):
Expand All @@ -182,7 +187,7 @@ def show_config(config_path):
yaml.dump(config, sys.stdout)


def set_config_value(config_path, key_path, value):
def set_config_value(config_path, key_path, value, validate):
"""
Set key at key_path in config_path to value
"""
Expand All @@ -194,13 +199,13 @@ def set_config_value(config_path, key_path, value):
config = {}
config = set_item_in_config(config, key_path, value)

validate_config(config)
validate_config(config, validate)

with open(config_path, "w") as f:
yaml.dump(config, f)


def unset_config_value(config_path, key_path):
def unset_config_value(config_path, key_path, validate):
"""
Unset key at key_path in config_path
"""
Expand All @@ -212,13 +217,13 @@ def unset_config_value(config_path, key_path):
config = {}

config = unset_item_from_config(config, key_path)
validate_config(config)
validate_config(config, validate)

with open(config_path, "w") as f:
yaml.dump(config, f)


def add_config_value(config_path, key_path, value):
def add_config_value(config_path, key_path, value, validate):
"""
Add value to list at key_path
"""
Expand All @@ -230,13 +235,13 @@ def add_config_value(config_path, key_path, value):
config = {}

config = add_item_to_config(config, key_path, value)
validate_config(config)
validate_config(config, validate)

with open(config_path, "w") as f:
yaml.dump(config, f)


def remove_config_value(config_path, key_path, value):
def remove_config_value(config_path, key_path, value, validate):
"""
Remove value from list at key_path
"""
Expand All @@ -248,7 +253,7 @@ def remove_config_value(config_path, key_path, value):
config = {}

config = remove_item_from_config(config, key_path, value)
validate_config(config)
validate_config(config, validate)

with open(config_path, "w") as f:
yaml.dump(config, f)
Expand Down Expand Up @@ -351,6 +356,12 @@ def main(argv=None):
argparser.add_argument(
"--config-path", default=CONFIG_FILE, help="Path to TLJH config.yaml file"
)
argparser.add_argument(
"--validate",
action=argparse.BooleanOptionalAction,
help="Validate the TLJH config",
)

subparsers = argparser.add_subparsers(dest="action")

show_parser = subparsers.add_parser("show", help="Show current configuration")
Expand Down Expand Up @@ -395,16 +406,25 @@ def main(argv=None):

args = argparser.parse_args(argv)

if args.validate == None:
args.validate = True

if args.action == "show":
show_config(args.config_path)
elif args.action == "set":
set_config_value(args.config_path, args.key_path, parse_value(args.value))
set_config_value(
args.config_path, args.key_path, parse_value(args.value), args.validate
)
elif args.action == "unset":
unset_config_value(args.config_path, args.key_path)
unset_config_value(args.config_path, args.key_path, args.validate)
elif args.action == "add-item":
add_config_value(args.config_path, args.key_path, parse_value(args.value))
add_config_value(
args.config_path, args.key_path, parse_value(args.value), args.validate
)
elif args.action == "remove-item":
remove_config_value(args.config_path, args.key_path, parse_value(args.value))
remove_config_value(
args.config_path, args.key_path, parse_value(args.value), args.validate
)
elif args.action == "reload":
reload_component(args.component)
else:
Expand Down

0 comments on commit 48fe440

Please sign in to comment.