bitfield / script

Making it easy to write shell-like scripts in Go
MIT License
5.55k stars 314 forks source link

Suggestion: `./go-script -c 'script.Stdin()...'` tool for running these #183

Open simonw opened 1 year ago

simonw commented 1 year ago

I built a little Bash script to let you do this: https://til.simonwillison.net/bash/go-script

cat file.txt | ./goscript.sh -c 'script.Stdin().Column(1).Freq().First(10).Stdout()'

It supports this too:

cat file.txt | ./goscript.sh top10.goscript

And this, saved as top10.goscript and made executable:

#!/tmp/goscript.sh
script.Stdin().Column(1).Freq().First(10).Stdout()

Then run like this:

cat file.txt | ./top10.goscript 

It's a fun little hack in Bash, but maybe something like this could be useful as an actual Go program distributed as part of this project.

simonw commented 1 year ago

Fun follow-up idea: https://fedi.simonwillison.net/@simon/110922873440211227

you could hash the script body, create ~/.cache/goscript/HASH/script.go, save a compiled binary to that folder and run that directly in the future.

bitfield commented 1 year ago

Super nice, thanks @simonw! I'll have a play with this.

alexbozhenko commented 1 year ago

@simonw have you seen https://github.com/erning/gorun ?

andrewcrook commented 8 months ago

Was just going to suggest something similar but in the form of a hash bang in the go file used as a script ....

#! /usr/bin/env go-script

andrewcrook commented 8 months ago

@alexbozhenko

https://github.com/erning/gorun

looks like exactly what I was looking for