feat(cmd): add startall and stopall commands for unit management

* Implement startall to enable and start all installed units
* Implement stopall to stop and disable all running units
This commit is contained in:
Hein
2026-04-08 13:33:39 +02:00
parent d8c90e4fff
commit 168b81f104
4 changed files with 71 additions and 12 deletions

View File

@@ -9,19 +9,19 @@ import (
"github.com/warkanum/unitdore/systemd" "github.com/warkanum/unitdore/systemd"
) )
var activeCmd = &cobra.Command{ var startallCmd = &cobra.Command{
Use: "active", Use: "startall",
Short: "Enable and start all installed, enabled units", Short: "Enable and start all installed, enabled units",
Long: `Active runs 'systemctl enable --now' for all enabled units that have Long: `Startall runs 'systemctl enable --now' for all enabled units that have
been installed. Units must be installed first via 'unitdore install'.`, been installed. Units must be installed first via 'unitdore install'.`,
RunE: runActive, RunE: runStartall,
} }
func init() { func init() {
rootCmd.AddCommand(activeCmd) rootCmd.AddCommand(startallCmd)
} }
func runActive(cmd *cobra.Command, args []string) error { func runStartall(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath) cfg, err := config.Load(configPath)
if err != nil { if err != nil {
return err return err
@@ -29,7 +29,6 @@ func runActive(cmd *cobra.Command, args []string) error {
prefix, suffix := cfg.Prefix, cfg.Suffix prefix, suffix := cfg.Prefix, cfg.Suffix
// Sort by order then name for deterministic startup sequence
units := make([]config.Unit, len(cfg.Units)) units := make([]config.Unit, len(cfg.Units))
copy(units, cfg.Units) copy(units, cfg.Units)
sort.Slice(units, func(i, j int) bool { sort.Slice(units, func(i, j int) bool {
@@ -50,7 +49,7 @@ func runActive(cmd *cobra.Command, args []string) error {
fmt.Printf(" ! %s: not installed — run 'unitdore install' first\n", u.Name) fmt.Printf(" ! %s: not installed — run 'unitdore install' first\n", u.Name)
continue continue
} }
fmt.Printf(" ▶ enabling: %s...\n", systemd.ServiceName(u, prefix, suffix)) fmt.Printf(" ▶ starting: %s...\n", systemd.ServiceName(u, prefix, suffix))
if err := systemd.Enable(u, prefix, suffix); err != nil { if err := systemd.Enable(u, prefix, suffix); err != nil {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err) fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
failed++ failed++

60
cmd/stopall.go Normal file
View File

@@ -0,0 +1,60 @@
package cmd
import (
"fmt"
"sort"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var stopallCmd = &cobra.Command{
Use: "stopall",
Short: "Stop and disable all running managed units",
Long: `Stopall runs 'systemctl disable --now' for all enabled, installed units.`,
RunE: runStopall,
}
func init() {
rootCmd.AddCommand(stopallCmd)
}
func runStopall(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
prefix, suffix := cfg.Prefix, cfg.Suffix
// Reverse order for shutdown
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
})
stopped := 0
failed := 0
for _, u := range units {
if !systemd.IsInstalled(u, prefix, suffix) {
continue
}
fmt.Printf(" ■ stopping: %s...\n", systemd.ServiceName(u, prefix, suffix))
if err := systemd.Disable(u, prefix, suffix); err != nil {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
failed++
} else {
fmt.Printf(" ✓ stopped: %s\n", u.Name)
stopped++
}
}
fmt.Printf("\nDone. Stopped: %d Failed: %d\n", stopped, failed)
return nil
}

View File

@@ -68,9 +68,9 @@ func (d *Docker) Exists(name string) (bool, error) {
return false, nil return false, nil
} }
out, err := exec.Command(bin, "ps", "-a", "--format", "{{.Names}}").Output() out, err := exec.Command(bin, "ps", "--format", "{{.Names}}").Output()
if err != nil { if err != nil {
return false, fmt.Errorf("docker ps -a: %w", err) return false, fmt.Errorf("docker ps: %w", err)
} }
for _, line := range strings.Split(string(out), "\n") { for _, line := range strings.Split(string(out), "\n") {

View File

@@ -68,9 +68,9 @@ func (p *Podman) Exists(name string) (bool, error) {
return false, nil return false, nil
} }
out, err := exec.Command(bin, "ps", "-a", "--format", "{{.Names}}").Output() out, err := exec.Command(bin, "ps", "--format", "{{.Names}}").Output()
if err != nil { if err != nil {
return false, fmt.Errorf("podman ps -a: %w", err) return false, fmt.Errorf("podman ps: %w", err)
} }
for _, line := range strings.Split(string(out), "\n") { for _, line := range strings.Split(string(out), "\n") {