From fe105305089835355697fd86550e6024e56f5ace Mon Sep 17 00:00:00 2001 From: Louis Garman Date: Fri, 13 Sep 2024 08:02:28 +0100 Subject: [PATCH] refactor: a little dup is ok --- internal/module/service.go | 44 +++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/internal/module/service.go b/internal/module/service.go index aacbc23..cb5fed1 100644 --- a/internal/module/service.go +++ b/internal/module/service.go @@ -206,7 +206,13 @@ const InitTask task.Identifier = "init" // Init invokes terraform init on the module. func (s *Service) Init(moduleID resource.ID) (task.Spec, error) { - return s.updateSpec(moduleID, task.Spec{ + mod, err := s.table.Get(moduleID) + if err != nil { + return task.Spec{}, err + } + spec := task.Spec{ + ModuleID: &mod.ID, + Path: mod.Path, Identifier: InitTask, Execution: task.Execution{ TerraformCommand: []string{"init"}, @@ -216,23 +222,38 @@ func (s *Service) Init(moduleID resource.ID) (task.Spec, error) { // The terraform plugin cache is not concurrency-safe, so only allow one // init task to run at any given time. Exclusive: s.pluginCache, - }) + } + return spec, nil } func (s *Service) Format(moduleID resource.ID) (task.Spec, error) { - return s.updateSpec(moduleID, task.Spec{ + mod, err := s.table.Get(moduleID) + if err != nil { + return task.Spec{}, err + } + spec := task.Spec{ + ModuleID: &mod.ID, + Path: mod.Path, Execution: task.Execution{ TerraformCommand: []string{"fmt"}, }, - }) + } + return spec, nil } func (s *Service) Validate(moduleID resource.ID) (task.Spec, error) { - return s.updateSpec(moduleID, task.Spec{ + mod, err := s.table.Get(moduleID) + if err != nil { + return task.Spec{}, err + } + spec := task.Spec{ + ModuleID: &mod.ID, + Path: mod.Path, Execution: task.Execution{ TerraformCommand: []string{"validate"}, }, - }) + } + return spec, nil } func (s *Service) List() []*Module { @@ -260,14 +281,3 @@ func (s *Service) SetCurrent(moduleID, workspaceID resource.ID) error { }) return err } - -// updateSpec updates the task spec with common module settings. -func (s *Service) updateSpec(moduleID resource.ID, spec task.Spec) (task.Spec, error) { - mod, err := s.table.Get(moduleID) - if err != nil { - return task.Spec{}, err - } - spec.ModuleID = &mod.ID - spec.Path = mod.Path - return spec, nil -}