Skip to content

Commit d568509

Browse files
Cristianraymondbutcher
authored andcommitted
Add GitHub token (claranet#4)
* add github token support * clean empty lines * remove param
1 parent 48b4625 commit d568509

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

terrafile/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ def get_source_from_registry(source, version):
3232
sys.exit(1)
3333

3434

35+
def add_github_token(github_download_url,token):
36+
github_repo_url_pattern = re.compile('.*github.com/(.*)/(.*)\.git')
37+
match = github_repo_url_pattern.match(github_download_url)
38+
url = github_download_url
39+
if match:
40+
user, repo = match.groups()
41+
url = 'https://{}@github.com/{}/{}.git'.format(token, user, repo)
42+
return url
43+
44+
3545
def run(*args, **kwargs):
3646
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs)
3747
stdout, stderr = proc.communicate()
@@ -112,6 +122,9 @@ def update_modules(path):
112122
print('Fetched {}/{}'.format(module_path_name, name))
113123
continue
114124

125+
# add token to tthe source url if exists
126+
if 'GITHUB_TOKEN' in os.environ:
127+
source = add_github_token(source, os.getenv('GITHUB_TOKEN'))
115128
# Delete the old directory and clone it from scratch.
116129
print('Fetching {}/{}'.format(module_path_name, name))
117130
shutil.rmtree(target, ignore_errors=True)

0 commit comments

Comments
 (0)