feat(cmd): add start, stop, uninstall, and update commands

This commit is contained in:
Hein
2026-04-08 13:36:32 +02:00
parent 168b81f104
commit 880023c68b
5 changed files with 234 additions and 0 deletions

46
cmd/start.go Normal file
View File

@@ -0,0 +1,46 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var startCmd = &cobra.Command{
Use: "start <name>",
Short: "Start a unit by name",
Args: cobra.ExactArgs(1),
RunE: runStart,
}
func init() {
rootCmd.AddCommand(startCmd)
}
func runStart(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
name := args[0]
u := cfg.FindUnit(name)
if u == nil {
return fmt.Errorf("unit %q not found", name)
}
prefix, suffix := cfg.Prefix, cfg.Suffix
if !systemd.IsInstalled(*u, prefix, suffix) {
return fmt.Errorf("%s is not installed — run 'unitdore install' first", name)
}
fmt.Printf(" ▶ starting %s...\n", systemd.ServiceName(*u, prefix, suffix))
if err := systemd.Start(*u, prefix, suffix); err != nil {
return err
}
fmt.Printf(" ✓ started: %s\n", name)
return nil
}

46
cmd/stop.go Normal file
View File

@@ -0,0 +1,46 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var stopCmd = &cobra.Command{
Use: "stop <name>",
Short: "Stop a unit by name",
Args: cobra.ExactArgs(1),
RunE: runStop,
}
func init() {
rootCmd.AddCommand(stopCmd)
}
func runStop(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
name := args[0]
u := cfg.FindUnit(name)
if u == nil {
return fmt.Errorf("unit %q not found", name)
}
prefix, suffix := cfg.Prefix, cfg.Suffix
if !systemd.IsInstalled(*u, prefix, suffix) {
return fmt.Errorf("%s is not installed", name)
}
fmt.Printf(" ■ stopping %s...\n", systemd.ServiceName(*u, prefix, suffix))
if err := systemd.Stop(*u, prefix, suffix); err != nil {
return err
}
fmt.Printf(" ✓ stopped: %s\n", name)
return nil
}

55
cmd/uninstall.go Normal file
View File

@@ -0,0 +1,55 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var uninstallCmd = &cobra.Command{
Use: "uninstall <name>",
Short: "Disable, stop, and remove the service file for a unit",
Args: cobra.ExactArgs(1),
RunE: runUninstall,
}
func init() {
rootCmd.AddCommand(uninstallCmd)
}
func runUninstall(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
name := args[0]
u := cfg.FindUnit(name)
if u == nil {
return fmt.Errorf("unit %q not found", name)
}
prefix, suffix := cfg.Prefix, cfg.Suffix
if !systemd.IsInstalled(*u, prefix, suffix) {
return fmt.Errorf("%s is not installed", name)
}
svcName := systemd.ServiceName(*u, prefix, suffix)
fmt.Printf(" ■ disabling %s...\n", svcName)
if err := systemd.Disable(*u, prefix, suffix); err != nil {
return fmt.Errorf("disabling unit: %w", err)
}
path, _ := systemd.UnitPath(*u, prefix, suffix)
fmt.Printf(" ✗ removing %s...\n", path)
if err := systemd.Uninstall(*u, prefix, suffix); err != nil {
return fmt.Errorf("removing service file: %w", err)
}
fmt.Printf(" ✓ uninstalled: %s\n", name)
return nil
}

77
cmd/update.go Normal file
View File

@@ -0,0 +1,77 @@
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var (
updateOrder int
updateEnable bool
updateDisable bool
)
var updateCmd = &cobra.Command{
Use: "update <name>",
Short: "Update a unit's config and recreate its service file",
Args: cobra.ExactArgs(1),
RunE: runUpdate,
}
func init() {
updateCmd.Flags().IntVar(&updateOrder, "order", 0, "set startup order")
updateCmd.Flags().BoolVar(&updateEnable, "enable", false, "enable the unit")
updateCmd.Flags().BoolVar(&updateDisable, "disable", false, "disable the unit")
rootCmd.AddCommand(updateCmd)
}
func runUpdate(cmd *cobra.Command, args []string) error {
if updateEnable && updateDisable {
return errors.New("--enable and --disable are mutually exclusive")
}
cfg, err := config.Load(configPath)
if err != nil {
return err
}
name := args[0]
u := cfg.FindUnit(name)
if u == nil {
return fmt.Errorf("unit %q not found", name)
}
if cmd.Flags().Changed("order") {
u.Order = updateOrder
fmt.Printf(" order → %d\n", u.Order)
}
if updateEnable {
u.Enabled = true
u.DisabledReason = ""
fmt.Printf(" enabled\n")
}
if updateDisable {
u.Enabled = false
fmt.Printf(" disabled\n")
}
if err := cfg.Save(configPath); err != nil {
return err
}
prefix, suffix, serviceUser := cfg.Prefix, cfg.Suffix, cfg.EffectiveServiceUser()
if systemd.IsInstalled(*u, prefix, suffix) {
if err := systemd.Install(*u, prefix, suffix, serviceUser); err != nil {
return fmt.Errorf("recreating service file: %w", err)
}
path, _ := systemd.UnitPath(*u, prefix, suffix)
fmt.Printf(" ✓ service file recreated: %s\n", path)
}
return nil
}