File tree Expand file tree Collapse file tree 8 files changed +107
-12
lines changed
Expand file tree Collapse file tree 8 files changed +107
-12
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 11from abc import ABC , abstractmethod
2- from argparse import Action
2+ from argparse import Action , Namespace
33
44
55class Command (ABC ):
66 @abstractmethod
7- def exec (self ) -> None :
7+ def exec (self , args : Namespace ) -> None :
88 pass
99
1010 @staticmethod
Original file line number Diff line number Diff line change 1- from argparse import Action
1+ from argparse import Action , Namespace
22
33from 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
68class 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 ):
Original file line number Diff line number Diff line change 1- from argparse import Action
1+ from argparse import Action , Namespace
22
33from 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 ):
Original file line number Diff line number Diff line change 1- from argparse import Action
1+ from argparse import Action , Namespace
22
33from 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
68class UseCommand (Command ):
79 NAME = 'use'
8- DESCRIPTION = 'Use a specific version of Composer. Defaulting to the latest-\n stable version when there is a lack of specificity.\n \n '
10+ DESCRIPTION = 'Use a specific version of Composer. Defaulting to the latest-\n stable 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 ):
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11ansicolors == 1.1.8
22argparse-color-formatter == 1.2.2.post2
3+ certifi == 2020.12.5
4+ chardet == 4.0.0
35colorama == 0.4.4
6+ idna == 2.10
47mypy == 0.790
58mypy-extensions == 0.4.3
9+ requests == 2.25.1
610six == 1.15.0
711typed-ast == 1.4.2
812typing-extensions == 3.7.4.3
13+ urllib3 == 1.26.2
You can’t perform that action at this time.
0 commit comments