11from __future__ import annotations
22
3+ import urllib .request
34from dataclasses import dataclass
45from pathlib import Path
56
67import click
8+ import requests
79
810from ckit .config .common import get_global_commands_dir
911
6062
6163@dataclass
6264class 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 ]
0 commit comments