Files
unitdore/cmd/status.go
sgcommand 203e8e3f04 Phase 3+4: systemd generator, install, active, status
- systemd/generator.go: build .service file content from Unit
- systemd/manager.go: install/uninstall/enable/disable/status via systemctl
- cmd/install.go: write unit files, --dry-run flag, remove disabled units
- cmd/active.go: enable + start units in order
- cmd/status.go: summary table with name/runtime/user/enabled/installed/state
2026-04-03 14:46:25 +02:00

127 lines
2.4 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
}
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) {
installed = "no"
}
state := "—"
if systemd.IsInstalled(u) {
state = systemd.Status(u)
}
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
}