Skip to content

Commit 5f42810

Browse files
committed
Service layer and list tags from GitHub
**Changes:** * Install and use command business logic is near complete * Request for a list of tags from GitHub is now working * Clean abstraction of install vs use
1 parent 3d5b054 commit 5f42810

File tree

8 files changed

+107
-12
lines changed

8 files changed

+107
-12
lines changed

src/app/bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def run():
4242
args = parser.parse_args()
4343

4444
command = get_command_by_name(args.command)
45-
command.exec()
45+
command.exec(args)

src/app/commands/command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from abc import ABC, abstractmethod
2-
from argparse import Action
2+
from argparse import Action, Namespace
33

44

55
class Command(ABC):
66
@abstractmethod
7-
def exec(self) -> None:
7+
def exec(self, args: Namespace) -> None:
88
pass
99

1010
@staticmethod

src/app/commands/install_command.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
from argparse import Action
1+
from argparse import Action, Namespace
22

33
from src.app.commands.command import Command
4+
from src.app.services.composer_service import ComposerService
5+
from src.app.services.github_service import GitHubService
46

57

68
class InstallCommand(Command):
79
NAME = 'install'
810
DESCRIPTION = 'Install a version of Composer without using it.'
911

10-
def exec(self):
11-
print("Install command executed.")
12+
def exec(self, args: Namespace):
13+
desired_version = args.version[0]
14+
15+
github_service = GitHubService('composer', 'composer')
16+
composer_service = ComposerService(github_service)
17+
18+
composer_service.install_version(desired_version)
1219

1320
@staticmethod
1421
def define_signature(parser: Action):

src/app/commands/scan_command.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from argparse import Action
1+
from argparse import Action, Namespace
22

33
from src.app.commands.command import Command
44

@@ -7,8 +7,9 @@ class ScanCommand(Command):
77
NAME = 'scan'
88
DESCRIPTION = 'Use .cvm_config from the current directory if present.'
99

10-
def exec(self):
10+
def exec(self, args: Namespace):
1111
print("Scan command executed.")
12+
print(args)
1213

1314
@staticmethod
1415
def define_signature(parser: Action):

src/app/commands/use_command.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
from argparse import Action
1+
from argparse import Action, Namespace
22

33
from src.app.commands.command import Command
4+
from src.app.services.composer_service import ComposerService
5+
from src.app.services.github_service import GitHubService
46

57

68
class UseCommand(Command):
79
NAME = 'use'
8-
DESCRIPTION = 'Use a specific version of Composer. Defaulting to the latest-\nstable version when there is a lack of specificity.\n\n'
10+
DESCRIPTION = 'Use a specific version of Composer. Defaulting to the latest-\nstable version when there is a ' \
11+
'lack of specificity.\n\n '
912

10-
def exec(self):
11-
print("Use command executed.")
13+
def exec(self, args: Namespace):
14+
desired_version = args.version[0]
15+
16+
github_service = GitHubService('composer', 'composer')
17+
composer_service = ComposerService(github_service)
18+
19+
existed = composer_service.use_version(desired_version)
20+
if not existed:
21+
composer_service.install_version(desired_version)
22+
composer_service.use_version(desired_version, False)
1223

1324
@staticmethod
1425
def define_signature(parser: Action):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
3+
from src.app.services.github_service import GitHubService
4+
import logging
5+
6+
7+
class ComposerService:
8+
def __init__(self, github_service: GitHubService):
9+
self.github_service = github_service
10+
self.tags = None
11+
12+
def list_tags(self):
13+
if self.tags is None:
14+
self.tags = self.github_service.list_tags()
15+
16+
return self.tags
17+
18+
def tag_exists(self, desired_tag: str) -> bool:
19+
github_tags = self.list_tags()
20+
21+
for tag_object in github_tags:
22+
if tag_object.get('name') == desired_tag:
23+
return True
24+
25+
return False
26+
27+
def cache_exists(self, tag_name: str) -> bool:
28+
29+
return True
30+
31+
def install_version(self, tag_name: str) -> bool:
32+
if not self.tag_exists(tag_name):
33+
logging.error("Tag {} does not exist.".format(tag_name))
34+
sys.exit(1)
35+
36+
# TODO: Download version into cache directory
37+
38+
return True
39+
40+
def use_version(self, tag_name: str, check_exists: bool = True) -> bool:
41+
if check_exists and not self.cache_exists(tag_name):
42+
return False
43+
44+
# TODO: Use cached version if exists
45+
46+
return True

src/app/services/github_service.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import logging
2+
import sys
3+
4+
import requests
5+
6+
7+
class GitHubService:
8+
def __init__(self, owner_id: str, repo_id: str):
9+
self.owner_id = owner_id
10+
self.repo_id = repo_id
11+
12+
def list_tags(self) -> requests.Response:
13+
headers = {'Accept': 'application/vnd.github.v3+json'}
14+
r = requests.get(
15+
"https://api.github.com/repos/{}/{}/tags".format(self.owner_id, self.repo_id),
16+
headers=headers
17+
)
18+
19+
if r.status_code >= 400:
20+
logging.error('Failed to fetch available tags from GitHub.')
21+
sys.exit(1)
22+
23+
json = r.json()
24+
25+
return json

src/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
ansicolors==1.1.8
22
argparse-color-formatter==1.2.2.post2
3+
certifi==2020.12.5
4+
chardet==4.0.0
35
colorama==0.4.4
6+
idna==2.10
47
mypy==0.790
58
mypy-extensions==0.4.3
9+
requests==2.25.1
610
six==1.15.0
711
typed-ast==1.4.2
812
typing-extensions==3.7.4.3
13+
urllib3==1.26.2

0 commit comments

Comments
 (0)