|
14 | 14 | from ctfcli.cli.templates import Templates |
15 | 15 | from ctfcli.cli.pages import Pages |
16 | 16 | from ctfcli.utils.plugins import get_plugin_dir |
| 17 | +from ctfcli.utils.git import check_if_dir_is_inside_git_repo |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class CTFCLI(object): |
20 | | - def init(self, no_config=False): |
| 21 | + def init(self, directory=None, no_config=False, no_git=False): |
| 22 | + # Create our event directory if requested and use it as our base directory |
| 23 | + if directory: |
| 24 | + path = Path(directory) |
| 25 | + path.mkdir() |
| 26 | + click.secho(f"Created empty directory in {path.absolute()}", fg="green") |
| 27 | + else: |
| 28 | + path = Path(".") |
| 29 | + |
| 30 | + # Get variables from user |
21 | 31 | ctf_url = click.prompt( |
22 | 32 | "Please enter CTFd instance URL", default="", show_default=False |
23 | 33 | ) |
24 | 34 | ctf_token = click.prompt( |
25 | 35 | "Please enter CTFd Admin Access Token", default="", show_default=False |
26 | 36 | ) |
| 37 | + # Confirm information with user |
27 | 38 | if ( |
28 | 39 | click.confirm(f"Do you want to continue with {ctf_url} and {ctf_token}") |
29 | 40 | is False |
30 | 41 | ): |
31 | 42 | click.echo("Aborted!") |
32 | 43 | return |
33 | 44 |
|
34 | | - if Path(".ctf").exists(): |
| 45 | + # Avoid colliding with existing .ctf directory |
| 46 | + if (path / ".ctf").exists(): |
35 | 47 | click.secho(".ctf/ folder already exists. Aborting!", fg="red") |
36 | 48 | return |
37 | 49 |
|
38 | | - os.mkdir(".ctf") |
| 50 | + # Create .ctf directory |
| 51 | + (path / ".ctf").mkdir() |
39 | 52 |
|
| 53 | + # Create initial .ctf/config file |
40 | 54 | config = configparser.ConfigParser() |
41 | 55 | config["config"] = {"url": ctf_url, "access_token": ctf_token} |
42 | 56 | config["challenges"] = {} |
43 | | - |
44 | | - with open(".ctf/config", "a+") as f: |
| 57 | + with (path / ".ctf" / "config").open(mode="a+") as f: |
45 | 58 | config.write(f) |
46 | 59 |
|
47 | | - subprocess.call(["git", "init"]) |
| 60 | + # Create a git repo in the event folder |
| 61 | + if check_if_dir_is_inside_git_repo(dir=path.absolute()) is True: |
| 62 | + click.secho("Already in git repo. Skipping git init.", fg="yellow") |
| 63 | + elif no_git is True: |
| 64 | + click.secho("Skipping git init.", fg="yellow") |
| 65 | + else: |
| 66 | + click.secho(f"Creating git repo in {path.absolute()}", fg="green") |
| 67 | + subprocess.call(["git", "init", str(path)]) |
48 | 68 |
|
49 | 69 | def config(self): |
50 | 70 | return COMMANDS.get("config") |
|
0 commit comments