Skip to content
Snippets Groups Projects
Commit b530cd3c authored by Leo's avatar Leo
Browse files

feat: add import command to import current profile or global

parent cda08d80
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="2f47370d-26d1-4fc6-a93f-363c8b4c6e5a" name="Changes" comment=""> <list default="true" id="2f47370d-26d1-4fc6-a93f-363c8b4c6e5a" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/Makefile" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/dist/gpm-0.1.0-py3-none-any.whl" beforeDir="false" afterPath="$PROJECT_DIR$/dist/gpm-0.1.0-py3-none-any.whl" afterDir="false" /> <change beforePath="$PROJECT_DIR$/dist/gpm-0.1.0-py3-none-any.whl" beforeDir="false" afterPath="$PROJECT_DIR$/dist/gpm-0.1.0-py3-none-any.whl" afterDir="false" />
<change beforePath="$PROJECT_DIR$/dist/gpm-0.1.0.tar.gz" beforeDir="false" afterPath="$PROJECT_DIR$/dist/gpm-0.1.0.tar.gz" afterDir="false" /> <change beforePath="$PROJECT_DIR$/dist/gpm-0.1.0.tar.gz" beforeDir="false" afterPath="$PROJECT_DIR$/dist/gpm-0.1.0.tar.gz" afterDir="false" />
...@@ -87,7 +86,7 @@ ...@@ -87,7 +86,7 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1724140762915</updated> <updated>1724140762915</updated>
<workItem from="1724140765116" duration="10726000" /> <workItem from="1724140765116" duration="10726000" />
<workItem from="1724224393281" duration="3409000" /> <workItem from="1724224393281" duration="4621000" />
</task> </task>
<servers /> <servers />
</component> </component>
......
No preview for this file type
No preview for this file type
...@@ -93,14 +93,27 @@ def setup(): ...@@ -93,14 +93,27 @@ def setup():
remove_parser = subparsers.add_parser('remove', help='Remove a git profile') remove_parser = subparsers.add_parser('remove', help='Remove a git profile')
remove_parser.add_argument('name', type=str, help='Name or email of the profile to remove, case-insensitive, partial match') remove_parser.add_argument('name', type=str, help='Name or email of the profile to remove, case-insensitive, partial match')
# Current profile import
import_parser = subparsers.add_parser('import', help='Import the current profile from git config')
import_parser.add_argument('--name', type=str, help='Override name for the current profile')
import_parser.add_argument('--email', type=str, help='Override email for the current profile')
import_parser.add_argument('--comment', type=str, help='Set comment for the current profile')
import_parser.add_argument('--global', help='Import the current "global" git config instead of the local one', action='store_true')
# Current profile subcommand # Current profile subcommand
current_parser = subparsers.add_parser('current', help='Manage the current profile') current_parser = subparsers.add_parser('current', help='Manage the current profile')
current_subparser = current_parser.add_subparsers(title="Commands", dest="current_command") current_subparser = current_parser.add_subparsers(title="Commands", dest="current_command")
# Current profile show
current_subparser.add_parser('show', help='Show the current profile') current_subparser.add_parser('show', help='Show the current profile')
# Current profile edit
current_edit_parser = current_subparser.add_parser('edit', help='Edit the current profile') current_edit_parser = current_subparser.add_parser('edit', help='Edit the current profile')
current_edit_parser.add_argument('--name', type=str, help='New name for the current profile') current_edit_parser.add_argument('--name', type=str, help='New name for the current profile')
current_edit_parser.add_argument('--email', type=str, help='New email for the current profile') current_edit_parser.add_argument('--email', type=str, help='New email for the current profile')
current_edit_parser.add_argument('--comment', type=str, help='New comment for the current profile') current_edit_parser.add_argument('--comment', type=str, help='New comment for the current profile')
# Current profile remove
current_subparser.add_parser('remove', help='Remove the current profile') current_subparser.add_parser('remove', help='Remove the current profile')
return parser return parser
...@@ -122,9 +135,10 @@ class Profile: ...@@ -122,9 +135,10 @@ class Profile:
class GPM: class GPM:
def __init__(self): def __init__(self, use_global_git_config=False):
self.profiles = [] self.profiles = []
self._load() self._load()
self.use_global_git_config = use_global_git_config
def _load(self): def _load(self):
with open(data_file, "r") as f: with open(data_file, "r") as f:
...@@ -210,12 +224,18 @@ class GPM: ...@@ -210,12 +224,18 @@ class GPM:
def get_profile_format(self, profile: Profile): def get_profile_format(self, profile: Profile):
return f"({self.get_profile_number(profile)}) {profile}" return f"({self.get_profile_number(profile)}) {profile}"
def _get_git_subcommand(self, *params):
git_command = "git config"
if self.use_global_git_config:
git_command += " --global"
return f"{git_command} {' '.join(params)}"
def use(self, name_or_email): def use(self, name_or_email):
if profile := self.match_query_to_profile(name_or_email): if profile := self.match_query_to_profile(name_or_email):
print(f"Using profile {self.get_profile_format(profile)}", end=" ") print(f"Using profile {self.get_profile_format(profile)}", end=" ")
try: try:
os.system(f"git config user.name \"{profile.name}\"") os.system(self._get_git_subcommand("user.name", f"\"{profile.name}\""))
os.system(f"git config user.email \"{profile.email}\"") os.system(self._get_git_subcommand("user.email", f"\"{profile.email}\""))
print(f"{colorama.Fore.GREEN}OK {colorama.Style.RESET_ALL}") print(f"{colorama.Fore.GREEN}OK {colorama.Style.RESET_ALL}")
except Exception as e: except Exception as e:
print(f"{colorama.Fore.RED}Error:\n{e} {colorama.Style.RESET_ALL}") print(f"{colorama.Fore.RED}Error:\n{e} {colorama.Style.RESET_ALL}")
...@@ -231,10 +251,9 @@ class GPM: ...@@ -231,10 +251,9 @@ class GPM:
else: else:
print(f"{colorama.Fore.RED}No matching profile found for '{name}'{colorama.Style.RESET_ALL}") print(f"{colorama.Fore.RED}No matching profile found for '{name}'{colorama.Style.RESET_ALL}")
@staticmethod def load_git_profile(self) -> Optional[Profile]:
def load_git_profile() -> Optional[Profile]: name = os.popen(self._get_git_subcommand("user.name")).read().strip()
name = os.popen("git config user.name").read().strip() email = os.popen(self._get_git_subcommand("user.email")).read().strip()
email = os.popen("git config user.email").read().strip()
try: try:
profile = Profile(name, email) profile = Profile(name, email)
return profile return profile
...@@ -317,6 +336,33 @@ class GPM: ...@@ -317,6 +336,33 @@ class GPM:
sys.exit(1) sys.exit(1)
return profile return profile
def current_import(self, name=None, email=None, comment=None):
profile = self.load_git_profile()
if not profile:
print(f"{colorama.Fore.YELLOW}No current profile set{colorama.Style.RESET_ALL}")
sys.exit(1)
existing_profile_index = self.get_profile_number(profile)
if existing_profile_index != -1:
print(f"{colorama.Fore.YELLOW}Current profile already exists{colorama.Style.RESET_ALL}")
# "To use this profile..."
print(f"\nUse the profile with: \n$ {colorama.Fore.CYAN}gpm use {profile.email}{colorama.Style.RESET_ALL}\nor\n$ {colorama.Fore.CYAN}gpm use {existing_profile_index}{colorama.Style.RESET_ALL}")
sys.exit(1)
if name:
profile.name = name
if email:
profile.email = email
if comment:
profile.comment = comment
if not profile.validate():
print(f"{colorama.Fore.RED}Invalid git config{colorama.Style.RESET_ALL}")
print(profile)
sys.exit(1)
self.profiles.append(profile)
self._save()
print(f"{colorama.Fore.GREEN}Imported profile: {self.get_profile_format(profile)}{colorama.Style.RESET_ALL}")
def run(self): def run(self):
parser = setup() parser = setup()
args = parser.parse_args() args = parser.parse_args()
...@@ -338,6 +384,12 @@ class GPM: ...@@ -338,6 +384,12 @@ class GPM:
self.current_edit(args.name, args.email, args.comment) self.current_edit(args.name, args.email, args.comment)
elif args.current_command == 'remove': elif args.current_command == 'remove':
self.current_remove() self.current_remove()
else:
parser.print_help()
elif args.command == 'import':
if getattr(args, 'global'):
self.use_global_git_config = True
self.current_import(args.name, args.email, args.comment)
elif args.author: elif args.author:
print(__AUTHOR__) print(__AUTHOR__)
else: else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment