Files
unitdore/runtime/podman.go
sgcommand 5078558d01 Phase 5-7: README, Makefile, unit tests
- README.md: full usage docs
- Makefile: build/install/uninstall/test/lint/clean targets
- config/config_test.go: 7 tests (load, save, find, add, roundtrip, invalid yaml)
- runtime/runtime_test.go: 6 tests (get, available, ListRunning graceful degradation)
- systemd/generator_test.go: 7 tests (ServiceName, Generate system/user/custom/docker/unknown, buildExecCommands)
- fix: docker/podman ListRunning returns nil (not error) when daemon unavailable
- fix: invalid YAML test uses tab-indent which is genuinely unparseable
2026-04-03 14:50:36 +02:00

75 lines
1.5 KiB
Go

package runtime
import (
"encoding/json"
"fmt"
"os/exec"
"strings"
)
type Podman struct{}
func (p *Podman) Name() string { return "podman" }
type podmanContainer struct {
Names []string `json:"Names"`
Image string `json:"Image"`
Command string `json:"Command"`
State string `json:"State"`
}
func (p *Podman) ListRunning() ([]Container, error) {
bin, err := exec.LookPath("podman")
if err != nil {
return nil, nil // podman not installed — not an error
}
out, err := exec.Command(bin, "ps", "--format", "json").Output()
if err != nil {
// Podman installed but not usable — treat as no containers
return nil, nil
}
var raw []podmanContainer
if err := json.Unmarshal(out, &raw); err != nil {
return nil, fmt.Errorf("parsing podman output: %w", err)
}
var containers []Container
for _, c := range raw {
name := ""
if len(c.Names) > 0 {
name = c.Names[0]
}
if name == "" {
continue
}
containers = append(containers, Container{
Name: name,
Image: c.Image,
Command: c.Command,
Runtime: "podman",
})
}
return containers, nil
}
func (p *Podman) Exists(name string) (bool, error) {
bin, err := exec.LookPath("podman")
if err != nil {
return false, nil
}
out, err := exec.Command(bin, "ps", "-a", "--format", "{{.Names}}").Output()
if err != nil {
return false, fmt.Errorf("podman ps -a: %w", err)
}
for _, line := range strings.Split(string(out), "\n") {
if strings.TrimSpace(line) == name {
return true, nil
}
}
return false, nil
}