Extract init-neutral unit resolution into service/, add initsys.Init with systemd, openrc and runit backends, selected by 'init:' config or autodetect. OpenRC and runit are root only; rootless units run as system services as that user. Add docs and generated examples for all three.
133 lines
2.5 KiB
Go
133 lines
2.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"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 := 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 !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
|
|
}
|