This is a trivial Go implementation of the sponge command from the moreutils collection.
sponge is a command that
- Soaks up the entirety of the data on its standard input (until EOF).
- Wrings that data out into a destination file.
It is primarily useful along with a data transformation command that does not itself support intelligent "in-place" editing of files. For example, jq.
Here is an example use of sponge along with jq to do in-place formatting of JSON files.
$ FILE=inventory.json
$ jq . "$FILE" | sponge "$FILE"
Upon first glance, it seems like normal shell output redirection, i.e. jq . "$FILE" >"$FILE" should be sufficient to do this. But that will truncate the file to 0 bytes immediately and there will be nothing for the formatter command to read.
A more capable normal shell approach, (rm -f "$FILE" ; jq . >"$FILE") <"$FILE", is mentioned here. However, that is still not robust if the formatter command itself fails, such as with a parsing error.
With the Go compiler installed, run:
go get github.com/frou/sponge
The sponge command will now be compiled and located in $GOPATH/bin