Files
kenaito-kopsc/internal/diagnose/runner.go
T
odboy bd6f20d7c5 feat: 新增 proc nohup 后台运行、docker save/load 内置实现,移除 Podman 支持
- proc.nohup:后台运行命令并将 stdout/stderr 追加写入日志,
  Unix 下新会话、Windows 下 DETACHED_PROCESS 脱离终端
- docker.save:等价 docker save <镜像> -o <镜像名>.tar,文件名按
  仓库名+标签生成(alpine:3.20 → alpine_3.20.tar),传镜像 ID 时
  先解析出对应名称;docker.load 改为归档文件作位置参数
- 移除 Podman 支持,所有容器命令只走 docker
2026-11-01 07:47:57 +08:00

55 lines
1.4 KiB
Go

// Package diagnose explains why a container or Pod is in its current state.
// It knows how to inspect Docker containers and Kubernetes Pods, and
// falls back to a built-in implementation so the output is identical on every
// platform.
package diagnose
import (
"bytes"
"context"
"errors"
"os/exec"
)
// CommandResult is the output of one external command.
type CommandResult struct {
Stdout []byte
Stderr []byte
ExitCode int
}
// Runner executes external programs. It is an interface so diagnoses can be
// tested without docker or kubectl.
type Runner interface {
LookPath(name string) (string, error)
Run(ctx context.Context, name string, args ...string) (CommandResult, error)
}
// ExecRunner runs commands on the host.
type ExecRunner struct{}
// LookPath reports whether a program is available on PATH.
func (ExecRunner) LookPath(name string) (string, error) {
return exec.LookPath(name)
}
// Run executes a command and captures its output.
func (ExecRunner) Run(ctx context.Context, name string, args ...string) (CommandResult, error) {
cmd := exec.CommandContext(ctx, name, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
result := CommandResult{Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}
if err == nil {
return result, nil
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
result.ExitCode = exitErr.ExitCode()
}
return result, err
}