Skip to content

Commit

Permalink
Added option to download the config files (#17)
Browse files Browse the repository at this point in the history
* Added method to download global config files
* Added requests as a dependency
  • Loading branch information
fpgmaas authored Mar 25, 2023
1 parent 0fc3d3c commit c3227f0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
22 changes: 20 additions & 2 deletions ckit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ def ckit(ctx, local_only: bool, global_only: bool) -> None:
Core(config).run()


@click.option(
"--download-global-defaults",
is_flag=True,
help="""Boolean flag. If set, instead of creating an example ckit.yaml in the global configuration directory, will prompt to download the files
from the `ckit-files` repository to the global configuration repository instead.""",
)
@click.option(
"--skip-local",
is_flag=True,
help="Boolean flag to indicate that we do not want to initialize a local configuration file.",
)
@click.option(
"--skip-global",
is_flag=True,
help="Boolean flag to indicate that we do not want to initialize global configuration files.",
)
@ckit.command()
def init():
ConfigFilesInitiator().init()
def init(download_global_defaults: bool, skip_local: bool, skip_global: bool):
ConfigFilesInitiator(
download_global_defaults=download_global_defaults, skip_global=skip_global, skip_local=skip_local
).init()
53 changes: 44 additions & 9 deletions ckit/config/config_files_initiatior.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import urllib.request
from dataclasses import dataclass
from pathlib import Path

import click
import requests

from ckit.config.common import get_global_commands_dir

Expand Down Expand Up @@ -60,13 +62,26 @@

@dataclass
class ConfigFilesInitiator:
"""
Args:
download_global_defaults (bool): If False, simply prompt to create ckit.yaml in the global config directory. If True,
prompt to download files from the ckit-files repository.
skip_global: Skip initializing the global configuration directory.
skip_local: Skip initializing the local configuration file.
"""

download_global_defaults: bool = False
skip_global: bool = False
skip_local: bool = False

def init(self):
"""
Check for the existence of local- and global ckit.yaml files, and if they do not exist,
ask the user if they should be created.
"""
self._init_global()
self._init_local()
if not self.skip_global:
if self.download_global_defaults:
self._init_global_by_downloading_config_files()
else:
self._init_global()
if not self.skip_local:
self._init_local()

def _init_local(self):
"""
Expand All @@ -85,11 +100,31 @@ def _init_global(self):
Check if there is a global ckit.yaml file. If it does not exist, ask the user if it should be created, and
if so, create it.
"""
global_commands_file = get_global_commands_dir() / "ckit.yaml"
global_commands_dir = get_global_commands_dir()
global_commands_file = global_commands_dir / "ckit.yaml"
if global_commands_file.exists():
click.echo(f"{global_commands_file} already exists.")
else:
if click.confirm(f"Create a global ckit.yaml file in {global_commands_file.parent}?", default=True):
global_commands_file.parent.mkdir(parents=True, exist_ok=True)
if click.confirm(f"Create a global ckit.yaml file in {global_commands_dir}?", default=True):
global_commands_dir.mkdir(parents=True, exist_ok=True)
with open(global_commands_file, "w") as f:
f.write(COMMANDS_YAML_DEFAULT)

def _init_global_by_downloading_config_files(self):
"""
Download config files from the `ckit-files` repository.
"""
global_commands_dir = get_global_commands_dir()
config_files = self._list_config_files_from_github()
if click.confirm(
f"Download the files {[file['name'] for file in config_files]} and place them in {global_commands_dir}?",
default=True,
):
global_commands_dir.mkdir(parents=True, exist_ok=True)
for file in config_files:
urllib.request.urlretrieve(file["url"], global_commands_dir / file["name"])

def _list_config_files_from_github(self):
url = "https://api.github.com/repos/fpgmaas/ckit-files/contents/config-files"
response_json = requests.get(url).json()
return [{"name": x["name"], "url": x["download_url"]} for x in response_json]
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ python = ">=3.8,<3.12"
click = "^8.1.3"
pyyaml = "^6.0"
blessed = "^1.19.1"
requests = "^2.28.2"

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
Expand Down

0 comments on commit c3227f0

Please sign in to comment.