GithubHelp home page GithubHelp logo

Comments (11)

garious avatar garious commented on August 26, 2024 1

Thanks. Bonus points for an error message when you try to pipe a nonexistent file into stdin. In the C world, the function you'd use is called isatty(). I trip on that regularly when I mistakenly run the client demo from the src directory the mint.json is up in the top-level directory.

from solana.

rlkelly avatar rlkelly commented on August 26, 2024

I'm going to take a stab at this, I'll try to have a PR ready for Monday

from solana.

garious avatar garious commented on August 26, 2024

@rleungx, did you test-drive that "bonus points" case? Easy to imagine that was implicitly handled by your patch. If not, I'll create a separate issue for that.

from solana.

rleungx avatar rleungx commented on August 26, 2024

Hi, @garious. I'm not sure about how isatty() works here. I've changed some code as below.

    let mint: Mint = if stdin_isatty() {
        serde_json::from_reader(stdin()).unwrap_or_else(|e| {
            eprintln!("failed to parse json: {}", e);
            exit(1);
        })
    } else {
        eprintln!("file may not exist");
        exit(1);
    };

Is this correct?

from solana.

garious avatar garious commented on August 26, 2024

"Look before you leap". Are you able to detect no file was passed to stdin before invoking from_reader?

from solana.

rleungx avatar rleungx commented on August 26, 2024

My bad. I've edited the comment. How it looks now?

from solana.

garious avatar garious commented on August 26, 2024

How about something like the following? That way if isatty isn't available on some platform (Windows?), we can compile it out without any additional restructuring.

    if !stdin_isatty() {
        eprintln!("file may not exist");
        exit(1);
    };
    let mint: Mint = serde_json::from_reader(stdin()).unwrap_or_else(|e| {
        eprintln!("failed to parse json: {}", e);
        exit(1);
    });

from solana.

rleungx avatar rleungx commented on August 26, 2024

I just found that we should use stdin_isatty() inside unwrap_or_else as I did before. Because it will always exit if we put stdin_isatty() before from_reader , right?

from solana.

garious avatar garious commented on August 26, 2024

There's 4 cases:

  • No "cat mint.json |"
  • mint.json isn't in the current directory
  • mint.json has corrupt data
  • mint.json is good

I'm just looking for it to exit early if we can detect either of the first two.

from solana.

rleungx avatar rleungx commented on August 26, 2024

Thanks for your explanation! Actually, what I mean is that stdin_isatty will all return true, except the first one. Any idea about how to make it work?

from solana.

garious avatar garious commented on August 26, 2024

That's perfect. So the thing that should change from my last code sample is just the error message. It should read something like, "nothing found on stdin, expected a JSON file".

For the second case, check if the stream is empty and report an error that says something like, "empty file on stdin, expected a JSON file".

And then the last two are good as-is.

from solana.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.