- 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
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/warkanum/unitdore/config"
|
|
"github.com/warkanum/unitdore/initsys"
|
|
)
|
|
|
|
var stopAllFlag bool
|
|
|
|
var stopCmd = &cobra.Command{
|
|
Use: "stop <name> | --all",
|
|
Short: "Stop a unit by name (or all with --all)",
|
|
Long: `Stop stops the named installed unit.
|
|
|
|
With --all (same as 'stopall') it stops every installed unit in reverse
|
|
startup order, one at a time.`,
|
|
Args: nameOrAll(&stopAllFlag),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg, ini, err := loadInit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if stopAllFlag {
|
|
stopAll(cfg, ini, os.Stdout)
|
|
return nil
|
|
}
|
|
return stopOne(cfg, ini, args[0], os.Stdout)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
stopCmd.Flags().BoolVar(&stopAllFlag, "all", false, "stop all installed units")
|
|
rootCmd.AddCommand(stopCmd)
|
|
}
|
|
|
|
func stopOne(cfg *config.Config, ini initsys.Init, name string, w io.Writer) error {
|
|
u, err := installedUnit(cfg, ini, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(w, " ■ stopping %s...\n", ini.ServiceName(*u, cfg.Prefix, cfg.Suffix))
|
|
if err := ini.Stop(*u, cfg.Prefix, cfg.Suffix); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(w, " ✓ stopped: %s\n", name)
|
|
return nil
|
|
}
|