feat: add command to stop a running service

This commit is contained in:
Marc 2024-06-02 23:57:55 -04:00
parent c843d35ae5
commit fca61353b7
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 42 additions and 0 deletions

11
main.go
View file

@ -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()
}

View file

@ -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.
*/

View file

@ -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)
}
}