autoupdater.lua
74 lines · 1.9 KB
eHydra Auto-Updater System
Usage: autoupdater <github_raw_url> <local_filename>
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eHydra_wip/autoupdater.lua
| 1 | -- eHydra Auto-Updater System |
| 2 | -- Downloads and installs programs from GitHub raw links |
| 3 | -- Usage: autoupdater <github_raw_url> <local_filename> |
| 4 | |
| 5 | local args = {...} |
| 6 | |
| 7 | if #args < 2 then |
| 8 | print("Usage: autoupdater <github_raw_url> <local_filename> [folder]") |
| 9 | print("Example: autoupdater https://raw.githubusercontent.com/user/repo/main/file.lua my-program eHydra") |
| 10 | print("Example: autoupdater https://raw.githubusercontent.com/user/repo/main/file.lua quarry quarry") |
| 11 | return |
| 12 | end |
| 13 | |
| 14 | local url = args[1] |
| 15 | local filename = args[2] |
| 16 | local folder = args[3] or "eHydra" -- Default to eHydra folder if not specified |
| 17 | local fileExtension = filename == "README" and ".md" or ".lua" |
| 18 | local programsPath = "programs/" .. folder .. "/" .. filename .. fileExtension |
| 19 | |
| 20 | -- Ensure programs directory and folder exist |
| 21 | if not fs.exists("programs") then |
| 22 | fs.makeDir("programs") |
| 23 | end |
| 24 | if not fs.exists("programs/" .. folder) then |
| 25 | fs.makeDir("programs/" .. folder) |
| 26 | end |
| 27 | |
| 28 | print("eHydra Auto-Updater v1.0") |
| 29 | print("========================") |
| 30 | print("URL: " .. url) |
| 31 | print("Target: " .. programsPath) |
| 32 | print() |
| 33 | |
| 34 | -- Delete existing file if it exists |
| 35 | if fs.exists(programsPath) then |
| 36 | print("Removing existing file...") |
| 37 | fs.delete(programsPath) |
| 38 | end |
| 39 | |
| 40 | -- Download the file |
| 41 | print("Downloading...") |
| 42 | local response = http.get(url) |
| 43 | |
| 44 | if not response then |
| 45 | print("Error: Failed to download from " .. url) |
| 46 | print("Check your internet connection and URL") |
| 47 | return |
| 48 | end |
| 49 | |
| 50 | print("Download successful!") |
| 51 | |
| 52 | -- Write the file |
| 53 | local file = fs.open(programsPath, "w") |
| 54 | if not file then |
| 55 | print("Error: Could not create file " .. programsPath) |
| 56 | response.close() |
| 57 | return |
| 58 | end |
| 59 | |
| 60 | file.write(response.readAll()) |
| 61 | file.close() |
| 62 | response.close() |
| 63 | |
| 64 | print("File saved to: " .. programsPath) |
| 65 | print("Installation complete!") |
| 66 | |
| 67 | -- Make file executable if it's a startup file |
| 68 | if filename == "startup" then |
| 69 | print("Setting up startup file...") |
| 70 | end |
| 71 | |
| 72 | print() |
| 73 | print("You can now run: " .. filename) |
| 74 |