Gists

Here's some YS code that you can use to post a secret GitHub Gist from the command line. After posting your files or stdin, it will print the URL of the newly created Gist.

Put this code in file called gist, make it executable, and put it in your $PATH.

You can then use it like this:

$ gist file1 file2
https://gist.github.com/ingydotnet/aad4bb7c23b998d
$ find stuff | gist   # post stdin
https://gist.github.com/ingydotnet/ff38f66cbc0474e

To use this YS hack, you'll need to set the GITHUB_API_TOKEN environment variable to your GitHub API token External link .

Here's the YS code:

#!/usr/bin/env ys-0

api-url =: 'https://api.github.com/gists'

defn main(*files):
  files =: files.?.if(files ['-'])
    .mapv(\((_ == '-').if('/dev/stdin' _)))

  api-key =: ENV.GITHUB_API_TOKEN ||
    die('No GitHub API token found in GITHUB_API_TOKEN')

  request =::
    :headers:
      :Content-Type: application/json
      :Authorization:: "token $api-key"
    :body::
      json/dump::
        :description: ''
        :public: false
        :files::
          reduce _ {} files:
            fn(files file):
              merge files::
                ! fs-basename(file):
                  content:: file:slurp

  response =:
    try:
      http/post api-url: request
      catch e:
        e =: ex-data(e)
        warn:
          yaml/dump::
            status:: e.status
            body:: e.body:json/load
            api-key:: api-key
        exit: 1

  when-not 200 <= response.status <= 201:
    die: ("Gist request failed:\n\n" +
          json/load(response.body):yaml/dump)

  say: json/load(response.body).html_url