|
9 | 9 | from pathlib import Path |
10 | 10 | from typing import Any, Dict, List, NamedTuple, Optional, Tuple |
11 | 11 |
|
12 | | -from .code_hosting import (CodeHostingClient, CodeHostingGitConfigKeys, |
13 | | - CodeHostingSpec, OrganizationAndRepository, |
14 | | - OrganizationAndRepositoryAndGitUrl, PullRequest) |
15 | | -from .exceptions import MacheteException, UnexpectedMacheteException |
16 | | -from .git_operations import LocalBranchShortName |
17 | | -from .utils import bold, compact_dict, debug, popen_cmd, warn |
| 12 | +from git_machete import utils |
| 13 | +from git_machete.code_hosting import (CodeHostingClient, |
| 14 | + CodeHostingGitConfigKeys, |
| 15 | + CodeHostingSpec, |
| 16 | + OrganizationAndRepository, |
| 17 | + OrganizationAndRepositoryAndGitUrl, |
| 18 | + PullRequest) |
| 19 | +from git_machete.exceptions import MacheteException, UnexpectedMacheteException |
| 20 | +from git_machete.git_operations import LocalBranchShortName |
| 21 | +from git_machete.utils import bold, compact_dict, debug, popen_cmd, warn |
18 | 22 |
|
19 | 23 | GITHUB_TOKEN_ENV_VAR = 'GITHUB_TOKEN' |
20 | 24 |
|
@@ -48,19 +52,18 @@ def __get_token_from_file_in_home_directory(cls, domain: str) -> Optional["GitHu |
48 | 52 |
|
49 | 53 | if os.path.isfile(file_full_path): |
50 | 54 | debug(f" File `{file_full_path}` exists") |
51 | | - with open(file_full_path) as file: |
52 | | - # ~/.github-token is a file with a structure similar to: |
53 | | - # |
54 | | - # ghp_mytoken_for_github_com |
55 | | - # ghp_myothertoken_for_git_example_org git.example.org |
56 | | - # ghp_yetanothertoken_for_git_example_com git.example.com |
57 | | - |
58 | | - for line in file.readlines(): |
59 | | - if line.rstrip().endswith(" " + domain): |
60 | | - token = line.split(" ")[0] |
61 | | - return cls(value=token, provider=provider) |
62 | | - elif domain == GitHubClient.DEFAULT_GITHUB_DOMAIN and " " not in line.rstrip(): |
63 | | - return cls(value=line.rstrip(), provider=provider) |
| 55 | + |
| 56 | + # ~/.github-token is a file with a structure similar to: |
| 57 | + # |
| 58 | + # ghp_mytoken_for_github_com |
| 59 | + # ghp_myothertoken_for_git_example_org git.example.org |
| 60 | + # ghp_yetanothertoken_for_git_example_com git.example.com |
| 61 | + for line in utils.slurp_file(file_full_path).splitlines(): |
| 62 | + if line.rstrip().endswith(" " + domain): |
| 63 | + token = line.split(" ")[0] |
| 64 | + return cls(value=token, provider=provider) |
| 65 | + elif domain == GitHubClient.DEFAULT_GITHUB_DOMAIN and " " not in line.rstrip(): |
| 66 | + return cls(value=line.rstrip(), provider=provider) |
64 | 67 | return None |
65 | 68 |
|
66 | 69 | @classmethod |
@@ -121,26 +124,25 @@ def __get_token_from_hub(cls, domain: str) -> Optional["GitHubToken"]: |
121 | 124 | home_path: str = str(Path.home()) |
122 | 125 | config_hub_path: str = os.path.join(home_path, ".config", "hub") |
123 | 126 | if os.path.isfile(config_hub_path): |
124 | | - with open(config_hub_path) as config_hub: |
125 | | - # ~/.config/hub is a yaml file, with a structure similar to: |
126 | | - # |
127 | | - # {domain1}: |
128 | | - # - user: {username1} |
129 | | - # oauth_token: ******************* |
130 | | - # protocol: {protocol} |
131 | | - # |
132 | | - # {domain2}: |
133 | | - # - user: {username2} |
134 | | - # oauth_token: ******************* |
135 | | - # protocol: {protocol} |
136 | | - found_host = False |
137 | | - for line in config_hub.readlines(): |
138 | | - if line.rstrip() == domain + ":": |
139 | | - found_host = True |
140 | | - elif found_host and line.lstrip().startswith("oauth_token:"): |
141 | | - result = re.sub(' *oauth_token: +', '', line).rstrip().replace('"', '') |
142 | | - return cls(value=result, |
143 | | - provider=f'auth token for {domain} from `hub` GitHub CLI') |
| 127 | + # ~/.config/hub is a yaml file, with a structure similar to: |
| 128 | + # |
| 129 | + # {domain1}: |
| 130 | + # - user: {username1} |
| 131 | + # oauth_token: ******************* |
| 132 | + # protocol: {protocol} |
| 133 | + # |
| 134 | + # {domain2}: |
| 135 | + # - user: {username2} |
| 136 | + # oauth_token: ******************* |
| 137 | + # protocol: {protocol} |
| 138 | + found_host = False |
| 139 | + for line in utils.slurp_file(config_hub_path).splitlines(): |
| 140 | + if line.rstrip() == domain + ":": |
| 141 | + found_host = True |
| 142 | + elif found_host and line.lstrip().startswith("oauth_token:"): |
| 143 | + result = re.sub(' *oauth_token: +', '', line).rstrip().replace('"', '') |
| 144 | + return cls(value=result, |
| 145 | + provider=f'auth token for {domain} from `hub` GitHub CLI') |
144 | 146 | return None |
145 | 147 |
|
146 | 148 |
|
|
0 commit comments