__main__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2021 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. from synapse.config._base import ConfigError
  17. from synapse.config.homeserver import HomeServerConfig
  18. def main(args):
  19. action = args[1] if len(args) > 1 and args[1] == "read" else None
  20. # If we're reading a key in the config file, then `args[1]` will be `read` and `args[2]`
  21. # will be the key to read.
  22. # We'll want to rework this code if we want to support more actions than just `read`.
  23. load_config_args = args[3:] if action else args[1:]
  24. try:
  25. config = HomeServerConfig.load_config("", load_config_args)
  26. except ConfigError as e:
  27. sys.stderr.write("\n" + str(e) + "\n")
  28. sys.exit(1)
  29. print("Config parses OK!")
  30. if action == "read":
  31. key = args[2]
  32. key_parts = key.split(".")
  33. value = config
  34. try:
  35. while len(key_parts):
  36. value = getattr(value, key_parts[0])
  37. key_parts.pop(0)
  38. print(f"\n{key}: {value}")
  39. except AttributeError:
  40. print(
  41. f"\nNo '{key}' key could be found in the provided configuration file."
  42. )
  43. sys.exit(1)
  44. if __name__ == "__main__":
  45. main(sys.argv)