Add prefix/suffix support, short ID fallback, unit docs

- config: add Prefix/Suffix fields to Config struct
- systemd: ServiceName/Generate/UnitPath/Install/Uninstall/Enable/Disable/Status all accept prefix+suffix
- runtime: fall back to short container ID (12 chars) when container has no name
- cmd: active, status, install all thread prefix/suffix from config
- systemd/generator_test.go: updated all calls + added TestGenerate_WithPrefixSuffix
- docs/generated-units.md: full examples of every unit type + ordering + naming
- README: updated config docs, prefix/suffix section, link to docs/
This commit is contained in:
2026-04-03 15:47:56 +02:00
parent 5078558d01
commit a8c12c8c21
11 changed files with 419 additions and 51 deletions

View File

@@ -27,6 +27,8 @@ func runActive(cmd *cobra.Command, args []string) error {
return err
}
prefix, suffix := cfg.Prefix, cfg.Suffix
// Sort by order then name for deterministic startup sequence
units := make([]config.Unit, len(cfg.Units))
copy(units, cfg.Units)
@@ -44,12 +46,12 @@ func runActive(cmd *cobra.Command, args []string) error {
if !u.Enabled {
continue
}
if !systemd.IsInstalled(u) {
if !systemd.IsInstalled(u, prefix, suffix) {
fmt.Printf(" ! %s: not installed — run 'unitdore install' first\n", u.Name)
continue
}
fmt.Printf(" ▶ enabling: %s...\n", systemd.ServiceName(u))
if err := systemd.Enable(u); err != nil {
fmt.Printf(" ▶ enabling: %s...\n", systemd.ServiceName(u, prefix, suffix))
if err := systemd.Enable(u, prefix, suffix); err != nil {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
failed++
} else {

View File

@@ -31,6 +31,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
return err
}
prefix, suffix := cfg.Prefix, cfg.Suffix
installed := 0
skipped := 0
removed := 0
@@ -38,14 +39,14 @@ func runInstall(cmd *cobra.Command, args []string) error {
for _, u := range cfg.Units {
if !u.Enabled {
// Remove unit file if it exists for disabled units
if systemd.IsInstalled(u) {
if systemd.IsInstalled(u, prefix, suffix) {
if dryRun {
fmt.Printf(" ~ would remove: %s\n", systemd.ServiceName(u))
fmt.Printf(" ~ would remove: %s\n", systemd.ServiceName(u, prefix, suffix))
} else {
if err := systemd.Uninstall(u); err != nil {
if err := systemd.Uninstall(u, prefix, suffix); err != nil {
fmt.Printf(" ✗ failed to remove %s: %v\n", u.Name, err)
} else {
fmt.Printf(" - removed: %s (disabled)\n", systemd.ServiceName(u))
fmt.Printf(" - removed: %s (disabled)\n", systemd.ServiceName(u, prefix, suffix))
removed++
}
}
@@ -56,21 +57,21 @@ func runInstall(cmd *cobra.Command, args []string) error {
}
if dryRun {
content, err := systemd.Generate(u)
content, err := systemd.Generate(u, prefix, suffix)
if err != nil {
fmt.Printf(" ✗ %s: %v\n", u.Name, err)
continue
}
path, _ := systemd.UnitPath(u)
path, _ := systemd.UnitPath(u, prefix, suffix)
fmt.Printf("\n--- %s ---\n%s\n", path, content)
installed++
continue
}
if err := systemd.Install(u); err != nil {
if err := systemd.Install(u, prefix, suffix); err != nil {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
} else {
path, _ := systemd.UnitPath(u)
path, _ := systemd.UnitPath(u, prefix, suffix)
fmt.Printf(" ✓ installed: %s\n", path)
installed++
}

View File

@@ -36,6 +36,8 @@ func runStatus(cmd *cobra.Command, args []string) error {
return err
}
prefix, suffix := cfg.Prefix, cfg.Suffix
if len(cfg.Units) == 0 {
fmt.Println("No units configured. Run 'unitdore syncup' to discover containers.")
return nil
@@ -63,13 +65,13 @@ func runStatus(cmd *cobra.Command, args []string) error {
}
installed := "yes"
if !systemd.IsInstalled(u) {
if !systemd.IsInstalled(u, prefix, suffix) {
installed = "no"
}
state := "—"
if systemd.IsInstalled(u) {
state = systemd.Status(u)
if systemd.IsInstalled(u, prefix, suffix) {
state = systemd.Status(u, prefix, suffix)
}
rows = append(rows, statusRow{