Skip to content

Commit e158222

Browse files
committed
Boot cache dirctory
**Changes:** * When program first executes, create the `~/.cvm/cache` directory if it does not already exist * Check if a cached version of a tag name already exists yet or not
1 parent 5f42810 commit e158222

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

src/app/bootstrap.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import argparse
2+
import logging
3+
import sys
24
from typing import Optional
35

46
from argparse_color_formatter import ColorRawTextHelpFormatter
@@ -9,6 +11,7 @@
911
from src.app.commands.scan_command import ScanCommand
1012
from src.app.commands.use_command import UseCommand
1113
from src.app.helpers.helpers import colored_fore
14+
from src.app.services.cache_service import CacheService
1215

1316
COMMAND_NAME = 'cvm'
1417
COMMAND_DESC = 'Composer Version Manager\n' + colored_fore(Fore.WHITE, 'Author: @game-of-morgan (Morgan Wowk)')
@@ -22,10 +25,16 @@
2225

2326

2427
def get_command_by_name(name: str) -> Optional[Command]:
25-
return COMMANDS.get(name, None)()
28+
command = COMMANDS.get(name, None)
29+
if command is None:
30+
logging.error("Command {} not found".format(name))
31+
sys.exit(1)
32+
33+
return command()
2634

2735

2836
def run():
37+
CacheService.boot_cache()
2938
parser = argparse.ArgumentParser(
3039
colored_fore(Fore.LIGHTGREEN_EX, COMMAND_NAME),
3140
formatter_class=ColorRawTextHelpFormatter,

src/app/commands/scan_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class ScanCommand(Command):
77
NAME = 'scan'
8-
DESCRIPTION = 'Use .cvm_config from the current directory if present.'
8+
DESCRIPTION = 'If present use .cvm_config from the current or specified directory.'
99

1010
def exec(self, args: Namespace):
1111
print("Scan command executed.")

src/app/commands/use_command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class UseCommand(Command):
99
NAME = 'use'
10-
DESCRIPTION = 'Use a specific version of Composer. Defaulting to the latest-\nstable version when there is a ' \
11-
'lack of specificity.\n\n '
10+
DESCRIPTION = 'Use a specific version of Composer; Defaulting to the latest stable major version when there is a ' \
11+
'lack of specificity (ie. 2). '
1212

1313
def exec(self, args: Namespace):
1414
desired_version = args.version[0]

src/app/services/cache_service.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
import logging
3+
import sys
4+
from pathlib import Path
5+
6+
7+
class CacheService:
8+
CACHE_DIR = os.path.expanduser('~') + '/.cvm/cache'
9+
10+
@staticmethod
11+
def boot_cache():
12+
home_directory = os.path.expanduser('~')
13+
14+
if not os.access(home_directory, os.W_OK) or not os.access(home_directory, os.X_OK):
15+
logging.error('Permission to home directory is required.')
16+
sys.exit(1)
17+
18+
if not os.path.exists(CacheService.CACHE_DIR):
19+
Path(CacheService.CACHE_DIR).mkdir(parents=True, exist_ok=True)

src/app/services/composer_service.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys
2+
import os
23

4+
from src.app.services.cache_service import CacheService
35
from src.app.services.github_service import GitHubService
46
import logging
57

@@ -24,9 +26,10 @@ def tag_exists(self, desired_tag: str) -> bool:
2426

2527
return False
2628

27-
def cache_exists(self, tag_name: str) -> bool:
29+
@staticmethod
30+
def cached_version_exists(tag_name: str) -> bool:
2831

29-
return True
32+
return os.path.exists(CacheService.CACHE_DIR + "/{}".format(tag_name))
3033

3134
def install_version(self, tag_name: str) -> bool:
3235
if not self.tag_exists(tag_name):
@@ -38,7 +41,7 @@ def install_version(self, tag_name: str) -> bool:
3841
return True
3942

4043
def use_version(self, tag_name: str, check_exists: bool = True) -> bool:
41-
if check_exists and not self.cache_exists(tag_name):
44+
if check_exists and not ComposerService.cached_version_exists(tag_name):
4245
return False
4346

4447
# TODO: Use cached version if exists

0 commit comments

Comments
 (0)