增加兼容性
This commit is contained in:
@@ -2,16 +2,99 @@
|
||||
配置模块 - 存储项目配置信息
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
|
||||
|
||||
class Config:
|
||||
"""配置类"""
|
||||
|
||||
# Gitea服务器配置
|
||||
GITEA_HOST = "git.shumengya.top"
|
||||
GITEA_PORT = "8022"
|
||||
# 配置文件路径
|
||||
CONFIG_FILE = os.path.join(os.path.expanduser("~"), ".quickgit_config.json")
|
||||
|
||||
# GitHub配置
|
||||
GITHUB_USER = "shumengya"
|
||||
# 默认配置
|
||||
_defaults = {
|
||||
"gitea_host": "git.shumengya.top",
|
||||
"gitea_port": "8022",
|
||||
"github_user": "shumengya",
|
||||
"default_branch": "main"
|
||||
}
|
||||
|
||||
# 运行时配置(从文件加载或使用默认值)
|
||||
_config = None
|
||||
|
||||
@classmethod
|
||||
def _load_config(cls):
|
||||
"""加载配置文件"""
|
||||
if cls._config is not None:
|
||||
return
|
||||
|
||||
if os.path.exists(cls.CONFIG_FILE):
|
||||
try:
|
||||
with open(cls.CONFIG_FILE, 'r', encoding='utf-8') as f:
|
||||
cls._config = json.load(f)
|
||||
except Exception:
|
||||
cls._config = cls._defaults.copy()
|
||||
else:
|
||||
cls._config = cls._defaults.copy()
|
||||
|
||||
@classmethod
|
||||
def _save_config(cls):
|
||||
"""保存配置到文件"""
|
||||
cls._load_config()
|
||||
try:
|
||||
with open(cls.CONFIG_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(cls._config, f, indent=2, ensure_ascii=False)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def get(cls, key: str, default=None):
|
||||
"""获取配置项"""
|
||||
cls._load_config()
|
||||
return cls._config.get(key, default)
|
||||
|
||||
@classmethod
|
||||
def set(cls, key: str, value):
|
||||
"""设置配置项"""
|
||||
cls._load_config()
|
||||
cls._config[key] = value
|
||||
cls._save_config()
|
||||
|
||||
# 属性访问器(保持向后兼容)
|
||||
@property
|
||||
def GITEA_HOST(self) -> str:
|
||||
return self.get("gitea_host", self._defaults["gitea_host"])
|
||||
|
||||
@property
|
||||
def GITEA_PORT(self) -> str:
|
||||
return self.get("gitea_port", self._defaults["gitea_port"])
|
||||
|
||||
@property
|
||||
def GITHUB_USER(self) -> str:
|
||||
return self.get("github_user", self._defaults["github_user"])
|
||||
|
||||
@property
|
||||
def DEFAULT_BRANCH(self) -> str:
|
||||
return self.get("default_branch", self._defaults["default_branch"])
|
||||
|
||||
# 类方法访问器(用于类级别访问)
|
||||
@classmethod
|
||||
def get_gitea_host(cls) -> str:
|
||||
return cls.get("gitea_host", cls._defaults["gitea_host"])
|
||||
|
||||
@classmethod
|
||||
def get_gitea_port(cls) -> str:
|
||||
return cls.get("gitea_port", cls._defaults["gitea_port"])
|
||||
|
||||
@classmethod
|
||||
def get_github_user(cls) -> str:
|
||||
return cls.get("github_user", cls._defaults["github_user"])
|
||||
|
||||
@classmethod
|
||||
def get_default_branch(cls) -> str:
|
||||
return cls.get("default_branch", cls._defaults["default_branch"])
|
||||
|
||||
# Git配置
|
||||
DEFAULT_BRANCH = "main"
|
||||
|
||||
@@ -46,7 +46,8 @@ class RemoteManager:
|
||||
from .utils import Colors
|
||||
repo_name = InputValidator.get_input(f"{Colors.BRIGHT_CYAN}>> 请输入GitHub仓库名: {Colors.ENDC}")
|
||||
|
||||
remote_url = f"git@github.com:{Config.GITHUB_USER}/{repo_name}.git"
|
||||
github_user = Config.get_github_user()
|
||||
remote_url = f"git@github.com:{github_user}/{repo_name}.git"
|
||||
|
||||
return self._add_remote("github", remote_url)
|
||||
|
||||
@@ -68,10 +69,97 @@ class RemoteManager:
|
||||
if not repo_name:
|
||||
repo_name = InputValidator.get_input(f"{Colors.BRIGHT_CYAN}>> 请输入Gitea仓库名: {Colors.ENDC}")
|
||||
|
||||
remote_url = f"ssh://git@{Config.GITEA_HOST}:{Config.GITEA_PORT}/{user}/{repo_name}.git"
|
||||
gitea_host = Config.get_gitea_host()
|
||||
gitea_port = Config.get_gitea_port()
|
||||
remote_url = f"ssh://git@{gitea_host}:{gitea_port}/{user}/{repo_name}.git"
|
||||
|
||||
return self._add_remote("gitea", remote_url)
|
||||
|
||||
def add_custom_remote(self) -> bool:
|
||||
"""
|
||||
添加自建Git远程仓库
|
||||
|
||||
Returns:
|
||||
是否成功
|
||||
"""
|
||||
from .utils import Colors
|
||||
|
||||
print(f"\n{Colors.BRIGHT_MAGENTA}>> 添加自建Git仓库{Colors.ENDC}")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
OutputFormatter.tip("支持 GitLab、自建Gitea、Gogs 等 Git 服务器")
|
||||
OutputFormatter.tip("SSH URL 格式示例:")
|
||||
print(f"{Colors.WHITE} - git@gitlab.com:user/repo.git")
|
||||
print(f"{Colors.WHITE} - ssh://git@your-server.com:port/user/repo.git{Colors.ENDC}")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
# 输入远程仓库名称
|
||||
remote_name = InputValidator.get_input(
|
||||
f"{Colors.BRIGHT_CYAN}>> 请输入远程仓库名称 (如: gitlab, mygit): {Colors.ENDC}"
|
||||
)
|
||||
|
||||
# 检查是否已存在
|
||||
remotes = self.get_remotes()
|
||||
if remote_name in remotes:
|
||||
OutputFormatter.warning(f"远程仓库 '{remote_name}' 已存在")
|
||||
if not InputValidator.confirm("是否覆盖?", default=False):
|
||||
return False
|
||||
self.executor.run(f"git remote remove {remote_name}", show_output=False)
|
||||
|
||||
# 输入SSH URL
|
||||
remote_url = InputValidator.get_input(
|
||||
f"{Colors.BRIGHT_CYAN}>> 请输入完整的SSH URL: {Colors.ENDC}"
|
||||
)
|
||||
|
||||
# 验证URL格式
|
||||
if not (remote_url.startswith("git@") or remote_url.startswith("ssh://")):
|
||||
OutputFormatter.error("无效的SSH URL格式,必须以 'git@' 或 'ssh://' 开头")
|
||||
return False
|
||||
|
||||
return self._add_remote(remote_name, remote_url)
|
||||
|
||||
def configure_gitea_settings(self):
|
||||
"""配置Gitea服务器设置"""
|
||||
from .utils import Colors
|
||||
|
||||
print(f"\n{Colors.BRIGHT_MAGENTA}>> 配置Gitea服务器{Colors.ENDC}")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
# 显示当前配置
|
||||
current_host = Config.get_gitea_host()
|
||||
current_port = Config.get_gitea_port()
|
||||
|
||||
OutputFormatter.info(f"当前配置:")
|
||||
print(f"{Colors.WHITE} 主机: {current_host}{Colors.ENDC}")
|
||||
print(f"{Colors.WHITE} 端口: {current_port}{Colors.ENDC}")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
# 询问是否修改
|
||||
if not InputValidator.confirm("是否修改Gitea服务器配置?", default=False):
|
||||
return
|
||||
|
||||
# 输入新的主机地址
|
||||
new_host = InputValidator.get_input(
|
||||
f"{Colors.BRIGHT_CYAN}>> 请输入Gitea主机地址 (回车保持 {current_host}): {Colors.ENDC}",
|
||||
allow_empty=True
|
||||
)
|
||||
|
||||
if new_host:
|
||||
Config.set("gitea_host", new_host)
|
||||
OutputFormatter.success(f"Gitea主机地址已更新为: {new_host}")
|
||||
|
||||
# 输入新的端口
|
||||
new_port = InputValidator.get_input(
|
||||
f"{Colors.BRIGHT_CYAN}>> 请输入SSH端口 (回车保持 {current_port}): {Colors.ENDC}",
|
||||
allow_empty=True
|
||||
)
|
||||
|
||||
if new_port:
|
||||
Config.set("gitea_port", new_port)
|
||||
OutputFormatter.success(f"Gitea SSH端口已更新为: {new_port}")
|
||||
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
OutputFormatter.tip("配置已保存,下次添加Gitea仓库时将使用新配置")
|
||||
|
||||
def _add_remote(self, name: str, url: str) -> bool:
|
||||
"""
|
||||
添加远程仓库
|
||||
@@ -150,7 +238,7 @@ class RemoteManager:
|
||||
|
||||
OutputFormatter.menu_item(1, "GitHub")
|
||||
OutputFormatter.menu_item(2, "Gitea")
|
||||
OutputFormatter.menu_item(3, "两者都配置")
|
||||
OutputFormatter.menu_item(3, "自建Git仓库")
|
||||
OutputFormatter.menu_item(4, "跳过")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
@@ -164,8 +252,7 @@ class RemoteManager:
|
||||
elif choice == 2:
|
||||
self.add_gitea_remote()
|
||||
elif choice == 3:
|
||||
self.add_github_remote()
|
||||
self.add_gitea_remote()
|
||||
self.add_custom_remote()
|
||||
else:
|
||||
OutputFormatter.info("跳过远程仓库配置")
|
||||
|
||||
|
||||
@@ -208,13 +208,15 @@ class GitManagerUI:
|
||||
OutputFormatter.menu_item(1, "查看远程仓库")
|
||||
OutputFormatter.menu_item(2, "添加GitHub远程仓库")
|
||||
OutputFormatter.menu_item(3, "添加Gitea远程仓库")
|
||||
OutputFormatter.menu_item(4, "删除远程仓库")
|
||||
OutputFormatter.menu_item(5, "返回主菜单")
|
||||
OutputFormatter.menu_item(4, "添加自建Git仓库")
|
||||
OutputFormatter.menu_item(5, "配置Gitea服务器")
|
||||
OutputFormatter.menu_item(6, "删除远程仓库")
|
||||
OutputFormatter.menu_item(7, "返回主菜单")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
choice = InputValidator.get_choice(
|
||||
f"{Colors.BRIGHT_CYAN}>> 请选择 [1-5]: {Colors.ENDC}",
|
||||
range(1, 6)
|
||||
f"{Colors.BRIGHT_CYAN}>> 请选择 [1-7]: {Colors.ENDC}",
|
||||
range(1, 8)
|
||||
)
|
||||
|
||||
if choice == 1:
|
||||
@@ -224,8 +226,12 @@ class GitManagerUI:
|
||||
elif choice == 3:
|
||||
self.remote_mgr.add_gitea_remote()
|
||||
elif choice == 4:
|
||||
self.remote_mgr.remove_remote()
|
||||
self.remote_mgr.add_custom_remote()
|
||||
elif choice == 5:
|
||||
self.remote_mgr.configure_gitea_settings()
|
||||
elif choice == 6:
|
||||
self.remote_mgr.remove_remote()
|
||||
elif choice == 7:
|
||||
break
|
||||
|
||||
def run(self):
|
||||
|
||||
Reference in New Issue
Block a user