88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
package capability
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||
|
||
"github.com/gitlink-org/gitlink-cli/internal/capability"
|
||
)
|
||
|
||
// SharedRegistry is the global capability registry used across the CLI.
|
||
// It is initialized in register.go and used by help annotations.
|
||
var SharedRegistry = capability.NewRegistry()
|
||
|
||
// Shortcuts returns the capability management shortcuts.
|
||
func Shortcuts() []*common.Shortcut {
|
||
return []*common.Shortcut{
|
||
{
|
||
Name: "check",
|
||
Description: "探测后端 API 能力,检查各模块是否可用",
|
||
Long: `向 GitLink 后端发送探测请求,检查各命令模块依赖的 API 是否就绪。
|
||
|
||
探测结果会缓存 24 小时。之后运行 capability +list 查看缓存结果。
|
||
|
||
需要 owner/repo 上下文的模块(如 label、webhook、member 等)会自动从
|
||
git remote 推断,或通过 --owner/--repo 指定。`,
|
||
Flags: []common.Flag{},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
// Resolve owner/repo for repo-dependent probes
|
||
owner, repo := ctx.Owner, ctx.Repo
|
||
if owner == "" || repo == "" {
|
||
_ = ctx.ResolveOwnerRepo()
|
||
owner, repo = ctx.Owner, ctx.Repo
|
||
}
|
||
|
||
results := SharedRegistry.Refresh(ctx.Client, owner, repo)
|
||
|
||
// Print results table
|
||
fmt.Println("API 后端能力探测结果:")
|
||
fmt.Println()
|
||
fmt.Printf(" %-4s %-15s %-12s %s\n", "", "模块", "状态", "说明")
|
||
fmt.Println(" " + "---- --------------- ------------ ------------------------------")
|
||
|
||
domains := []string{
|
||
"label", "notification", "pm", "wiki", "pipeline",
|
||
"webhook", "member", "milestone", "export", "search", "workflow",
|
||
}
|
||
for _, domain := range domains {
|
||
ds, ok := results[domain]
|
||
if !ok {
|
||
fmt.Printf(" %-4s %-15s %-12s %s\n", "?", domain, "skipped", "缺少 owner/repo 上下文,未探测")
|
||
continue
|
||
}
|
||
icon := ds.Status.Emoji()
|
||
statusText := map[capability.Status]string{
|
||
capability.StatusAvailable: "可用 ✓",
|
||
capability.StatusUnavailable: "不可用 ✗",
|
||
capability.StatusError: "错误 ✗",
|
||
capability.StatusUnknown: "未知 ?",
|
||
}[ds.Status]
|
||
detail := ds.Message
|
||
if detail == "" && ds.Status == capability.StatusAvailable {
|
||
detail = "API 正常响应"
|
||
}
|
||
fmt.Printf(" %-4s %-15s %-12s %s\n", icon, domain, statusText, detail)
|
||
}
|
||
|
||
fmt.Println()
|
||
fmt.Println("提示: 不可用的模块会在 --help 中标记 ⚠,调用时会显示中文错误指引。")
|
||
fmt.Println("缓存位置: ~/.config/gitlink-cli/capabilities.json(24 小时有效)")
|
||
return nil
|
||
},
|
||
},
|
||
{
|
||
Name: "list",
|
||
Description: "查看已缓存的 API 能力探测结果",
|
||
Flags: []common.Flag{},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
fmt.Print(SharedRegistry.Summary())
|
||
if SharedRegistry.IsStale() {
|
||
fmt.Println("\n⚠ 缓存已过期(超过 24 小时),运行 capability +check 刷新。")
|
||
}
|
||
return nil
|
||
},
|
||
},
|
||
}
|
||
}
|