Created
May 23, 2022 17:40
-
-
Save sgolemon/7e520a3a22d04660d89cf2f5b08cb34c to your computer and use it in GitHub Desktop.
Basic CLI script for piping into gists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/php | |
<?php | |
# settings -> developer settings -> personal access token -> 'gist' permission | |
const GISTKEY = ''; | |
const GISTUSER = 'sgolemon'; | |
const GISTPOST = 'https://api.github.com/gists'; | |
$headers = [ | |
'Authorization: token ' . GISTKEY, | |
'Content-type: text/json', | |
'User-agent: sgolemon\'s gist writer', | |
]; | |
$cmd = array_shift($_SERVER['argv']); | |
$public = basename($cmd) === 'git-gist-public'; | |
if (count($_SERVER['argv']) < 1) { | |
fprintf(STDERR, "Usage: $cmd filename [description...]\n"); | |
exit(1); | |
} | |
$filename = array_shift($_SERVER['argv']); | |
$description = implode(' ', $_SERVER['argv']) ?: $filename; | |
$contents = stream_get_contents(STDIN); | |
if (empty($filename) || empty($contents)) { | |
fwrite(STDERR, "Nothing to post\n"); | |
exit(1); | |
} | |
$query = [ | |
'files' => [ $filename => [ 'content' => $contents ] ], | |
'public' => $public, | |
'description' => $description, | |
]; | |
$ch = curl_init(GISTPOST); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($ch); | |
$url = json_decode($result)->html_url ?? null; | |
if (!isset($url)) { | |
fwrite(STDERR, "$result\n"); | |
} else { | |
echo $url, PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment