Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a non-zero exit-code on error #1

Merged
merged 1 commit into from
Nov 30, 2021
Merged

Conversation

djipko
Copy link
Contributor

@djipko djipko commented Nov 30, 2021

First - thanks for an awesome CLI!!! Here's a small fix for an issue I ran into when gearing up for this year :)

This is a common (miss)-behavior of a lot of CLI utilities that rust
makes very easy to fix - by simply returning a Result type from the main()
function (which we already have). This will default to 1 for all
failures.

This is useful for scripting downloads and submissions, but is also
generally the right behavior of a unix utility on error.

We also remove logging the error as this is the default behavior when
returning result from main anyway.

Tested locally with

$ ./target/debug/aoc d --year 2021 --day 20 # not out yet when tested
Loaded session cookie from "~/.adventofcode.session".
Error: "Puzzle 20 of 2021 is still locked."
$ echo $?
1

@scarvalhojr
Copy link
Owner

That's great, thanks for that!

We could actually take a step further and drop the declaration of the result variable and let the match block return the results. Something like:

    let session_cookie = read_session_cookie(args.value_of("session"));

    match args.value_of("command").unwrap() {
        cmd if cmd == "download" || cmd == "d" => {
            let filename = args.value_of("file").unwrap();
            download_input(&session_cookie, year, day, filename)
        }
        cmd if cmd == "submit" || cmd == "s" => {
            let part = args.value_of("part").unwrap();
            let answer = args.value_of("answer").unwrap();
            submit_answer(&session_cookie, year, day, part, answer)
        }
        cmd if cmd == "read" || cmd == "r" => {
            read_puzzle(&session_cookie, year, day)
        }
        _ => unreachable!(),
    }
}

Wanna try and do that?

@djipko
Copy link
Contributor Author

djipko commented Nov 30, 2021

Sounds good - will re-spin later!

This is a common (miss)-behavior of a lot of CLI utilities that rust
makes very easy to fix - by simply returning a Result type from the main()
function (which we already have). This will default to 1 for all
failures.

This is useful for scripting downloads and submissions, but is also
generally the right behavior of a unix utility on error.

We also remove logging the error as this is the default behavior when
returning result from main anyway.

Tested locally with

  $ ./target/debug/aoc d --year 2021 --day 20  # not out yet when tested
  Loaded session cookie from "~/.adventofcode.session".
  Error: "Puzzle 20 of 2021 is still locked."
  $ echo $?
  1
Copy link
Owner

@scarvalhojr scarvalhojr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@scarvalhojr scarvalhojr merged commit c468277 into scarvalhojr:main Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants