diff --git a/cli/start_service.go b/cli/start_service.go index d9d9281..b695f86 100644 --- a/cli/start_service.go +++ b/cli/start_service.go @@ -60,8 +60,7 @@ func getStartCommand() *cobra.Command { return nil } - service.CreateService(def) - return nil + return service.NewPodmanServiceManager().Create(def) }, } diff --git a/cli/stop_service.go b/cli/stop_service.go index d68fb04..3814149 100644 --- a/cli/stop_service.go +++ b/cli/stop_service.go @@ -20,9 +20,7 @@ func getStopCommand() *cobra.Command { serviceName := args[0] - service.StopService(serviceName) - - return nil + return service.NewPodmanServiceManager().Stop(serviceName) }, } diff --git a/daemon/api_test.go b/daemon/api_test.go index 925e4fd..f979e4f 100644 --- a/daemon/api_test.go +++ b/daemon/api_test.go @@ -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) { diff --git a/daemon/daemon.go b/daemon/daemon.go index eb020e7..b6cc6db 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -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 } diff --git a/daemon/daemon_test.go b/daemon/daemon_test.go index 9b97a39..fdf73af 100644 --- a/daemon/daemon_test.go +++ b/daemon/daemon_test.go @@ -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) } } diff --git a/service/client.go b/service/client.go index a8dcd38..9536e09 100644 --- a/service/client.go +++ b/service/client.go @@ -5,6 +5,6 @@ import ( ) type ServiceClient interface { - Create(service_definition.ServiceDefinition) - Stop(string) + Create(service_definition.ServiceDefinition) error + Stop(string) error } diff --git a/service/service.go b/service/service.go index 7ed7ef9..da88f10 100644 --- a/service/service.go +++ b/service/service.go @@ -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) }