Skip to content

Commit c3227f0

Browse files
authored
Added option to download the config files (#17)
* Added method to download global config files * Added requests as a dependency
1 parent 0fc3d3c commit c3227f0

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

ckit/cli.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ def ckit(ctx, local_only: bool, global_only: bool) -> None:
6868
Core(config).run()
6969

7070

71+
@click.option(
72+
"--download-global-defaults",
73+
is_flag=True,
74+
help="""Boolean flag. If set, instead of creating an example ckit.yaml in the global configuration directory, will prompt to download the files
75+
from the `ckit-files` repository to the global configuration repository instead.""",
76+
)
77+
@click.option(
78+
"--skip-local",
79+
is_flag=True,
80+
help="Boolean flag to indicate that we do not want to initialize a local configuration file.",
81+
)
82+
@click.option(
83+
"--skip-global",
84+
is_flag=True,
85+
help="Boolean flag to indicate that we do not want to initialize global configuration files.",
86+
)
7187
@ckit.command()
72-
def init():
73-
ConfigFilesInitiator().init()
88+
def init(download_global_defaults: bool, skip_local: bool, skip_global: bool):
89+
ConfigFilesInitiator(
90+
download_global_defaults=download_global_defaults, skip_global=skip_global, skip_local=skip_local
91+
).init()

ckit/config/config_files_initiatior.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

3+
import urllib.request
34
from dataclasses import dataclass
45
from pathlib import Path
56

67
import click
8+
import requests
79

810
from ckit.config.common import get_global_commands_dir
911

@@ -60,13 +62,26 @@
6062

6163
@dataclass
6264
class ConfigFilesInitiator:
65+
"""
66+
Args:
67+
download_global_defaults (bool): If False, simply prompt to create ckit.yaml in the global config directory. If True,
68+
prompt to download files from the ckit-files repository.
69+
skip_global: Skip initializing the global configuration directory.
70+
skip_local: Skip initializing the local configuration file.
71+
"""
72+
73+
download_global_defaults: bool = False
74+
skip_global: bool = False
75+
skip_local: bool = False
76+
6377
def init(self):
64-
"""
65-
Check for the existence of local- and global ckit.yaml files, and if they do not exist,
66-
ask the user if they should be created.
67-
"""
68-
self._init_global()
69-
self._init_local()
78+
if not self.skip_global:
79+
if self.download_global_defaults:
80+
self._init_global_by_downloading_config_files()
81+
else:
82+
self._init_global()
83+
if not self.skip_local:
84+
self._init_local()
7085

7186
def _init_local(self):
7287
"""
@@ -85,11 +100,31 @@ def _init_global(self):
85100
Check if there is a global ckit.yaml file. If it does not exist, ask the user if it should be created, and
86101
if so, create it.
87102
"""
88-
global_commands_file = get_global_commands_dir() / "ckit.yaml"
103+
global_commands_dir = get_global_commands_dir()
104+
global_commands_file = global_commands_dir / "ckit.yaml"
89105
if global_commands_file.exists():
90106
click.echo(f"{global_commands_file} already exists.")
91107
else:
92-
if click.confirm(f"Create a global ckit.yaml file in {global_commands_file.parent}?", default=True):
93-
global_commands_file.parent.mkdir(parents=True, exist_ok=True)
108+
if click.confirm(f"Create a global ckit.yaml file in {global_commands_dir}?", default=True):
109+
global_commands_dir.mkdir(parents=True, exist_ok=True)
94110
with open(global_commands_file, "w") as f:
95111
f.write(COMMANDS_YAML_DEFAULT)
112+
113+
def _init_global_by_downloading_config_files(self):
114+
"""
115+
Download config files from the `ckit-files` repository.
116+
"""
117+
global_commands_dir = get_global_commands_dir()
118+
config_files = self._list_config_files_from_github()
119+
if click.confirm(
120+
f"Download the files {[file['name'] for file in config_files]} and place them in {global_commands_dir}?",
121+
default=True,
122+
):
123+
global_commands_dir.mkdir(parents=True, exist_ok=True)
124+
for file in config_files:
125+
urllib.request.urlretrieve(file["url"], global_commands_dir / file["name"])
126+
127+
def _list_config_files_from_github(self):
128+
url = "https://api.github.com/repos/fpgmaas/ckit-files/contents/config-files"
129+
response_json = requests.get(url).json()
130+
return [{"name": x["name"], "url": x["download_url"]} for x in response_json]

poetry.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ python = ">=3.8,<3.12"
1818
click = "^8.1.3"
1919
pyyaml = "^6.0"
2020
blessed = "^1.19.1"
21+
requests = "^2.28.2"
2122

2223
[tool.poetry.group.dev.dependencies]
2324
pytest = "^7.2.0"

0 commit comments

Comments
 (0)