diff --git a/.changeset/patch-add-multiple-workflows.md b/.changeset/patch-add-multiple-workflows.md new file mode 100644 index 00000000000..82f7ae99245 --- /dev/null +++ b/.changeset/patch-add-multiple-workflows.md @@ -0,0 +1,5 @@ +--- +"gh-aw": patch +--- + +Fix `--name` flag to only apply to the first workflow when adding multiple workflows with `gh aw add workflow1 workflow2 ...`. Previously the name was applied to all workflows, causing each to overwrite the previous one. diff --git a/pkg/cli/add_command.go b/pkg/cli/add_command.go index 8a105dc8511..56e4348652a 100644 --- a/pkg/cli/add_command.go +++ b/pkg/cli/add_command.go @@ -73,7 +73,7 @@ Workflow specifications: - Local wildcard: "./*.md" or "./dir/*.md" (adds all .md files matching pattern) - Version can be tag, branch, or SHA (for remote workflows) -The -n flag allows you to specify a custom name for the workflow file (only applies to the first workflow when adding multiple). +The -n flag allows you to specify a custom name for the workflow file (not allowed when adding multiple workflows at once). The --dir flag allows you to specify the workflow directory (default: .github/workflows). The --create-pull-request flag creates a pull request with the workflow changes. The --force flag overwrites existing workflow files. @@ -101,6 +101,11 @@ Note: For guided interactive setup, use the 'add-wizard' command instead.`, noStopAfter, _ := cmd.Flags().GetBool("no-stop-after") stopAfter, _ := cmd.Flags().GetString("stop-after") disableSecurityScanner, _ := cmd.Flags().GetBool("disable-security-scanner") + + if nameFlag != "" && len(workflows) > 1 { + return errors.New("--name flag cannot be used when adding multiple workflows at once") + } + if err := validateEngine(engineOverride); err != nil { return err } diff --git a/pkg/cli/add_command_test.go b/pkg/cli/add_command_test.go index fb1ad142cdd..2428107d986 100644 --- a/pkg/cli/add_command_test.go +++ b/pkg/cli/add_command_test.go @@ -337,3 +337,15 @@ func TestAddCommandArgs(t *testing.T) { err = cmd.Args(cmd, []string{"workflow1", "workflow2"}) require.NoError(t, err, "Should not error with multiple arguments") } + +// TestAddMultipleWorkflowsNameFlag verifies that --name is not allowed when multiple workflows are specified. +func TestAddMultipleWorkflowsNameFlag(t *testing.T) { + cmd := NewAddCommand(validateEngineStub) + + // Simulate calling the command with --name and multiple workflow arguments + cmd.SetArgs([]string{"workflow1", "workflow2", "--name", "custom-name"}) + + err := cmd.Execute() + require.Error(t, err, "Should error when --name is used with multiple workflows") + assert.Contains(t, err.Error(), "--name flag cannot be used when adding multiple workflows", "Error should mention --name restriction") +}