Skip to content

Commit 5275dcf

Browse files
authored
Improve ctf init to skip git repo creation and create directories (#86)
* Adds a `--no-git` option to `ctf init` to skip git repo creation in event folder * `ctf init` will not attempt to create git repos when the event folder is in a git repo already * `ctf init <folder>` can now be used to create the event folder instead of creating the folder beforehand * Closes #34
1 parent 0fbe400 commit 5275dcf

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

ctfcli/__main__.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,57 @@
1414
from ctfcli.cli.templates import Templates
1515
from ctfcli.cli.pages import Pages
1616
from ctfcli.utils.plugins import get_plugin_dir
17+
from ctfcli.utils.git import check_if_dir_is_inside_git_repo
1718

1819

1920
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
2131
ctf_url = click.prompt(
2232
"Please enter CTFd instance URL", default="", show_default=False
2333
)
2434
ctf_token = click.prompt(
2535
"Please enter CTFd Admin Access Token", default="", show_default=False
2636
)
37+
# Confirm information with user
2738
if (
2839
click.confirm(f"Do you want to continue with {ctf_url} and {ctf_token}")
2940
is False
3041
):
3142
click.echo("Aborted!")
3243
return
3344

34-
if Path(".ctf").exists():
45+
# Avoid colliding with existing .ctf directory
46+
if (path / ".ctf").exists():
3547
click.secho(".ctf/ folder already exists. Aborting!", fg="red")
3648
return
3749

38-
os.mkdir(".ctf")
50+
# Create .ctf directory
51+
(path / ".ctf").mkdir()
3952

53+
# Create initial .ctf/config file
4054
config = configparser.ConfigParser()
4155
config["config"] = {"url": ctf_url, "access_token": ctf_token}
4256
config["challenges"] = {}
43-
44-
with open(".ctf/config", "a+") as f:
57+
with (path / ".ctf" / "config").open(mode="a+") as f:
4558
config.write(f)
4659

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)])
4868

4969
def config(self):
5070
return COMMANDS.get("config")

ctfcli/utils/git.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,25 @@ def get_git_repo_head_branch(repo):
1111
).decode()
1212
head_branch = out.split()[1]
1313
return head_branch
14+
15+
16+
def check_if_dir_is_inside_git_repo(dir=None):
17+
"""
18+
Checks whether a given directory is inside of a git repo.
19+
"""
20+
try:
21+
out = (
22+
subprocess.check_output(
23+
["git", "rev-parse", "--is-inside-work-tree"],
24+
cwd=dir,
25+
stderr=subprocess.DEVNULL,
26+
)
27+
.decode()
28+
.strip()
29+
)
30+
print(out)
31+
if out == "true":
32+
return True
33+
return False
34+
except subprocess.CalledProcessError:
35+
return False

0 commit comments

Comments
 (0)