bur/bur.v
2026-05-13 18:00:39 +12:00

232 lines
No EOL
6.5 KiB
V

/*
* ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
* ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
* ░▒▓███████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░
* ascii art from: https://patorjk.com/software/taag/ :)
*/
import os
import net.http
struct PackageInfo {
name string
version string
}
struct Manifest {
packages []PackageInfo
}
const default_mirrors = [
'https://git.curds.dev/chersbobers/bur-packages/raw/branch/main/'
]
fn main() {
args := os.args
if args.len < 2 {
println('commands: bur [install|update|upgrade|remove]')
return
}
cmd := args[1]
if !os.exists('/etc/bur/burmirrors.conf') {
os.mkdir_all('/etc/bur/') or { }
os.write_file('/etc/bur/burmirrors.conf', default_mirrors[0]) or { }
println('made bur conf at /etc/bur/burmirrors.conf')
}
match cmd {
'install' {
if args.len < 3 {
println('error: please provide a package name')
return
}
install_function(args[2])
}
'update' {
update_function()
}
'upgrade' {
if args.len < 3 {
println('error: please provide a package name')
return
}
upgrade_function(args[2])
}
'remove' {
if args.len < 3 {
println('error: please provide a package name')
return
}
remove_function(args[2])
}
else {
println('commands: bur [install|update|upgrade|remove]')
}
}
}
fn get_active_mirrors() []string {
mut list := []string{}
conf_path := '/etc/bur/burmirrors.conf'
if os.exists(conf_path) {
preferred := os.read_file(conf_path) or { '' }.trim_space()
if preferred != '' {
list << preferred
}
}
for m in default_mirrors {
if m !in list {
list << m
}
}
return list
}
fn install_function(name string) {
mut success := false
mut current_hash := ''
active_mirrors := get_active_mirrors()
for mirror in active_mirrors {
url := mirror + name + '/' + name + '.c'
println('trying mirror: ' + mirror)
resp := http.get(url) or {
println('mirror down skipping')
continue
}
if resp.status_code != 200 {
println('not found on this mirror skipping')
continue
}
// Using concatenation to be safe for file paths
tmp_path := '/tmp/' + name + '.c'
os.write_file(tmp_path, resp.body) or {
println('error failed writing to tmp')
return
}
hash_resp := http.get(mirror + name + '/' + name + '.hash') or { continue }
if hash_resp.status_code == 200 {
current_hash = hash_resp.body.trim_space()
}
success = true
break
}
if !success {
println('error: ' + name + ' not found on any mirrors!')
return
}
println('boringly compiling ' + name)
compile_cmd := 'tcc /tmp/' + name + '.c -o /bin/' + name + '.elf'
res := os.execute(compile_cmd)
if res.exit_code == 0 {
println('boringly compiled ' + name + '.elf run ' + name + ' in your terminal to run')
os.rm('/tmp/' + name + '.c') or { }
if current_hash != '' {
os.mkdir_all('/var/db/bur/') or { }
os.write_file('/var/db/bur/' + name + '.hash', current_hash) or { }
}
} else {
println('oh crud ' + name + ' failed to compile!')
println(res.output)
}
}
fn update_function() {
println('checking mirrors for manifest...')
mut success := false
active_mirrors := get_active_mirrors()
for mirror in active_mirrors {
manifest_url := mirror + 'packages.json'
resp := http.get(manifest_url) or { continue }
if resp.status_code != 200 { continue }
os.mkdir_all('/var/db/bur/') or { }
os.write_file('/var/db/bur/packages.json', resp.body) or {
println('failed to save packages to the json :(')
return
}
success = true
break
}
if success {
println('done! list refreshed. run bur upgrade [name] to check for updates.')
} else {
println('error: could not update from any mirrors')
}
}
fn upgrade_function(name string) {
mut remote_hash := ''
mut success := false
active_mirrors := get_active_mirrors()
for mirror in active_mirrors {
hash_url := mirror + name + '/' + name + '.hash'
resp := http.get(hash_url) or { continue }
if resp.status_code != 200 { continue }
remote_hash = resp.body.trim_space()
success = true
break
}
if !success {
println('error: could not check remote hash on any mirror')
return
}
hash_path := '/var/db/bur/' + name + '.hash'
if os.exists(hash_path) {
local_hash := os.read_file(hash_path) or { '' }.trim_space()
if local_hash == remote_hash {
println(name + ' is already the latest version.')
return
}
}
println('new version of ' + name + ' found! upgrading...')
install_function(name)
}
fn remove_function(name string) {
println('removing ' + name + ' play the bugle ;_;')
executable_path := '/bin/' + name + '.elf'
if os.exists(executable_path) {
os.rm(executable_path) or {
println('oh crud could not remove the binary')
}
} else {
println('HEY ' + name + '.elf was not found in /bin/!')
}
hash_path := '/var/db/bur/' + name + '.hash'
if os.exists(hash_path) {
os.rm(hash_path) or { }
}
println('rip ' + name + ' it has been removed')
}