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"
)
var activeCmd = &cobra.Command{
Use: "active",
var startallCmd = &cobra.Command{
Use: "startall",
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'.`,
RunE: runActive,
RunE: runStartall,
}
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)
if err != nil {
return err
@@ -29,7 +29,6 @@ func runActive(cmd *cobra.Command, args []string) error {
prefix, suffix := cfg.Prefix, cfg.Suffix
// Sort by order then name for deterministic startup sequence
units := make([]config.Unit, len(cfg.Units))
copy(units, cfg.Units)
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)
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 {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
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
}