diff --git a/cmd/build/cmd_workflow.go b/cmd/build/cmd_workflow.go index 91a52a9..8a3f3d6 100644 --- a/cmd/build/cmd_workflow.go +++ b/cmd/build/cmd_workflow.go @@ -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 diff --git a/pkg/api/provider.go b/pkg/api/provider.go index 6653234..4afadfc 100644 --- a/pkg/api/provider.go +++ b/pkg/api/provider.go @@ -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