Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions cmd/codecrafters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)")
Expand Down
5 changes: 2 additions & 3 deletions internal/commands/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand Down
Loading