Files
unitdore/cmd/status.go
sgcommand a8c12c8c21 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/
2026-04-03 15:47:56 +02:00

129 lines
2.5 KiB
Go

package cmd
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var statusCmd = &cobra.Command{
Use: "status",
Short: "Show status summary of all managed units",
RunE: runStatus,
}
func init() {
rootCmd.AddCommand(statusCmd)
}
type statusRow struct {
Name string
Runtime string
User string
Enabled string
Installed string
State string
Reason string
}
func runStatus(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
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
}
units := make([]config.Unit, len(cfg.Units))
copy(units, cfg.Units)
sort.Slice(units, func(i, j int) bool {
if units[i].Order != units[j].Order {
return units[i].Order < units[j].Order
}
return units[i].Name < units[j].Name
})
rows := make([]statusRow, 0, len(units))
for _, u := range units {
user := u.User
if user == "" {
user = "root"
}
enabled := "yes"
if !u.Enabled {
enabled = "no"
}
installed := "yes"
if !systemd.IsInstalled(u, prefix, suffix) {
installed = "no"
}
state := "—"
if systemd.IsInstalled(u, prefix, suffix) {
state = systemd.Status(u, prefix, suffix)
}
rows = append(rows, statusRow{
Name: u.Name,
Runtime: u.Runtime,
User: user,
Enabled: enabled,
Installed: installed,
State: state,
Reason: u.DisabledReason,
})
}
// Calculate column widths
cols := []string{"NAME", "RUNTIME", "USER", "ENABLED", "INSTALLED", "STATE", "REASON"}
widths := []int{4, 7, 4, 7, 9, 5, 6}
for _, r := range rows {
vals := []string{r.Name, r.Runtime, r.User, r.Enabled, r.Installed, r.State, r.Reason}
for i, v := range vals {
if len(v) > widths[i] {
widths[i] = len(v)
}
}
}
// Print header
fmt.Println()
printRow(cols, widths)
fmt.Println(strings.Repeat("─", totalWidth(widths)))
// Print rows
for _, r := range rows {
vals := []string{r.Name, r.Runtime, r.User, r.Enabled, r.Installed, r.State, r.Reason}
printRow(vals, widths)
}
fmt.Println()
return nil
}
func printRow(vals []string, widths []int) {
for i, v := range vals {
fmt.Printf("%-*s", widths[i]+2, v)
}
fmt.Println()
}
func totalWidth(widths []int) int {
total := 0
for _, w := range widths {
total += w + 2
}
return total
}