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 return nil
} }
service.CreateService(def) return service.NewPodmanServiceManager().Create(def)
return nil
}, },
} }

View file

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

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) 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) c.calls.Stop = append(c.calls.Stop, n)
return nil
} }
func TestServiceListPostCreatesService(t *testing.T) { 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} d := &Daemon{Host: host, Port: port}
if serviceClient == nil { if serviceClient == nil {
d.Services = &service.PodmanServiceClient{} d.Services = service.NewPodmanServiceManager()
} else { } else {
d.Services = serviceClient d.Services = serviceClient
} }

View file

@ -9,14 +9,14 @@ import (
type DummyClient struct{} type DummyClient struct{}
func (c DummyClient) Create(d service_definition.ServiceDefinition) {} func (c DummyClient) Create(d service_definition.ServiceDefinition) error { return nil }
func (c DummyClient) Stop(d string) {} func (c DummyClient) Stop(d string) error { return nil }
func TestDaemonDefaultsToPodmanClient(t *testing.T) { func TestDaemonDefaultsToPodmanClient(t *testing.T) {
d := NewDaemon("host", 0, nil) d := NewDaemon("host", 0, nil)
clientType := reflect.TypeOf(d.Services).String() 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) t.Errorf("Expected podman client, got %s instead.", clientType)
} }
} }

View file

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

View file

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