- assets/: sap-cli source (v2.1.0, 26 commands, 16 object types) - references/: tool constraints + error handling (self-contained) - scripts/setup.py: one-click install/config/verify - SKILL.md: full command reference + dual-platform install guide - VERSION: 2.1.0 Built from D:/Codespace/sap-cli via scripts/pack_skill.py
107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
"""config 子命令:显示配置、列出 profile、设置配置项。"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import configparser
|
|
import os
|
|
|
|
|
|
def cmd_config(args: argparse.Namespace, client=None) -> None:
|
|
"""配置管理命令。"""
|
|
action = getattr(args, "config_action", None)
|
|
if action == "show":
|
|
_config_show(args)
|
|
elif action == "list-profiles":
|
|
_config_list_profiles(args)
|
|
elif action == "set":
|
|
_config_set(args)
|
|
else:
|
|
print(" 用法: sap-cli config [show|list-profiles|set]")
|
|
|
|
|
|
def _config_show(args: argparse.Namespace) -> None:
|
|
"""显示当前配置。"""
|
|
from sapcli.config import load_config
|
|
|
|
config_path = getattr(args, "config", None)
|
|
|
|
try:
|
|
cfg, loaded_from = load_config(config_path)
|
|
except Exception as e:
|
|
print(f" ✗ 加载配置失败: {e}")
|
|
return
|
|
|
|
print("=" * 60)
|
|
print(" sap-cli 当前配置")
|
|
print("=" * 60)
|
|
if loaded_from:
|
|
print(f" 配置文件: {loaded_from}")
|
|
print(f" 主机: {cfg.host}")
|
|
print(f" Client: {cfg.client}")
|
|
print(f" 用户: {cfg.user}")
|
|
print(f" 密码: {'***' if cfg.password else '(未设置)'}")
|
|
|
|
|
|
def _config_list_profiles(args: argparse.Namespace) -> None:
|
|
"""列出所有可用的 profile。"""
|
|
config_path = getattr(args, "config", None)
|
|
if config_path is None:
|
|
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "config.ini")
|
|
|
|
print("=" * 60)
|
|
print(" sap-cli 可用 profile")
|
|
print("=" * 60)
|
|
|
|
if not os.path.isfile(config_path):
|
|
print(" (无配置文件)")
|
|
return
|
|
|
|
parser = configparser.ConfigParser()
|
|
parser.read(config_path, encoding="utf-8")
|
|
sections = parser.sections()
|
|
if not sections:
|
|
print(" (无 profile)")
|
|
for name in sections:
|
|
active = " (默认)" if name == "SAP" else ""
|
|
print(f" - {name}{active}")
|
|
|
|
print(f"\n 配置文件: {config_path}")
|
|
|
|
|
|
def _config_set(args: argparse.Namespace) -> None:
|
|
"""设置配置项到 config.ini。"""
|
|
key = getattr(args, "key", None)
|
|
value = getattr(args, "value", None)
|
|
profile = getattr(args, "profile", None) or "SAP"
|
|
|
|
if not key or not value:
|
|
print(" ✗ 用法: sap-cli config set <key> <value>")
|
|
print(" 可设置的 key: host, client, user, password")
|
|
return
|
|
|
|
valid_keys = {"host", "client", "user", "password"}
|
|
if key not in valid_keys:
|
|
print(f" ✗ 不支持的配置项: {key}")
|
|
print(f" 可设置的 key: {', '.join(sorted(valid_keys))}")
|
|
return
|
|
|
|
# 找到或创建 config.ini
|
|
config_path = getattr(args, "config", None)
|
|
if not config_path:
|
|
config_path = os.path.join(os.getcwd(), "config.ini")
|
|
|
|
parser = configparser.ConfigParser()
|
|
if os.path.isfile(config_path):
|
|
parser.read(config_path, encoding="utf-8")
|
|
|
|
if not parser.has_section(profile):
|
|
parser.add_section(profile)
|
|
|
|
parser.set(profile, key, value)
|
|
|
|
with open(config_path, "w", encoding="utf-8") as f:
|
|
parser.write(f)
|
|
|
|
print(f" ✓ 已设置 [{profile}] {key} = {'***' if key == 'password' else value}")
|
|
print(f" 配置文件: {config_path}")
|