This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Yopu's profile picture

gitget - An updater for ComputerCraft scripts stored on a github.com

Started by Yopu, 17 March 2013 - 09:57 AM
Yopu #1
Posted 17 March 2013 - 10:57 AM
Gitget is a utility program similar to pastebin that allows a user to download files from github.com.
After manually copying stuff back and forth to pastbin I figured I'd just write a quick script to fetch items from github.
My first "real" script. Looking for some constructive criticism.

http://pastebin.com/G88S5FFD

pastebin get G88S5FFD gitget

Spoiler

--[[ gitget
  An updater for ComputerCraft scripts stored on a github.com
]]--

-- Configuration variables
local sConfigName = "gitget.cfg"

-- Returns full GitHub address
function createAddress(tConfig)
  local sUrl = "https://raw.github.com"
  return (sUrl .. "/" .. tConfig["user"] .. "/" .. tConfig["project"] .. "/" .. tConfig["branch"] .. "/" .. tConfig["filename"])
end

-- Loads a table from file
function loadTable(sPath)
  if not fs.exists(sPath) then
	error("loadTable() file not found: " .. sPath)
  end
  
  if fs.isDir(sPath) then
	error("loadTable() cannot open a directory")
  end
  
  local file = fs.open(sPath, "r")
  local sTable = file.readAll()
  file.close()
  return textutils.unserialize(sTable)
end

-- Saves a table to file
-- Uses saveString()
function saveTable(tData, sPath)
  local sSerializedData = textutils.serialize(tData)
  return saveString(sSerializedData, sPath)
end

-- Saves a string to file
function saveString(sData, sPath)
  if fs.isDir(sPath) then
	error("saveString() cannot save over a directory")
  end
  
  local file = fs.open(sPath, "w")
  file.write(sData)
  file.close()
  return true
end

-- Downloads a file from url provided
function download(sUrl, sPath)
  print("Downloading: " .. sUrl)
  if http then
	local sData = http.get(sUrl)
	
	if sData then
	  print("Fetched file")
	  return saveString(sData.readAll(), sPath)
	else
	  print("Failed to fetch file: " .. sUrl)
	  return false
	end
	
  else
	error("download() needs http api enabled to fetch files")
  end
end

-- Writes a prompt at pos
function promptPos(sPrompt, nX, nY)
  term.setCursorPos(nX, nY)
  term.clearLine()
  term.write(sPrompt)
  return true
end

function assemblePromptTable(tConfig)
  tPrompt = {
	"Gitget - a github file fetcher.",
	"Filename [" .. tConfig["filename"] .. "] :",
	"Branch [" .. tConfig["branch"] .. "] :",
	"Project [" .. tConfig["project"] .. "] :",
	"User [" .. tConfig["user"] .. "] :",
	"Press enter with no input to download.",
  }
  return tPrompt
end

-- Draws the config settings on the screen
-- returns prompt table
function drawPrompt(tPrompt)
  for nKey, sValue in ipairs(tPrompt) do
	promptPos(sValue, 1, nKey)
  end
  return true
end

-- Invokes read() after the line given
function userInput(nPos, tPrompt)
  term.setCursorPos((#tPrompt[nPos] + 1), nPos)
  return read()
end

-- Checks for first run and creates config file
function checkFirstInit()
  if not fs.exists(sConfigName) then
	tInit = {["filename"] = " ", ["branch"] = " ", ["project"] = " ", ["user"] = " ",}
	saveTable(tInit, sConfigName)
  end
end

-- Set some global variables
local nInputPos = 2
local tConfig
local tPrompt
local sInput
local tInputRow = {"prompt", "filename", "branch", "project", "user"}

-- Runtime section
checkFirstInit()
term.clear()
while true do
  tConfig = loadTable(sConfigName)
  tPrompt = assemblePromptTable(tConfig)
  drawPrompt(tPrompt)
  sInput = userInput(nInputPos, tPrompt)
  
  if sInput == "" then
	sInput = tConfig[tInputRow[nInputPos]]
	break
  else
	tConfig[tInputRow[nInputPos]] = sInput
	saveTable(tConfig, sConfigName)
	nInputPos = nInputPos + 1
	if nInputPos > #tInputRow then nInputPos = 2 end
  end
end

term.setCursorPos(1, (#tPrompt + 1))
download(createAddress(tConfig), tConfig["filename"])
CoolisTheName007 #2
Posted 17 March 2013 - 12:00 PM
Hum, to me the problem with github for small scripts has been that pushing commits is slow. But maybe I'm doing it the wrong way. How much time does it take your git shell to push a commit to a remote? Plus, I always have to enter git add -A, git commit, git push, but maybe there's a macro for that.

Btw, just finished a python script that recursively minifies an entire directory using LuaSrcDiet (locals get replaced by smaller locals, comments stripped, ect) and then packs everything in auto-extracting lua-file. I had to to that because I'm using github for doing an os and their api limits requests to 60 per hour, and one file get = one request, so for many files, like in my case, the downloader I found wouldn't do, neither will this one, I suppose.
SuicidalSTDz #3
Posted 17 March 2013 - 12:04 PM
This is pretty nice. Different from the regular programs we get on the forums.

Good job+1 ^_^/>
Mailmanq! #4
Posted 17 March 2013 - 12:10 PM
Good job!
Yopu #5
Posted 17 March 2013 - 12:35 PM
Hum, to me the problem with github for small scripts has been that pushing commits is slow. But maybe I'm doing it the wrong way. How much time does it take your git shell to push a commit to a remote? Plus, I always have to enter git add -A, git commit, git push, but maybe there's a macro for that.

Btw, just finished a python script that recursively minifies an entire directory using LuaSrcDiet (locals get replaced by smaller locals, comments stripped, ect) and then packs everything in auto-extracting lua-file. I had to to that because I'm using github for doing an os and their api limits requests to 60 per hour, and one file get = one request, so for many files, like in my case, the downloader I found wouldn't do, neither will this one, I suppose.

I'm currently using SmartGit and my commits are pushed up in seconds. I not really working on anything big at the moment. Just single file stuff.
CoolisTheName007 #6
Posted 17 March 2013 - 01:17 PM
I'm currently using SmartGit and my commits are pushed up in seconds. I not really working on anything big at the moment. Just single file stuff.
I've been delaying setting it up, but this seems one more reason to do it for good. Thanks.