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:
64
cmd/startall.go
Normal file
64
cmd/startall.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var startallCmd = &cobra.Command{
|
||||
Use: "startall",
|
||||
Short: "Enable and start all installed, enabled units",
|
||||
Long: `Startall runs 'systemctl enable --now' for all enabled units that have
|
||||
been installed. Units must be installed first via 'unitdore install'.`,
|
||||
RunE: runStartall,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(startallCmd)
|
||||
}
|
||||
|
||||
func runStartall(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
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
|
||||
})
|
||||
|
||||
started := 0
|
||||
failed := 0
|
||||
|
||||
for _, u := range units {
|
||||
if !u.Enabled {
|
||||
continue
|
||||
}
|
||||
if !systemd.IsInstalled(u, prefix, suffix) {
|
||||
fmt.Printf(" ! %s: not installed — run 'unitdore install' first\n", u.Name)
|
||||
continue
|
||||
}
|
||||
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++
|
||||
} else {
|
||||
fmt.Printf(" ✓ started: %s\n", u.Name)
|
||||
started++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\nDone. Started: %d Failed: %d\n", started, failed)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user