Skip to content

Commit fc5ff0f

Browse files
committed
Scan should recursively check parent directories
**Changes:** * Recursively check parent directories for `.cvm_config` file
1 parent 793ad53 commit fc5ff0f

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

cvm/commands/scan_command.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from argparse import Action, Namespace
2+
from typing import Optional
23

4+
from colorama import Fore
35
from cvm.commands.command import Command
6+
from cvm.helpers.cli import warning
7+
from cvm.helpers.fs import find_file_in_parent
8+
from cvm.services.application_service import ApplicationService
49
from cvm.services.composer_service import ComposerService
510
from cvm.services.config_service import ConfigService
611
from cvm.services.github_service import GitHubService
7-
from cvm.services.application_service import ApplicationService
8-
from cvm.helpers.cli import warning
9-
from colorama import Fore
1012

1113

1214
class ScanCommand(Command):
@@ -15,9 +17,10 @@ class ScanCommand(Command):
1517

1618
def exec(self, args: Namespace):
1719
version = None
20+
config_file = ConfigService.find()
1821

19-
if ConfigService.exists():
20-
version = self._check_local()
22+
if config_file is not None:
23+
version = self._check_local(config_file)
2124
else:
2225
version = self._check_global()
2326

@@ -26,13 +29,12 @@ def exec(self, args: Namespace):
2629

2730
github_service = GitHubService('composer', 'composer')
2831
composer_service = ComposerService(github_service)
29-
3032
updated_path = composer_service.use_version(version, False)
3133

3234
print(f"export PATH=\"{updated_path}\"; echo Updated path.;")
3335

34-
def _check_local(self):
35-
data = ConfigService.read()
36+
def _check_local(self, config_file: str) -> Optional[str]:
37+
data = ConfigService.read(config_file)
3638
if not ConfigService.validate(data):
3739
msg = warning(".cvm_config format in current directory is invalid.")
3840
print(f"echo \"{msg}\"")
@@ -41,7 +43,7 @@ def _check_local(self):
4143

4244
return data['requires']
4345

44-
def _check_global(self):
46+
def _check_global(self) -> Optional[str]:
4547
application_service = ApplicationService()
4648

4749
return application_service.get('global')

cvm/helpers/fs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pathlib
2+
from typing import Optional
3+
4+
5+
def find_file_in_parent(filename: str, recursive: bool=False, current_path: Optional[pathlib.Path]=None) -> Optional[pathlib.Path]:
6+
if current_path is None:
7+
current_path = pathlib.Path.cwd()
8+
9+
file_path = current_path / filename
10+
11+
if file_path.exists():
12+
return file_path
13+
14+
if recursive and str(current_path) != '/':
15+
return find_file_in_parent(filename, recursive=True, current_path=current_path.parent)
16+
17+
return None

cvm/services/config_service.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import json
22
import pathlib
3+
from typing import Optional
4+
5+
from cvm.helpers.fs import find_file_in_parent
36

47

58
class ConfigService:
@@ -11,8 +14,12 @@ def exists() -> bool:
1114
return ConfigService.CONFIG_INPUT.exists()
1215

1316
@staticmethod
14-
def read():
15-
return json.load(ConfigService.CONFIG_INPUT.open('r'))
17+
def find() -> Optional[pathlib.Path]:
18+
return find_file_in_parent(ConfigService.FILENAME, recursive=True)
19+
20+
@staticmethod
21+
def read(config_file: pathlib.Path):
22+
return json.load(config_file.open('r'))
1623

1724
@staticmethod
1825
def validate(data) -> bool:

0 commit comments

Comments
 (0)