Skip to content

Commit

Permalink
Merge pull request #20 from codecrafters-io/add-submit-command
Browse files Browse the repository at this point in the history
Refactor codecrafters main.go: remove unused import and error variable, update usage instructions, and handle unknown command error.
  • Loading branch information
rohitpaulk authored Apr 30, 2024
2 parents 6d8a937 + c099bfe commit c0d01ee
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions cmd/codecrafters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"os"

"github.com/codecrafters-io/cli/internal/commands"
"github.com/codecrafters-io/cli/internal/utils"
"github.com/fatih/color"
"github.com/rs/zerolog/log"
)

// Usage: codecrafters test
Expand All @@ -21,14 +19,18 @@ func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `CLI to interact with CodeCrafters
VERSION
%s
USAGE
$ codecrafters [COMMAND]
$ codecrafters [command]
EXAMPLES
$ codecrafters test # Run tests without committing changes
COMMANDS
test: run tests on project in current directory
test: Run tests without committing changes
help: Show usage instructions
VERSION
%s
`, utils.VersionString())

}
Expand All @@ -50,7 +52,11 @@ COMMANDS
err := run()
if err != nil {
red := color.New(color.FgRed).SprintFunc()
fmt.Fprintf(os.Stderr, "%v\n", red(err))

if err.Error() != "" {
fmt.Fprintf(os.Stderr, "%v\n", red(err))
}

os.Exit(1)
}

Expand All @@ -73,10 +79,11 @@ func run() error {
"": // no argument
flag.Usage()
default:
log.Error().Str("command", cmd).Msgf("Unknown command. Did you mean to run \"codecrafters test\"?")
log.Info().Msg("Run codecrafters help for a list of available commands.")
red := color.New(color.FgRed).SprintFunc()
fmt.Printf(red("Unknown command '%s'. Did you mean to run `codecrafters test`?\n\n"), cmd)
fmt.Printf("Run `codecrafters help` for a list of available commands.\n")

return errors.New("bad usage")
return fmt.Errorf("")
}

return nil
Expand Down

0 comments on commit c0d01ee

Please sign in to comment.