Skip to content

Commit e89e691

Browse files
authored
Merge pull request #24 from nsec/refactor
Refactor each command into their own file.
2 parents 85663ba + dce11b9 commit e89e691

21 files changed

+1849
-1709
lines changed

.github/workflows/tests.yml

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ jobs:
3232
echo "Pulled files:"
3333
{ git lfs ls-files | grep -E '[a-f0-9]{10}\s\*'; } || true
3434
35+
- name: Install python dependencies
36+
run: |
37+
pip install -e .
38+
39+
- name: ctf init
40+
run: |
41+
ctf init test-ctf
42+
43+
- name: Copy CTF files
44+
run: |
45+
mkdir -p test-ctf/challenges
46+
cp -r ./challenges test-ctf/
47+
ls -al test-ctf/
48+
ls -al test-ctf/challenges
49+
50+
- name: ctf version
51+
working-directory: test-ctf
52+
run: |
53+
ctf version
54+
55+
- name: CTF stats
56+
# Run this in the test-ctf directory
57+
working-directory: test-ctf
58+
run: |
59+
ctf stats
60+
61+
- name: CTF list
62+
# Run this in the test-ctf directory
63+
working-directory: test-ctf
64+
run: |
65+
ctf list
66+
3567
- name: Remove docker
3668
run: |
3769
sudo apt-get autopurge -y moby-containerd docker uidmap
@@ -104,38 +136,11 @@ jobs:
104136
./install-opentofu.sh --install-method deb
105137
rm -f install-opentofu.sh
106138
107-
- name: Install python dependencies
108-
run: |
109-
pip install -e .
110-
111-
- name: ctf init
112-
run: |
113-
ctf init test-ctf
114-
115-
- name: Copy CTF files
116-
run: |
117-
mkdir -p test-ctf/challenges
118-
cp -r ./challenges test-ctf/
119-
ls -al test-ctf/
120-
ls -al test-ctf/challenges
121-
122139
- name: Validate CTF structure
123140
# Run this in the test-ctf directory
124141
working-directory: test-ctf
125142
run: |
126143
ctf validate
127-
128-
- name: CTF stats
129-
# Run this in the test-ctf directory
130-
working-directory: test-ctf
131-
run: |
132-
ctf stats
133-
134-
- name: CTF list
135-
# Run this in the test-ctf directory
136-
working-directory: test-ctf
137-
run: |
138-
ctf list
139144
140145
- name: Deployment check
141146
working-directory: test-ctf

ctf/__init__.py

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
#!/usr/bin/env python3
22
import importlib.metadata
33
import json
4-
import logging
54
import os
65
import sys
76
import urllib.request
87

9-
import coloredlogs
8+
from ctf.logger import LOG
9+
from ctf.utils import (
10+
find_ctf_root_directory,
11+
get_ctf_script_schemas_directory,
12+
get_ctf_script_templates_directory,
13+
)
1014

1115
VERSION = importlib.metadata.version("ctf-script")
1216

1317
if len(sys.argv) > 1 and sys.argv[1] == "version":
1418
print(VERSION)
1519
exit(code=0)
1620

21+
1722
ENV = {}
1823
for k, v in os.environ.items():
1924
ENV[k] = v
2025

21-
LOG = logging.getLogger()
22-
LOG.setLevel(level=logging.DEBUG)
23-
coloredlogs.install(level="DEBUG", logger=LOG)
26+
match sys.argv[1] if len(sys.argv) > 1 else "":
27+
case "init":
28+
CTF_ROOT_DIRECTORY = os.path.join(os.getcwd(), ".")
29+
case "version":
30+
CTF_ROOT_DIRECTORY = ""
31+
case _:
32+
CTF_ROOT_DIRECTORY = find_ctf_root_directory()
33+
34+
TEMPLATES_ROOT_DIRECTORY = get_ctf_script_templates_directory()
35+
SCHEMAS_ROOT_DIRECTORY = get_ctf_script_schemas_directory()
2436

2537

2638
def check_tool_version() -> None:
@@ -56,40 +68,10 @@ def check_tool_version() -> None:
5668
LOG.debug("Script is up to date.")
5769
case -1:
5870
LOG.warning(
59-
"Script is outdated. Please update to the latest release before continuing."
71+
f"Script is outdated (current: {VERSION}, upstream: {latest_version}). Please update to the latest release before continuing."
6072
)
6173
if (input("Do you want to continue? [y/N] ").lower() or "n") == "n":
6274
exit(code=0)
6375

6476

6577
check_tool_version()
66-
67-
68-
def find_ctf_root_directory() -> str:
69-
path = os.path.join(os.getcwd(), ".")
70-
71-
while path != (path := os.path.dirname(p=path)):
72-
dir = os.listdir(path=path)
73-
74-
if ".deploy" not in dir:
75-
continue
76-
if "challenges" not in dir:
77-
continue
78-
break
79-
80-
if path == "/":
81-
if "CTF_ROOT_DIR" not in os.environ:
82-
LOG.critical(
83-
msg='Could not automatically find the root directory nor the "CTF_ROOT_DIR" environment variable. To initialize a new root directory, use `ctf init [path]`'
84-
)
85-
exit(1)
86-
return os.environ.get("CTF_ROOT_DIR", default=".")
87-
88-
LOG.debug(msg=f"Found root directory: {path}")
89-
return path
90-
91-
92-
if len(sys.argv) > 1 and sys.argv[1] == "init":
93-
CTF_ROOT_DIRECTORY = os.path.join(os.getcwd(), ".")
94-
else:
95-
CTF_ROOT_DIRECTORY = find_ctf_root_directory()

0 commit comments

Comments
 (0)