feat: order/delay in service files, enable cmd, --all on unit commands
- install/uninstall/start/stop/enable take <name> or --all; *all kept as shortcuts - add enable (enable at boot + start); startall no longer enables - order/delay resolved by config.ResolveOrder and written into systemd, OpenRC and runit service files (After=/sleep), so they apply at boot - install --all stops and disables units before removing their files - update regenerates all installed, enabled units and removes disabled ones - edit creates the config dir; docs and man page updated - tests: fake Init for cmd, ResolveOrder, backend ordering output
This commit is contained in:
+28
-28
@@ -2,58 +2,58 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/initsys"
|
||||
)
|
||||
|
||||
var uninstallAllFlag bool
|
||||
|
||||
var uninstallCmd = &cobra.Command{
|
||||
Use: "uninstall <name>",
|
||||
Short: "Disable, stop, and remove the service file for a unit",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runUninstall,
|
||||
Use: "uninstall <name> | --all",
|
||||
Short: "Stop, disable and remove the service file for a unit (or all with --all)",
|
||||
Long: `Uninstall stops, disables and removes the service file of the named unit.
|
||||
|
||||
With --all (same as 'uninstallall') it does so for every installed unit in
|
||||
reverse startup order.`,
|
||||
Args: nameOrAll(&uninstallAllFlag),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg, ini, err := loadInit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if uninstallAllFlag {
|
||||
uninstallAll(cfg, ini, os.Stdout)
|
||||
return nil
|
||||
}
|
||||
return uninstallOne(cfg, ini, args[0], os.Stdout)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
uninstallCmd.Flags().BoolVar(&uninstallAllFlag, "all", false, "uninstall all installed units")
|
||||
rootCmd.AddCommand(uninstallCmd)
|
||||
}
|
||||
|
||||
func runUninstall(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
func uninstallOne(cfg *config.Config, ini initsys.Init, name string, w io.Writer) error {
|
||||
u, err := installedUnit(cfg, ini, name)
|
||||
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
|
||||
ini, err := initsys.New(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !ini.IsInstalled(*u, prefix, suffix) {
|
||||
return fmt.Errorf("%s is not installed", name)
|
||||
}
|
||||
|
||||
svcName := ini.ServiceName(*u, prefix, suffix)
|
||||
|
||||
fmt.Printf(" ■ disabling %s...\n", svcName)
|
||||
fmt.Fprintf(w, " ■ disabling %s...\n", ini.ServiceName(*u, prefix, suffix))
|
||||
if err := ini.Disable(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("disabling unit: %w", err)
|
||||
}
|
||||
|
||||
path, _ := ini.UnitPath(*u, prefix, suffix)
|
||||
fmt.Printf(" ✗ removing %s...\n", path)
|
||||
fmt.Fprintf(w, " ✗ removing %s...\n", path)
|
||||
if err := ini.Uninstall(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("removing service file: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf(" ✓ uninstalled: %s\n", name)
|
||||
fmt.Fprintf(w, " ✓ uninstalled: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user