From c4eb4a4aea9dae0ef23f7925a4dcc3f23971dadc Mon Sep 17 00:00:00 2001 From: Bobby Best Date: Tue, 4 Jun 2024 11:51:27 -0400 Subject: [PATCH] feat: add ability to specify standalone CLI path --- client/service.go | 2 +- client/service_manager.go | 13 +++++++------ client/service_test.go | 2 +- dsl/client.go | 14 +++++++------- dsl/client_test.go | 2 +- dsl/pact.go | 5 ++++- dsl/publish.go | 5 ++++- dsl/service_mock.go | 2 +- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/client/service.go b/client/service.go index e08ce3b38..beffbfa24 100644 --- a/client/service.go +++ b/client/service.go @@ -17,7 +17,7 @@ import ( // Service is a process wrapper for 3rd party binaries. It will spawn an instance // of the binary and manage the life-cycle and IO of the process. type Service interface { - Setup() + Setup(pactCLIDir string) Stop(pid int) (bool, error) List() map[int]*exec.Cmd Command() *exec.Cmd diff --git a/client/service_manager.go b/client/service_manager.go index d1dfa6657..269094ce9 100644 --- a/client/service_manager.go +++ b/client/service_manager.go @@ -5,12 +5,14 @@ import ( "log" "os" "os/exec" + "path" "sync" "time" ) // ServiceManager is the default implementation of the Service interface. type ServiceManager struct { + PactCLIDir string Cmd string processMap processMap Args []string @@ -20,9 +22,11 @@ type ServiceManager struct { } // Setup the Management services. -func (s *ServiceManager) Setup() { +func (s *ServiceManager) Setup(pactCLIDir string) { log.Println("[DEBUG] setting up a service manager") + s.PactCLIDir = pactCLIDir + s.commandCreatedChan = make(chan *exec.Cmd) s.commandCompleteChan = make(chan *exec.Cmd) s.processMap = processMap{processes: make(map[int]*exec.Cmd)} @@ -97,7 +101,7 @@ func (s *ServiceManager) List() map[int]*exec.Cmd { // Command creates an os command to be run func (s *ServiceManager) Command() *exec.Cmd { - cmd := exec.Command(s.Cmd, s.Args...) + cmd := exec.Command(path.Join(s.PactCLIDir, s.Cmd), s.Args...) env := os.Environ() env = append(env, s.Env...) cmd.Env = env @@ -108,10 +112,7 @@ func (s *ServiceManager) Command() *exec.Cmd { // Start a Service and log its output. func (s *ServiceManager) Start() *exec.Cmd { log.Println("[DEBUG] starting service") - cmd := exec.Command(s.Cmd, s.Args...) - env := os.Environ() - env = append(env, s.Env...) - cmd.Env = env + cmd := s.Command() cmdReader, err := cmd.StdoutPipe() if err != nil { diff --git a/client/service_test.go b/client/service_test.go index 0bb45ee57..767e3109f 100644 --- a/client/service_test.go +++ b/client/service_test.go @@ -19,7 +19,7 @@ func createServiceManager() *ServiceManager { Args: cs, Env: env, } - mgr.Setup() + mgr.Setup("") return mgr } diff --git a/dsl/client.go b/dsl/client.go index 5451da7b1..4342aaaa1 100644 --- a/dsl/client.go +++ b/dsl/client.go @@ -70,11 +70,11 @@ type PactClient struct { } // newClient creates a new Pact client manager with the provided services -func newClient(mockServiceManager client.Service, verificationServiceManager client.Service, messageServiceManager client.Service, publishServiceManager client.Service) *PactClient { - mockServiceManager.Setup() - verificationServiceManager.Setup() - messageServiceManager.Setup() - publishServiceManager.Setup() +func newClient(pactCLIDir string, mockServiceManager client.Service, verificationServiceManager client.Service, messageServiceManager client.Service, publishServiceManager client.Service) *PactClient { + mockServiceManager.Setup(pactCLIDir) + verificationServiceManager.Setup(pactCLIDir) + messageServiceManager.Setup(pactCLIDir) + publishServiceManager.Setup(pactCLIDir) return &PactClient{ pactMockSvcManager: mockServiceManager, @@ -86,8 +86,8 @@ func newClient(mockServiceManager client.Service, verificationServiceManager cli } // NewClient creates a new Pact client manager with defaults -func NewClient() *PactClient { - return newClient(&client.MockService{}, &client.VerificationService{}, &client.MessageService{}, &client.PublishService{}) +func NewClient(pactCLIDir string) *PactClient { + return newClient(pactCLIDir, &client.MockService{}, &client.VerificationService{}, &client.MessageService{}, &client.PublishService{}) } // StartServer starts a remote Pact Mock Server. diff --git a/dsl/client_test.go b/dsl/client_test.go index 7d70f2e5e..7c25e1143 100644 --- a/dsl/client_test.go +++ b/dsl/client_test.go @@ -229,7 +229,7 @@ func createMockClient(success bool) (*PactClient, *ServiceMock) { } }() - d := newClient(svc, svc, svc, svc) + d := newClient("", svc, svc, svc, svc) d.TimeoutDuration = 100 * time.Millisecond return d, svc } diff --git a/dsl/pact.go b/dsl/pact.go index e14ad5fc0..756a77f8d 100644 --- a/dsl/pact.go +++ b/dsl/pact.go @@ -32,6 +32,9 @@ type Pact struct { // Current server for the consumer. Server *types.MockServer + // Directory of Pactflow standalone CLI (blank if CLI is installed on system) + PactCLIDir string + // Pact RPC Client. pactClient Client @@ -158,7 +161,7 @@ func (p *Pact) Setup(startMockServer bool) *Pact { } if p.pactClient == nil { - c := NewClient() + c := NewClient(p.PactCLIDir) c.TimeoutDuration = p.ClientTimeout p.pactClient = c } diff --git a/dsl/publish.go b/dsl/publish.go index 7847b028b..dd742499c 100644 --- a/dsl/publish.go +++ b/dsl/publish.go @@ -25,6 +25,9 @@ type PactName struct { // Publisher is the API to send Pact files to a Pact Broker. type Publisher struct { + // Directory of Pactflow standalone CLI (blank if CLI is installed on system) + PactCLIDir string + pactClient Client // Log levels. @@ -40,7 +43,7 @@ func (p *Publisher) Publish(request types.PublishRequest) error { log.Println("[DEBUG] pact publisher: publish pact") if p.pactClient == nil { - c := NewClient() + c := NewClient(p.PactCLIDir) p.pactClient = c } diff --git a/dsl/service_mock.go b/dsl/service_mock.go index e9d116606..505786033 100644 --- a/dsl/service_mock.go +++ b/dsl/service_mock.go @@ -25,7 +25,7 @@ type ServiceMock struct { } // Setup the Management services. -func (s *ServiceMock) Setup() { +func (s *ServiceMock) Setup(pactCLIDir string) { s.ServicesSetupCalled = true }