Created
November 29, 2017 07:53
-
-
Save KeithHanson/556f48d8975dacfb0abd4c10e0b1dcc7 to your computer and use it in GitHub Desktop.
A ComputerCraft Gist
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
-- Load a helper script to decode JSON from github | |
os.loadAPI("tools/json") | |
-- This fancy {...} thing means all of | |
-- the command line arguments passed in | |
-- gist get url would produce "get" in position 1 | |
args = {...} | |
-- The command the user is trying to run | |
-- should be "put" or "get" | |
command = args[1] | |
gistIDOrFile = args[2] | |
if args[1] == nil or args[2] == nil then | |
print("Usage: gist put filename") | |
end | |
-- Allows gists to be posted | |
-- as KeithHanson on Github | |
githubOauthToken = "token 8402f278e0dafc1c93870aebb5b71221ddd692e1" | |
httpHeaders = { Authorization = githubOauthToken } | |
baseURL = "https://api.github.com/" | |
if command == "put" then | |
gistFileName = (gistIDOrFile .. ".lua") | |
print("Creating: " .. gistFileName) | |
-- Open the file, then read it, then close it | |
fileHandle = fs.open(gistIDOrFile, "r") | |
gistContent = fileHandle.readAll() | |
fileHandle.close() | |
-- Craft the data to post to github | |
postData = {} | |
postData.description = "A ComputerCraft Gist" | |
postData.public = true | |
postData.files = {} | |
postData.files[gistFileName] = {} | |
postData.files[gistFileName].content = gistContent | |
postJSON = json.encode(postData) | |
postURL = (baseURL .. "gists") | |
webHandle = http.post(postURL, postJSON, httpHeaders) | |
webResult = webHandle.readAll() | |
-- Use this to see the actual payload returned | |
-- textutils.pagedPrint(webResult) | |
jsonResults = json.decode(webResult) | |
print(jsonResults["html_url"]) | |
else | |
print("Only the put command is support.") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment