The gist of YS
Yesterday we started learning about the ys
CLI and there's a lot more cool
stuff to learn about it.
But today I want to switch it up and talk about one of my favorite programs that I use many times a day.
You probably know about GitHub gists
.
They are one of the best ways to share text files with others.
Let's take a look!
Making a Gist🔗
Yesterday we talked about a file called sample.yaml
:
!YS-v0
fun-things =::
- ice cream
- reading books
- playing outside
- jumping rope
- building blocks
- finger painting
- bubble blowing
- hide and seek
- coloring books
- toy cars
- dress up clothes
- board games
- stuffed animals
--- !data
name: Sammie
age: 10
favorite things::
take 4:
shuffle(fun-things)
Imagine you have this file on your computer and you want to share it with a friend on Slack.
Just do this:
$ gist sample.yaml
https://gist.github.com/ingydotnet/6a63ddce2bb2a7806f07a3408e0b7723
It's that easy!
Now you can share the URL
with your
friend.
Gists of multiple files🔗
You can also create a gist of multiple files.
Let's say we loaded that sample.yaml
with ys
:
$ ys -Y sample.yaml
name: Sammie
age: 10
favorite things:
- jumping rope
- dress up clothes
- stuffed animals
- building blocks
and now we want to share the output as well:
$ ys -Y sample.yaml > output.yaml
$ gist sample.yaml output.yaml
https://gist.github.com/ingydotnet/18f6ece6ff4cfb54c439160bf9595560
There you go!
That gist
contains both the input and the output of the
ys
command.
gist is YS🔗
So we can see that gists are awesome and the gist
command is super handy.
But why am I writing about it for the Summer of YS??
Well because it's a YS program!
Let's take a look at the source code:
#!/usr/bin/env ys-0
source <(curl '-sL' getys.org/run) "$@" :
# Create a GitHub Gist from a list of files or from stdin
api-token-file =: ENV.GIST_API_TOKEN_FILE |||
"$(ENV.HOME)/.gist-api-token"
api-token =:
cond:
fs-f(api-token-file): api-token-file:read
ENV.GIST_API_TOKEN: ENV.GIST_API_TOKEN
else:
resp =:
sh:: |
bash -c '
echo -n "Enter your GitHub API Token: " >/dev/tty
read -r -s token </dev/tty
echo $token
'
say: ''
resp: .out
api-token =: api-token:chomp
api-url =: 'https://api.github.com/gists'
re-stdin =: /^-(\.\w{1,8})?$/
defn main(*files):
file-map =:
reduce _ {} (files || ['-']):
fn(map file):
m =: file =~ re-stdin
name =: m.if("stdin$(m.1)" file:fs/file-name)
content =: m.if('/dev/stdin' file):read
merge map: +
{name {:content content}}
request =::
:headers:: +{:Authorization "token $api-token"}
:body: !:json/dump
files:: file-map
response =: http/post(api-url request)
when-not 200 <= response.status <= 201:
die: "Gist request failed:\n\n$(response.body)"
say: json/load(response.body).html_url
I can't explain it all right now, but you can see that it makes an HTTP request to the GitHub API to create a gist.
Give it a try!
Gisting gist🔗
Would you like a link to that source code?
I have an idea...
$ gist `which gist`
https://gist.github.com/ingydotnet/6126048571086156db4d145b5b5d802e
That's it for today!