Files
unitdore/cmd/status.go
T
warkanum 91811b4fa1 feat: order/delay in service files, enable cmd, --all on unit commands
- install/uninstall/start/stop/enable take <name> or --all; *all kept as shortcuts
- add enable (enable at boot + start); startall no longer enables
- order/delay resolved by config.ResolveOrder and written into systemd,
  OpenRC and runit service files (After=/sleep), so they apply at boot
- install --all stops and disables units before removing their files
- update regenerates all installed, enabled units and removes disabled ones
- edit creates the config dir; docs and man page updated
- tests: fake Init for cmd, ResolveOrder, backend ordering output
2026-10-12 21:31:41 +02:00

125 lines
2.3 KiB
Go

package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/initsys"
)
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
ini, err := initsys.New(cfg)
if err != nil {
return err
}
if len(cfg.Units) == 0 {
fmt.Println("No units configured. Run 'unitdore syncup' to discover containers.")
return nil
}
units := sortUnits(cfg.Units, false)
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 !ini.IsInstalled(u, prefix, suffix) {
installed = "no"
}
state := "—"
if ini.IsInstalled(u, prefix, suffix) {
state = ini.Status(u, prefix, suffix)
}
rows = append(rows, statusRow{
Name: u.Name,
Runtime: runtimeLabel(u),
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
}