feat(cmd): add start, stop, uninstall, and update commands
This commit is contained in:
46
cmd/start.go
Normal file
46
cmd/start.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user