diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index e88504e..a95f447 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -25,6 +25,7 @@ USAGE EXAMPLES $ codecrafters submit # Commit changes & run tests + $ codecrafters submit -m "msg" # Commit changes & run tests with a custom commit message $ codecrafters test # Run tests without committing changes $ codecrafters test --previous # Run tests for all previous stages and the current stage without committing changes @@ -77,12 +78,28 @@ func run() error { switch cmd { case "test": testCmd := flag.NewFlagSet("test", flag.ExitOnError) - shouldTestPrevious := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order") + shouldTestPrevious := testCmd.Bool("previous", false, "Run tests for all previous stages and the current stage without committing changes") testCmd.Parse(flag.Args()[1:]) // parse the args after the test command return commands.TestCommand(*shouldTestPrevious) case "submit": - return commands.SubmitCommand() + submitCmd := flag.NewFlagSet("submit", flag.ExitOnError) + + var commitMessage string + defaultCommitMessage := "codecrafters submit" + usage := "Commit changes & run tests with a custom commit message" + submitCmd.StringVar(&commitMessage, "m", defaultCommitMessage, usage) + submitCmd.StringVar(&commitMessage, "message", defaultCommitMessage, usage) + + submitCmd.Parse(flag.Args()[1:]) + if submitCmd.NArg() > 0 { + return fmt.Errorf("Unexpected arguments: use -m or --message to set a custom commit message.") + } + if commitMessage == "" { + return fmt.Errorf("Cannot submit with an empty commit message.") + } + + return commands.SubmitCommand(commitMessage + " [skip ci]") case "task": taskCmd := flag.NewFlagSet("task", flag.ExitOnError) stageSlug := taskCmd.String("stage", "", "view instructions for a specific stage (slug, +N, or -N)") diff --git a/internal/commands/submit.go b/internal/commands/submit.go index a7e5756..981849e 100644 --- a/internal/commands/submit.go +++ b/internal/commands/submit.go @@ -12,7 +12,7 @@ import ( "github.com/getsentry/sentry-go" ) -func SubmitCommand() (err error) { +func SubmitCommand(commitMessage string) (err error) { utils.Logger.Debug().Msg("submit command starts") defer func() { @@ -70,8 +70,7 @@ func SubmitCommand() (err error) { } utils.Logger.Debug().Msgf("committing changes to %s", defaultBranchName) - - commitSha, err := commitChanges(repoDir, "codecrafters submit [skip ci]") + commitSha, err := commitChanges(repoDir, commitMessage) if err != nil { return fmt.Errorf("commit changes: %w", err) }