chore(io): Address PR feedback for Medium migration

- Made pkg/repos truly medium-agnostic by removing local filepath.Abs calls.
- Restored Medium abstraction in pkg/cli/daemon.go (PIDFile and Daemon).
- Restored context cancellation checks in pkg/container/linuxkit.go.
- Updated pkg/cli/daemon_test.go to use MockMedium.
- Documented FindRegistry's local filesystem dependencies.
- Verified project-wide build and tests pass.
This commit is contained in:
Snider 2026-02-04 15:05:46 +00:00
parent 4f7869d982
commit 174d097a3a
4 changed files with 61 additions and 48 deletions

View file

@ -74,13 +74,14 @@ func IsStderrTTY() bool {
// PIDFile manages a process ID file for single-instance enforcement.
type PIDFile struct {
path string
mu sync.Mutex
medium io.Medium
path string
mu sync.Mutex
}
// NewPIDFile creates a PID file manager.
func NewPIDFile(path string) *PIDFile {
return &PIDFile{path: path}
func NewPIDFile(m io.Medium, path string) *PIDFile {
return &PIDFile{medium: m, path: path}
}
// Acquire writes the current PID to the file.
@ -90,7 +91,7 @@ func (p *PIDFile) Acquire() error {
defer p.mu.Unlock()
// Check if PID file exists
if data, err := io.Local.Read(p.path); err == nil {
if data, err := p.medium.Read(p.path); err == nil {
pid, err := strconv.Atoi(data)
if err == nil && pid > 0 {
// Check if process is still running
@ -101,19 +102,19 @@ func (p *PIDFile) Acquire() error {
}
}
// Stale PID file, remove it
_ = io.Local.Delete(p.path)
_ = p.medium.Delete(p.path)
}
// Ensure directory exists
if dir := filepath.Dir(p.path); dir != "." {
if err := io.Local.EnsureDir(dir); err != nil {
if err := p.medium.EnsureDir(dir); err != nil {
return fmt.Errorf("failed to create PID directory: %w", err)
}
}
// Write current PID
pid := os.Getpid()
if err := io.Local.Write(p.path, strconv.Itoa(pid)); err != nil {
if err := p.medium.Write(p.path, strconv.Itoa(pid)); err != nil {
return fmt.Errorf("failed to write PID file: %w", err)
}
@ -124,7 +125,7 @@ func (p *PIDFile) Acquire() error {
func (p *PIDFile) Release() error {
p.mu.Lock()
defer p.mu.Unlock()
return io.Local.Delete(p.path)
return p.medium.Delete(p.path)
}
// Path returns the PID file path.
@ -246,6 +247,10 @@ func (h *HealthServer) Addr() string {
// DaemonOptions configures daemon mode execution.
type DaemonOptions struct {
// Medium is the storage backend for PID files.
// Defaults to io.Local if not set.
Medium io.Medium
// PIDFile path for single-instance enforcement.
// Leave empty to skip PID file management.
PIDFile string
@ -283,13 +288,17 @@ func NewDaemon(opts DaemonOptions) *Daemon {
opts.ShutdownTimeout = 30 * time.Second
}
if opts.Medium == nil {
opts.Medium = io.Local
}
d := &Daemon{
opts: opts,
reload: make(chan struct{}, 1),
}
if opts.PIDFile != "" {
d.pid = NewPIDFile(opts.PIDFile)
d.pid = NewPIDFile(opts.Medium, opts.PIDFile)
}
if opts.HealthAddr != "" {

View file

@ -3,11 +3,10 @@ package cli
import (
"context"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/host-uk/core/pkg/io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -28,37 +27,36 @@ func TestDetectMode(t *testing.T) {
func TestPIDFile(t *testing.T) {
t.Run("acquire and release", func(t *testing.T) {
tmpDir := t.TempDir()
pidPath := filepath.Join(tmpDir, "test.pid")
m := io.NewMockMedium()
pidPath := "/tmp/test.pid"
pid := NewPIDFile(pidPath)
pid := NewPIDFile(m, pidPath)
// Acquire should succeed
err := pid.Acquire()
require.NoError(t, err)
// File should exist with our PID
data, err := os.ReadFile(pidPath)
data, err := m.Read(pidPath)
require.NoError(t, err)
assert.Contains(t, string(data), "")
assert.NotEmpty(t, data)
// Release should remove file
err = pid.Release()
require.NoError(t, err)
_, err = os.Stat(pidPath)
assert.True(t, os.IsNotExist(err))
assert.False(t, m.Exists(pidPath))
})
t.Run("stale pid file", func(t *testing.T) {
tmpDir := t.TempDir()
pidPath := filepath.Join(tmpDir, "stale.pid")
m := io.NewMockMedium()
pidPath := "/tmp/stale.pid"
// Write a stale PID (non-existent process)
err := os.WriteFile(pidPath, []byte("999999999"), 0644)
err := m.Write(pidPath, "999999999")
require.NoError(t, err)
pid := NewPIDFile(pidPath)
pid := NewPIDFile(m, pidPath)
// Should acquire successfully (stale PID removed)
err = pid.Acquire()
@ -69,23 +67,23 @@ func TestPIDFile(t *testing.T) {
})
t.Run("creates parent directory", func(t *testing.T) {
tmpDir := t.TempDir()
pidPath := filepath.Join(tmpDir, "subdir", "nested", "test.pid")
m := io.NewMockMedium()
pidPath := "/tmp/subdir/nested/test.pid"
pid := NewPIDFile(pidPath)
pid := NewPIDFile(m, pidPath)
err := pid.Acquire()
require.NoError(t, err)
_, err = os.Stat(pidPath)
require.NoError(t, err)
assert.True(t, m.Exists(pidPath))
err = pid.Release()
require.NoError(t, err)
})
t.Run("path getter", func(t *testing.T) {
pid := NewPIDFile("/tmp/test.pid")
m := io.NewMockMedium()
pid := NewPIDFile(m, "/tmp/test.pid")
assert.Equal(t, "/tmp/test.pid", pid.Path())
})
}
@ -157,10 +155,12 @@ func TestHealthServer(t *testing.T) {
func TestDaemon(t *testing.T) {
t.Run("start and stop", func(t *testing.T) {
tmpDir := t.TempDir()
m := io.NewMockMedium()
pidPath := "/tmp/test.pid"
d := NewDaemon(DaemonOptions{
PIDFile: filepath.Join(tmpDir, "test.pid"),
Medium: m,
PIDFile: pidPath,
HealthAddr: "127.0.0.1:0",
ShutdownTimeout: 5 * time.Second,
})
@ -182,8 +182,7 @@ func TestDaemon(t *testing.T) {
require.NoError(t, err)
// PID file should be removed
_, err = os.Stat(filepath.Join(tmpDir, "test.pid"))
assert.True(t, os.IsNotExist(err))
assert.False(t, m.Exists(pidPath))
})
t.Run("double start fails", func(t *testing.T) {

View file

@ -232,6 +232,9 @@ func (m *LinuxKitManager) waitForExit(id string, cmd *exec.Cmd) {
// Stop stops a running container by sending SIGTERM.
func (m *LinuxKitManager) Stop(ctx context.Context, id string) error {
if err := ctx.Err(); err != nil {
return err
}
container, ok := m.state.Get(id)
if !ok {
return fmt.Errorf("container not found: %s", id)
@ -290,6 +293,9 @@ func (m *LinuxKitManager) Stop(ctx context.Context, id string) error {
// List returns all known containers, verifying process state.
func (m *LinuxKitManager) List(ctx context.Context) ([]*Container, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
containers := m.state.All()
// Verify each running container's process is still alive
@ -319,6 +325,9 @@ func isProcessRunning(pid int) bool {
// Logs returns a reader for the container's log output.
func (m *LinuxKitManager) Logs(ctx context.Context, id string, follow bool) (goio.ReadCloser, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
_, ok := m.state.Get(id)
if !ok {
return nil, fmt.Errorf("container not found: %s", id)
@ -403,6 +412,9 @@ func (f *followReader) Close() error {
// Exec executes a command inside the container via SSH.
func (m *LinuxKitManager) Exec(ctx context.Context, id string, cmd []string) error {
if err := ctx.Err(); err != nil {
return err
}
container, ok := m.state.Get(id)
if !ok {
return fmt.Errorf("container not found: %s", id)

View file

@ -61,14 +61,10 @@ type Repo struct {
registry *Registry `yaml:"-"`
}
// LoadRegistry reads and parses a repos.yaml file.
// LoadRegistry reads and parses a repos.yaml file from the given medium.
// The path should be a valid path for the provided medium.
func LoadRegistry(m io.Medium, path string) (*Registry, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("failed to resolve path: %w", err)
}
content, err := m.Read(absPath)
content, err := m.Read(path)
if err != nil {
return nil, fmt.Errorf("failed to read registry file: %w", err)
}
@ -101,6 +97,7 @@ func LoadRegistry(m io.Medium, path string) (*Registry, error) {
// FindRegistry searches for repos.yaml in common locations.
// It checks: current directory, parent directories, and home directory.
// This function is primarily intended for use with io.Local or other local-like filesystems.
func FindRegistry(m io.Medium) (string, error) {
// Check current directory and parents
dir, err := os.Getwd()
@ -143,20 +140,16 @@ func FindRegistry(m io.Medium) (string, error) {
// ScanDirectory creates a Registry by scanning a directory for git repos.
// This is used as a fallback when no repos.yaml is found.
// The dir should be a valid path for the provided medium.
func ScanDirectory(m io.Medium, dir string) (*Registry, error) {
absDir, err := filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf("failed to resolve directory path: %w", err)
}
entries, err := m.List(absDir)
entries, err := m.List(dir)
if err != nil {
return nil, fmt.Errorf("failed to read directory: %w", err)
}
reg := &Registry{
Version: 1,
BasePath: absDir,
BasePath: dir,
Repos: make(map[string]*Repo),
medium: m,
}
@ -167,7 +160,7 @@ func ScanDirectory(m io.Medium, dir string) (*Registry, error) {
continue
}
repoPath := filepath.Join(absDir, entry.Name())
repoPath := filepath.Join(dir, entry.Name())
gitPath := filepath.Join(repoPath, ".git")
if !m.IsDir(gitPath) {