|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import importlib.metadata |
3 | 3 | import json |
4 | | -import logging |
5 | 4 | import os |
6 | 5 | import sys |
7 | 6 | import urllib.request |
8 | 7 |
|
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 | +) |
10 | 14 |
|
11 | 15 | VERSION = importlib.metadata.version("ctf-script") |
12 | 16 |
|
13 | 17 | if len(sys.argv) > 1 and sys.argv[1] == "version": |
14 | 18 | print(VERSION) |
15 | 19 | exit(code=0) |
16 | 20 |
|
| 21 | + |
17 | 22 | ENV = {} |
18 | 23 | for k, v in os.environ.items(): |
19 | 24 | ENV[k] = v |
20 | 25 |
|
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() |
24 | 36 |
|
25 | 37 |
|
26 | 38 | def check_tool_version() -> None: |
@@ -56,40 +68,10 @@ def check_tool_version() -> None: |
56 | 68 | LOG.debug("Script is up to date.") |
57 | 69 | case -1: |
58 | 70 | 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." |
60 | 72 | ) |
61 | 73 | if (input("Do you want to continue? [y/N] ").lower() or "n") == "n": |
62 | 74 | exit(code=0) |
63 | 75 |
|
64 | 76 |
|
65 | 77 | 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