Skip to content

Commit d1e3b45

Browse files
committed
Merge branch 'full_test' of https://github.com/nsec/ctf-script into full_test
2 parents f5845f2 + fd5e4ec commit d1e3b45

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

ctf/__main__.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,49 @@ def terraform_binary() -> str:
9393

9494

9595
def init(args: argparse.Namespace) -> None:
96-
if (
97-
os.path.isdir(os.path.join(args.path, "challenges"))
98-
or os.path.isdir(os.path.join(args.path, ".deploy"))
99-
) and not args.force:
100-
LOG.error(
101-
f"Directory {args.path} is already initialized. Use --force to overwrite."
102-
)
103-
LOG.error(args.force)
104-
exit(code=1)
96+
created_directory = False
97+
try:
98+
if not os.path.isdir(args.path):
99+
os.mkdir(args.path)
100+
LOG.info(f'Creating directory "{args.path}"')
101+
created_directory = True
102+
elif (
103+
os.path.isdir(os.path.join(args.path, "challenges"))
104+
or os.path.isdir(os.path.join(args.path, ".deploy"))
105+
) and not args.force:
106+
LOG.error(
107+
f'Directory "{args.path}" is already initialized. Use --force to overwrite.'
108+
)
109+
LOG.error(args.force)
110+
exit(code=1)
105111

106-
created_assets: list[str] = []
112+
created_assets: list[str] = []
107113

108-
try:
109114
for asset in os.listdir(p := os.path.join(TEMPLATES_ROOT_DIRECTORY, "init")):
110115
dst_asset = os.path.join(args.path, asset)
111116
if os.path.isdir(src_asset := os.path.join(p, asset)):
112117
shutil.copytree(src_asset, dst_asset, dirs_exist_ok=True)
113-
LOG.info(f"Created {dst_asset} folder")
118+
LOG.info(f'Created "{dst_asset}" folder')
114119
else:
115120
shutil.copy(src_asset, dst_asset)
116-
LOG.info(f"Created {dst_asset} file")
121+
LOG.info(f'Created "{dst_asset}" file')
117122

118123
created_assets.append(dst_asset)
119124

120125
except Exception:
121126
import traceback
122127

123-
for asset in created_assets:
124-
if os.path.isdir(asset):
125-
shutil.rmtree(asset)
126-
LOG.info(f"Removed created {asset} folder")
127-
else:
128-
os.unlink(asset)
129-
LOG.info(f"Removed created {asset} file")
128+
if created_directory:
129+
shutil.rmtree(args.path)
130+
LOG.info(f'Removed created "{args.path}" folder')
131+
else:
132+
for asset in created_assets:
133+
if os.path.isdir(asset):
134+
shutil.rmtree(asset)
135+
LOG.info(f'Removed created "{asset}" folder')
136+
else:
137+
os.unlink(asset)
138+
LOG.info(f'Removed created "{asset}" file')
130139

131140
LOG.critical(traceback.format_exc())
132141

0 commit comments

Comments
 (0)