Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Fix synapse.config module "read" command #11145

Merged
merged 6 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix synapse.config module "read" command
`synapse.config.__main__` has the possibility to read a config item. This can be used to conveniently also validate the config is valid before trying to start Synapse.

 The "read" command broke in #10916 as it now requires passing in "server.server_name" for example.

 Also made the read command optional so one can just call this with just the confirm file reference and get a "Config parses OK" if things are ok.

Signed-off-by: Jason Robinson <[email protected]>
  • Loading branch information
jaywink committed Oct 21, 2021
commit 38abbe0889c9a77fbd2d3422b5197140515944bc
1 change: 1 addition & 0 deletions changelog.d/11145.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix synapse.config module "read" command.
28 changes: 18 additions & 10 deletions synapse/config/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2015, 2016 OpenMarket Ltd
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,18 +19,25 @@

from synapse.config.homeserver import HomeServerConfig

action = sys.argv[1]
action = sys.argv[1] if len(sys.argv) > 1 and sys.argv[1] == "read" else None
load_config_args = sys.argv[3:] if action else sys.argv[1:]

try:
config = HomeServerConfig.load_config("", load_config_args)
except ConfigError as e:
sys.stderr.write("\n" + str(e) + "\n")
sys.exit(1)

print("Config parses OK!")

if action == "read":
key = sys.argv[2]
try:
config = HomeServerConfig.load_config("", sys.argv[3:])
except ConfigError as e:
sys.stderr.write("\n" + str(e) + "\n")
sys.exit(1)
key_parts = key.split(".")

print(getattr(config, key))
value = config
while len(key_parts):
value = getattr(value, key_parts[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're here, could you add some error handling please? So that the user isn't met with a stacktrace but rather something like "No server_name key could be found in the provided configuration file."

key_parts.pop(0)

print(f"{key}: {value}")
sys.exit(0)
else:
sys.stderr.write("Unknown command %r\n" % (action,))
sys.exit(1)