import { Step } from "./step"; import KafkaStep from "./decorators/kafka.step"; import MetricStep from "./decorators/metric.step"; import { config } from "../utils/config"; import { AbstractStep } from "./abstractStep"; export interface StepConfig { kafkaLog?: boolean; metrics?: boolean; dryRun?: boolean; conditionalRun?: boolean; } export class StepBuilder { private config: StepConfig; private readonly step: AbstractStep; private CONDITIONAL_ACTIVATION_PREFIX = "STEP_ACTIVATE_"; constructor(step: AbstractStep) { this.step = step; this.config = {}; } public kafka(): StepBuilder { this.config.kafkaLog = true; return this; } public metrics(): StepBuilder { this.config.metrics = true; return this; } public dryRun(): StepBuilder { this.config.dryRun = true; return this; } /* * Behaviour depends on env variable STEP_ACTIVATE_ where is the step name * true: step is activated normaly * false: step is not run * dry: step is dry run */ public conditionalRun(): StepBuilder { this.config.conditionalRun = true; return this; } public build(): Step { let step: Step = this.step; const conditional: string = config[this.CONDITIONAL_ACTIVATION_PREFIX + step.getName()] ?? "false"; const noRun: boolean = !!this.config.conditionalRun && conditional === "false"; const dryRun: boolean = !!this.config.dryRun || (!!this.config.conditionalRun && conditional === "dry"); if (noRun) { this.step.noRun(); } else if (dryRun) { this.step.dryRun(); } if (this.config.metrics) { step = new MetricStep(step); } if (this.config.kafkaLog) { step = new KafkaStep(step); } return step; } }