Skip to content

Commit

Permalink
Allow broker_settings example copy from local repo (#293)
Browse files Browse the repository at this point in the history
There are cases, like github actions, where it is preferable for
broker's settings to come from a local repo clone than from github.
This change starts with a check for the local clone with a fallback to
the source on github.
  • Loading branch information
JacobCallahan committed May 20, 2024
1 parent 0c7c455 commit 1523e8b
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions broker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
from broker.exceptions import ConfigurationError


def init_settings(settings_path, interactive=False):
def init_settings(settings_path, source, interactive=False, is_url=False):
"""Initialize the broker settings file."""
raw_url = (
"https://raw.githubusercontent.com/SatelliteQE/broker/master/broker_settings.yaml.example"
)
proceed = not False
if interactive:
try:
proceed = (
click.prompt(
f"Download example file from GitHub?\n{raw_url}",
f"Get example file from {source}?\n{source}",
type=click.Choice(["y", "n"]),
default="y",
)
Expand All @@ -41,12 +38,16 @@ def init_settings(settings_path, interactive=False):
global INTERACTIVE_MODE
proceed, INTERACTIVE_MODE = True, False
if proceed:
# download example file from github
import requests

click.echo(f"Downloading example file from: {raw_url}")
raw_file = requests.get(raw_url, timeout=60)
settings_path.write_text(raw_file.text)
# get example file from source
if is_url:
import requests

click.echo(f"Downloading example file from: {source}")
raw_file = requests.get(source, timeout=60)
settings_path.write_text(raw_file.text)
else:
example_file = source.read_text()
settings_path.write_text(example_file)
if INTERACTIVE_MODE:
try:
click.edit(filename=str(settings_path.absolute()))
Expand All @@ -55,8 +56,23 @@ def init_settings(settings_path, interactive=False):
f"Please edit the file {settings_path.absolute()} and add your settings.",
fg="yellow",
)
else:
raise ConfigurationError(f"Broker settings file not found at {settings_path.absolute()}.")
return True


def init_settings_from_github(settings_path, interactive=False):
"""Initialize the broker settings file."""
raw_url = (
"https://raw.githubusercontent.com/SatelliteQE/broker/master/broker_settings.yaml.example"
)
return init_settings(settings_path, raw_url, interactive, is_url=True)


def init_settings_from_local_repo(settings_path, interactive=False):
"""Initialize the broker settings file."""
example_path = Path(__file__).parent.parent.joinpath("broker_settings.yaml.example")
if not example_path.exists():
return
return init_settings(settings_path, example_path, interactive)


INTERACTIVE_MODE = False
Expand Down Expand Up @@ -84,7 +100,10 @@ def init_settings(settings_path, interactive=False):

if not settings_path.exists():
click.secho(f"Broker settings file not found at {settings_path.absolute()}.", fg="red")
init_settings(settings_path, interactive=INTERACTIVE_MODE)
if not (success := init_settings_from_local_repo(settings_path, interactive=INTERACTIVE_MODE)):
success = init_settings_from_github(settings_path, interactive=INTERACTIVE_MODE)
if not success:
raise ConfigurationError(f"Broker settings file not found at {settings_path.absolute()}.")

validators = [
Validator("HOST_USERNAME", default="root"),
Expand Down

0 comments on commit 1523e8b

Please sign in to comment.