Skip to content

Commit 41c0db8

Browse files
authored
Fix: 重构并修复升级/降级项目格式时总是使用全局配置的问题 (#169)
* ♻️ add `get_config_manager()` for refactor * 🐛 fix project format upgrade/downgrade always using `GLOBAL_CONFIG`
1 parent 5fbc1ee commit 41c0db8

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

nb_cli/handlers/meta.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ def draw_logo() -> str:
3737
return figlet_format("NoneBot", font="basic").strip()
3838

3939

40-
def get_nonebot_config() -> NoneBotConfig | LegacyNoneBotConfig:
41-
return GLOBAL_CONFIG.get_nonebot_config()
40+
def get_config_manager(cwd: Path | None = None) -> ConfigManager:
41+
return ConfigManager(working_dir=cwd) if cwd is not None else GLOBAL_CONFIG
42+
43+
44+
def get_nonebot_config(cwd: Path | None = None) -> NoneBotConfig | LegacyNoneBotConfig:
45+
config = get_config_manager(cwd)
46+
return config.get_nonebot_config()
4247

4348

4449
def get_project_root(cwd: Path | None = None) -> Path:
45-
config = ConfigManager(working_dir=cwd) if cwd is not None else GLOBAL_CONFIG
50+
config = get_config_manager(cwd)
4651
return config.project_root
4752

4853

@@ -90,7 +95,7 @@ async def _get_env_python() -> str:
9095

9196

9297
async def get_default_python(cwd: Path | None = None) -> str:
93-
config = ConfigManager(working_dir=cwd) if cwd is not None else GLOBAL_CONFIG
98+
config = get_config_manager(cwd)
9499
if config.python_path is not None:
95100
return config.python_path
96101

nb_cli/handlers/project.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from nb_cli import _
1010
from nb_cli.config import (
11-
GLOBAL_CONFIG,
1211
SimpleInfo,
1312
PackageInfo,
1413
NoneBotConfig,
@@ -23,6 +22,7 @@
2322
from .meta import (
2423
get_project_root,
2524
requires_nonebot,
25+
get_config_manager,
2626
get_default_python,
2727
get_nonebot_config,
2828
requires_project_root,
@@ -127,8 +127,8 @@ def _index_by_module_name(data: Iterable[T_info]) -> dict[str, T_info]:
127127

128128

129129
@requires_project_root
130-
async def upgrade_project_format() -> None:
131-
bot_config = get_nonebot_config()
130+
async def upgrade_project_format(*, cwd: Path | None = None) -> None:
131+
bot_config = get_nonebot_config(cwd)
132132
if isinstance(bot_config, NoneBotConfig):
133133
click.echo(_("Current format is already the new format."))
134134
return
@@ -178,13 +178,14 @@ async def upgrade_project_format() -> None:
178178
builtin_plugins=bot_config.builtin_plugins,
179179
)
180180

181-
GLOBAL_CONFIG.update_nonebot_config(new_config)
182-
GLOBAL_CONFIG.update_dependency(nonebot_pkg, *packages)
181+
manager = get_config_manager(cwd)
182+
manager.update_nonebot_config(new_config)
183+
manager.update_dependency(nonebot_pkg, *packages)
183184

184185

185186
@requires_project_root
186-
async def downgrade_project_format() -> None:
187-
bot_config = get_nonebot_config()
187+
async def downgrade_project_format(*, cwd: Path | None = None) -> None:
188+
bot_config = get_nonebot_config(cwd)
188189
if isinstance(bot_config, LegacyNoneBotConfig):
189190
click.echo(_("Current format is already the old format."))
190191
return
@@ -196,4 +197,5 @@ async def downgrade_project_format() -> None:
196197
builtin_plugins=bot_config.builtin_plugins,
197198
)
198199

199-
GLOBAL_CONFIG.update_nonebot_config(old_config)
200+
manager = get_config_manager(cwd)
201+
manager.update_nonebot_config(old_config)

0 commit comments

Comments
 (0)