From fca61353b7cc255c2212743234c57b655022d3a2 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 2 Jun 2024 23:57:55 -0400 Subject: [PATCH] feat: add command to stop a running service --- main.go | 11 +++++++++++ podman/main.go | 18 ++++++++++++++++++ service/service.go | 13 +++++++++++++ 3 files changed, 42 insertions(+) diff --git a/main.go b/main.go index 3b9e98d..bd63bd8 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,18 @@ var start = &cobra.Command{ }, } +var stop = &cobra.Command{ + Use: "stop", + Short: "Stops a running service and all of its containers.", + Run: func(cmd *cobra.Command, args []string) { + serviceName := args[0] + service.StopService(serviceName) + }, +} + func main() { cli.AddCommand(start) + cli.AddCommand(stop) + cli.Execute() } diff --git a/podman/main.go b/podman/main.go index 050e894..5a5ef51 100644 --- a/podman/main.go +++ b/podman/main.go @@ -59,6 +59,24 @@ func CreatePod(name string, ports []service_definition.PortMapping) error { return nil } +/* + * Stops a running pod. + */ +func StopPod(name string) error { + args := []string{"pod", "stop", name} + + command := exec.Command("podman", args...) + + if err := command.Run(); err != nil { + return err + } + + log.Printf("✅ Stopped pod \"%s\" and child containers.", name) + + return nil + +} + /* * Creates individual containers. */ diff --git a/service/service.go b/service/service.go index 708978b..b616e3e 100644 --- a/service/service.go +++ b/service/service.go @@ -30,3 +30,16 @@ func CreateService(definition service_definition.ServiceDefinition) { } } } + +/* + * Stops a running service. + * + * The service and all its containers are stopped but not deleted. + */ +func StopService(name string) { + err := podman.StopPod(name) + + if err != nil { + log.Fatalf("%s", err) + } +}