From f47f8a68f370253382f4fd9b54acd69de598d315 Mon Sep 17 00:00:00 2001 From: Brandon Sprague Date: Wed, 14 Feb 2024 10:26:27 -0800 Subject: [PATCH] Address review comments --- async/req.go | 40 ++++++++++++++-------------------------- cmd/parser/main.go | 2 +- cmd/runner/main.go | 4 ++-- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/async/req.go b/async/req.go index 5149c3e..28bf8d4 100644 --- a/async/req.go +++ b/async/req.go @@ -10,38 +10,26 @@ import ( "github.com/RMI/pacta/task" ) -func ParsePortfolioReq() (*task.ParsePortfolioRequest, error) { - taskStr := os.Getenv("PARSE_PORTFOLIO_REQUEST") - if taskStr == "" { - return nil, errors.New("no PARSE_PORTFOLIO_REQUEST given") - } - var task task.ParsePortfolioRequest - if err := json.NewDecoder(strings.NewReader(taskStr)).Decode(&task); err != nil { - return nil, fmt.Errorf("failed to load ParsePortfolioRequest: %w", err) - } - return &task, nil +func LoadParsePortfolioRequestFromEnv() (*task.ParsePortfolioRequest, error) { + return loadFromEnv[task.ParsePortfolioRequest]("PARSE_PORTFOLIO_REQUEST", "ParsePortfolioRequest") } -func CreateAuditReq() (*task.CreateAuditRequest, error) { - car := os.Getenv("CREATE_AUDIT_REQUEST") - if car == "" { - return nil, errors.New("no CREATE_AUDIT_REQUEST was given") - } - var task task.CreateAuditRequest - if err := json.NewDecoder(strings.NewReader(car)).Decode(&task); err != nil { - return nil, fmt.Errorf("failed to load CreateAuditRequest: %w", err) - } - return &task, nil +func LoadCreateAuditRequestFromEnv() (*task.CreateAuditRequest, error) { + return loadFromEnv[task.CreateAuditRequest]("CREATE_AUDIT_REQUEST", "CreateAuditRequest") +} + +func LoadCreateReportRequestFromEnv() (*task.CreateReportRequest, error) { + return loadFromEnv[task.CreateReportRequest]("CREATE_REPORT_REQUEST", "CreateReportRequest") } -func CreateReportReq() (*task.CreateReportRequest, error) { - crr := os.Getenv("CREATE_REPORT_REQUEST") - if crr == "" { +func loadFromEnv[T any](envVar string, entityName string) (*T, error) { + envStr := os.Getenv(envVar) + if envStr == "" { return nil, errors.New("no CREATE_REPORT_REQUEST was given") } - var task task.CreateReportRequest - if err := json.NewDecoder(strings.NewReader(crr)).Decode(&task); err != nil { - return nil, fmt.Errorf("failed to load CreateReportRequest: %w", err) + var task T + if err := json.NewDecoder(strings.NewReader(envStr)).Decode(&task); err != nil { + return nil, fmt.Errorf("failed to load %q: %w", entityName, err) } return &task, nil } diff --git a/cmd/parser/main.go b/cmd/parser/main.go index a27c214..bd2d91d 100644 --- a/cmd/parser/main.go +++ b/cmd/parser/main.go @@ -113,7 +113,7 @@ func run(args []string) error { return errors.New("no TASK_ID given") } - req, err := async.ParsePortfolioReq() + req, err := async.LoadParsePortfolioRequestFromEnv() if err != nil { return fmt.Errorf("failed to parse portfolio request: %w", err) } diff --git a/cmd/runner/main.go b/cmd/runner/main.go index d87d5c3..664e029 100644 --- a/cmd/runner/main.go +++ b/cmd/runner/main.go @@ -89,10 +89,10 @@ func run(args []string) error { } validTasks := map[task.Type]func(context.Context, task.ID) error{ - task.CreateReport: toRunFn(async.CreateReportReq, func(ctx context.Context, id task.ID, req *task.CreateReportRequest) error { + task.CreateReport: toRunFn(async.LoadCreateReportRequestFromEnv, func(ctx context.Context, id task.ID, req *task.CreateReportRequest) error { return h.CreateReport(ctx, id, req, *azReportContainer) }), - task.CreateAudit: toRunFn(async.CreateAuditReq, h.CreateAudit), + task.CreateAudit: toRunFn(async.LoadCreateAuditRequestFromEnv, h.CreateAudit), } taskID := task.ID(os.Getenv("TASK_ID"))