Extract init-neutral unit resolution into service/, add initsys.Init with systemd, openrc and runit backends, selected by 'init:' config or autodetect. OpenRC and runit are root only; rootless units run as system services as that user. Add docs and generated examples for all three.
51 lines
993 B
Go
51 lines
993 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/warkanum/unitdore/config"
|
|
"github.com/warkanum/unitdore/initsys"
|
|
)
|
|
|
|
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
|
|
ini, err := initsys.New(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !ini.IsInstalled(*u, prefix, suffix) {
|
|
return fmt.Errorf("%s is not installed — run 'unitdore install' first", name)
|
|
}
|
|
|
|
fmt.Printf(" ▶ starting %s...\n", ini.ServiceName(*u, prefix, suffix))
|
|
if err := ini.Start(*u, prefix, suffix); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf(" ✓ started: %s\n", name)
|
|
return nil
|
|
}
|