refactor: unify service management under manager struct

This commit is contained in:
Marc 2024-10-04 20:05:33 -04:00
parent daa19167a7
commit a6c937b565
Signed by: marc
GPG key ID: 048E042F22B5DC79
7 changed files with 26 additions and 30 deletions

View file

@ -60,8 +60,7 @@ func getStartCommand() *cobra.Command {
return nil
}
service.CreateService(def)
return nil
return service.NewPodmanServiceManager().Create(def)
},
}

View file

@ -20,9 +20,7 @@ func getStopCommand() *cobra.Command {
serviceName := args[0]
service.StopService(serviceName)
return nil
return service.NewPodmanServiceManager().Stop(serviceName)
},
}

View file

@ -15,12 +15,14 @@ type MockClient struct {
}
}
func (c *MockClient) Create(s service_definition.ServiceDefinition) {
func (c *MockClient) Create(s service_definition.ServiceDefinition) error {
c.calls.Create = append(c.calls.Create, s)
return nil
}
func (c *MockClient) Stop(n string) {
func (c *MockClient) Stop(n string) error {
c.calls.Stop = append(c.calls.Stop, n)
return nil
}
func TestServiceListPostCreatesService(t *testing.T) {

View file

@ -49,7 +49,7 @@ func NewDaemon(host string, port int, serviceClient service.ServiceClient) *Daem
d := &Daemon{Host: host, Port: port}
if serviceClient == nil {
d.Services = &service.PodmanServiceClient{}
d.Services = service.NewPodmanServiceManager()
} else {
d.Services = serviceClient
}

View file

@ -9,14 +9,14 @@ import (
type DummyClient struct{}
func (c DummyClient) Create(d service_definition.ServiceDefinition) {}
func (c DummyClient) Stop(d string) {}
func (c DummyClient) Create(d service_definition.ServiceDefinition) error { return nil }
func (c DummyClient) Stop(d string) error { return nil }
func TestDaemonDefaultsToPodmanClient(t *testing.T) {
d := NewDaemon("host", 0, nil)
clientType := reflect.TypeOf(d.Services).String()
if clientType != reflect.TypeOf(&service.PodmanServiceClient{}).String() {
if clientType != reflect.TypeOf(service.NewPodmanServiceManager()).String() {
t.Errorf("Expected podman client, got %s instead.", clientType)
}
}

View file

@ -5,6 +5,6 @@ import (
)
type ServiceClient interface {
Create(service_definition.ServiceDefinition)
Stop(string)
Create(service_definition.ServiceDefinition) error
Stop(string) error
}

View file

@ -1,29 +1,28 @@
package service
import (
"log"
podman "spud/podman"
service_definition "spud/service_definition"
)
type PodmanServiceClient struct{}
func (c PodmanServiceClient) Create(definition service_definition.ServiceDefinition) {
CreateService(definition)
type ServiceManager interface {
Create(definition service_definition.ServiceDefinition)
Stop(name string)
}
func (c PodmanServiceClient) Stop(name string) {
StopService(name)
type PodmanServiceManager struct{}
func NewPodmanServiceManager() *PodmanServiceManager {
return &PodmanServiceManager{}
}
func CreateService(definition service_definition.ServiceDefinition) {
func (c PodmanServiceManager) Create(definition service_definition.ServiceDefinition) error {
var err error
err = podman.CreatePod(definition.Name, definition.Ports)
if err != nil {
log.Fatalf("%s", err)
return err
}
knownVolumes := map[string]string{}
@ -36,9 +35,11 @@ func CreateService(definition service_definition.ServiceDefinition) {
for _, container := range definition.Containers {
if err = podman.CreateContainer(container, knownVolumes, definition.Name); err != nil {
log.Fatalf("%s", err)
return err
}
}
return nil
}
/*
@ -46,10 +47,6 @@ func CreateService(definition service_definition.ServiceDefinition) {
*
* 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)
}
func (c PodmanServiceManager) Stop(name string) error {
return podman.StopPod(name)
}