refactor(build): centralise release workflow target resolution

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 02:57:00 +00:00
parent 5bc2b6dc96
commit 0fca52d975
2 changed files with 58 additions and 33 deletions

View file

@ -43,6 +43,40 @@ type releaseWorkflowInputs struct {
workflowOutputPathSnakeInput string
}
// resolvedWorkflowTargetPath resolves both workflow path inputs and workflow
// output inputs before merging them into the final target path.
//
// inputs := releaseWorkflowInputs{pathInput: "ci/release.yml"}
// path, err := inputs.resolvedWorkflowTargetPath("/tmp/project")
func (inputs releaseWorkflowInputs) resolvedWorkflowTargetPath(projectDir string) (string, error) {
resolvedWorkflowPath, err := resolveReleaseWorkflowInputPathAliases(
projectDir,
inputs.pathInput,
inputs.workflowPathInput,
inputs.workflowPathHyphenInput,
inputs.workflowPathSnakeInput,
)
if err != nil {
return "", err
}
resolvedWorkflowOutputPath, err := resolveReleaseWorkflowOutputPathAliases(
projectDir,
inputs.outputPathInput,
inputs.outputPathHyphenInput,
inputs.outputPathSnakeInput,
inputs.legacyOutputInput,
inputs.workflowOutputPathInput,
inputs.workflowOutputPathHyphenInput,
inputs.workflowOutputPathSnakeInput,
)
if err != nil {
return "", err
}
return build.ResolveReleaseWorkflowInputPathWithMedium(io.Local, projectDir, resolvedWorkflowPath, resolvedWorkflowOutputPath)
}
var releaseWorkflowCmd = &cli.Command{
Use: "workflow",
RunE: func(cmd *cli.Command, args []string) error {
@ -107,32 +141,16 @@ func runReleaseWorkflow(_ context.Context, inputs releaseWorkflowInputs) error {
return coreerr.E("build.runReleaseWorkflow", "failed to get working directory", err)
}
resolvedWorkflowPath, err := resolveReleaseWorkflowInputPathAliases(
projectDir,
inputs.pathInput,
inputs.workflowPathInput,
inputs.workflowPathHyphenInput,
inputs.workflowPathSnakeInput,
)
resolvedWorkflowPath, err := inputs.resolvedWorkflowTargetPath(projectDir)
if err != nil {
return err
}
resolvedWorkflowOutputPath, err := resolveReleaseWorkflowOutputPathAliases(
projectDir,
inputs.outputPathInput,
inputs.outputPathHyphenInput,
inputs.outputPathSnakeInput,
inputs.legacyOutputInput,
inputs.workflowOutputPathInput,
inputs.workflowOutputPathHyphenInput,
inputs.workflowOutputPathSnakeInput,
)
if err != nil {
return err
if err := io.Local.EnsureDir(ax.Dir(resolvedWorkflowPath)); err != nil {
return coreerr.E("build.runReleaseWorkflowInDir", "failed to create release workflow directory", err)
}
return runReleaseWorkflowInDir(projectDir, resolvedWorkflowPath, resolvedWorkflowOutputPath)
return build.WriteReleaseWorkflow(io.Local, resolvedWorkflowPath)
}
// resolveReleaseWorkflowInputPathAliases keeps the CLI error wording stable while

View file

@ -588,6 +588,25 @@ type ReleaseWorkflowRequest struct {
WorkflowOutputPathHyphen string `json:"workflow-output-path"`
}
// resolvedWorkflowTargetPath resolves both workflow path inputs and workflow
// output inputs before merging them into the final target path.
//
// req := ReleaseWorkflowRequest{Path: "ci/release.yml"}
// path, err := req.resolvedWorkflowTargetPath("/tmp/project", io.Local)
func (r ReleaseWorkflowRequest) resolvedWorkflowTargetPath(dir string, medium io.Medium) (string, error) {
outputPath, err := r.resolvedOutputPath(dir, medium)
if err != nil {
return "", err
}
workflowPath, err := r.resolvedWorkflowPath(dir, medium)
if err != nil {
return "", err
}
return build.ResolveReleaseWorkflowInputPathWithMedium(medium, dir, workflowPath, outputPath)
}
// resolvedWorkflowPath resolves the workflow path aliases with the same
// conflict rules as the CLI.
//
@ -649,19 +668,7 @@ func (p *BuildProvider) generateReleaseWorkflow(c *gin.Context) {
}
}
outputPath, err := req.resolvedOutputPath(dir, p.medium)
if err != nil {
c.JSON(http.StatusBadRequest, api.Fail("invalid_request", err.Error()))
return
}
workflowPath, err := req.resolvedWorkflowPath(dir, p.medium)
if err != nil {
c.JSON(http.StatusBadRequest, api.Fail("invalid_request", err.Error()))
return
}
workflowPath, err = build.ResolveReleaseWorkflowInputPathWithMedium(p.medium, dir, workflowPath, outputPath)
workflowPath, err := req.resolvedWorkflowTargetPath(dir, p.medium)
if err != nil {
c.JSON(http.StatusBadRequest, api.Fail("invalid_request", err.Error()))
return