- Docker:新增只读、生命周期、清理、Compose 共 32 条命令,Linux 自动适配 podman - 危险命令 clearall/prunevolumes 未带 --yes 时仅提示并以退出码 2 结束 - 需要容器/镜像名的命令做必填参数校验,-- 之后参数计入位置参数 - diagnose:自动识别 Docker/Podman 容器或 K8s Pod,输出状态、根因、依据、建议并附日志与事件 - diagnose 中 -n 表示 namespace,其余命令 -n 仍为 --dry-run - 新增内置动作桥接与平台变体 helper,补充单元/集成/CLI 测试与 README
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package diagnose
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.odboy.cn/odboy/kenaito-kopsc/internal/platform"
|
|
)
|
|
|
|
// fakeRunner records calls and returns canned output.
|
|
type fakeRunner struct {
|
|
available map[string]bool
|
|
calls []string
|
|
respond func(name string, args []string) (CommandResult, error)
|
|
}
|
|
|
|
func (f *fakeRunner) LookPath(name string) (string, error) {
|
|
if f.available[name] {
|
|
return "/usr/bin/" + name, nil
|
|
}
|
|
return "", fmt.Errorf("%s not found", name)
|
|
}
|
|
|
|
func (f *fakeRunner) Run(ctx context.Context, name string, args ...string) (CommandResult, error) {
|
|
f.calls = append(f.calls, name+" "+strings.Join(args, " "))
|
|
if f.respond == nil {
|
|
return CommandResult{}, nil
|
|
}
|
|
return f.respond(name, args)
|
|
}
|
|
|
|
func (f *fakeRunner) called(substr string) bool {
|
|
for _, call := range f.calls {
|
|
if strings.Contains(call, substr) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func newTestService(r Runner) (*Service, *bytes.Buffer, *bytes.Buffer) {
|
|
out, errOut := &bytes.Buffer{}, &bytes.Buffer{}
|
|
s := New(platform.Info{OS: "linux", Arch: "amd64"}, r, out, errOut)
|
|
s.now = func() time.Time { return time.Date(2026, 10, 31, 12, 0, 0, 0, time.UTC) }
|
|
return s, out, errOut
|
|
}
|
|
|
|
// sampleDocker builds an exited container.
|
|
func sampleDocker(status string, exitCode int, oom bool, restarts int) dockerContainer {
|
|
c := dockerContainer{ID: "abc", Name: "/web", RestartCount: restarts}
|
|
c.Config.Image = "nginx:1.25"
|
|
c.State.Status = status
|
|
c.State.ExitCode = exitCode
|
|
c.State.OOMKilled = oom
|
|
c.State.Running = status == "running"
|
|
return c
|
|
}
|