Easy way to get `cargo watch` to emit json / http call?

Is there an easy way to do the following:

  1. I run cargo watch

  2. Every time a recompile is triggered:

if it succeeds, have it POST a http endpoint with:

{status: "success", log: "output generated during the compile"}

If it fails, have it POST a http endpoint with:

{status; "fail", log: "output generated during the compile"}

====

The XY problem here is that right now, cargo watch pipes output to a terminal, which means it's either taking up screen real estate (or hidden).

Instead, I'd like to turn cargo watch into a json-event-generator, where it gets sent to some other program, then I can control how to deal with the output.

(For example, a UI that collects events from multiple sources, only displays headlines for the status=fail ones, and expands the msg on click).

I assume when you say terminal you mean stdout/stderr. If so, you can just redirect the output with something like cargo watch -x [BASH_CMD] &> file.txt.

Your 2nd program could then just read that file.

Unless I'm missing something, the issue here is the 2nd program now needs to figure out:

  1. whether file.txt is complete or is partially written (in the middle of a compile)

  2. be able to separate out the output from the different builds

Well if you don't went the cargo output, what do you need cargo watch for? Sounds more like you need something like the watch functionality contained within. Something like epoll.

A single cargo watch is going to call cargo build multiple times, ~ once per change to the watched src directory.

I want a http call per cargo build; I don't want a single file containing the concatenation of all the cargo build from a single cargo watch.

Imagine I run cargo build then I:

  1. make change1 to src
  2. wait 5 mins; make change2 to src
  3. wait 5 mins; make change3 to src

this is going to trigger 3 cargo builds

I'd like build1.txt build2.txt build3.txt to get sent to some http endpoint, as separate requests.

The above however, would concat build1.txt build2.txt build3.txt into a file output.txt

You can change the command that cargo watch runs -- that could be a script that runs cargo build, captures its output whole, and sends that in your http call.

7 Likes

Are the following assumptions correct ?

  1. We are calling cargo build NOT the builtin build command for the actual buidling.

  2. If #1 is true, we can actually replace cargo watch with a generic watcher?

I.e. the idea is:

  • use carbo build with INCREMENTAL=1 for the build process
  • use any generic watcher to watch for changes, then send data to http endpoint

Yes, cargo-watch is an external 3rd party tool that literally just runs a cargo build command when the source changes. You could replace it with any generic file watch service, but you don't need to and cargo-watch includes multiple special conveniences derived from the fact it knows it's watching a cargo project (like ignoring changes to VCS ignored files).

To ask cargo watch to run a custom command, do something like cargo watch -- my-cargo-wrapper build. my-cargo-wrapper is your simple wrapper which forwards arguments to an invocation of cargo and pushes the output to your endpoint.

1 Like

There's possibly an approach using --message-format json and redirection to a Unix socket... but it sounds like the above approach is much simpler for what you want!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.