163 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			V
		
	
	
	
	
	
			
		
		
	
	
			163 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			V
		
	
	
	
	
	
import os
 | 
						|
import net.http
 | 
						|
import json
 | 
						|
import cli
 | 
						|
 | 
						|
const mpr_url = 'https://mpr.makedeb.org/'
 | 
						|
 | 
						|
fn main() {
 | 
						|
	mut app := cli.Command{
 | 
						|
		name: 'ayy'
 | 
						|
		description: 'A simple MPR helper'
 | 
						|
		commands: [
 | 
						|
			cli.Command{
 | 
						|
				name: 'install'
 | 
						|
				usage: '[package name]'
 | 
						|
				description: 'Install packages'
 | 
						|
				execute: install
 | 
						|
			},
 | 
						|
			cli.Command{
 | 
						|
				name: 'search'
 | 
						|
				usage: '[package name]'
 | 
						|
				description: 'Search for packages'
 | 
						|
				execute: search
 | 
						|
				required_args: 1
 | 
						|
			},
 | 
						|
		]
 | 
						|
	}
 | 
						|
	app.setup()
 | 
						|
	app.parse(os.args)
 | 
						|
}
 | 
						|
 | 
						|
fn get_pkg_data(pkg_name string) Rpc_resp {
 | 
						|
	// resp := http.get('$mpr_url/rpc?v=5&type=info&arg[]=$pkg_name') or {
 | 
						|
	// 	println(err)
 | 
						|
	// 	exit(1)
 | 
						|
	// }
 | 
						|
 | 
						|
	resp := http.get('https://vps.anotherfoxguy.com/dl/sw.json') or {
 | 
						|
		println(err)
 | 
						|
		exit(1)
 | 
						|
	}
 | 
						|
 | 
						|
	data := json.decode(Rpc_resp, resp.text) or {
 | 
						|
		println('Failed to parse data: $err')
 | 
						|
		exit(1)
 | 
						|
	}
 | 
						|
	return data
 | 
						|
}
 | 
						|
 | 
						|
fn install(cmd cli.Command) ? {
 | 
						|
	mut deps := map[string]Dependency{}
 | 
						|
	for pkg_name in cmd.args {
 | 
						|
		data := get_pkg_data(pkg_name)
 | 
						|
 | 
						|
		if data.resultcount != 1 {
 | 
						|
			println('Package $pkg_name not found on MPR')
 | 
						|
			exit(1)
 | 
						|
		}
 | 
						|
 | 
						|
		pkg := data.results[0]
 | 
						|
 | 
						|
		collect_deps(pkg.depends, mut deps)
 | 
						|
		collect_deps(pkg.make_depends, mut deps)
 | 
						|
 | 
						|
		for _, dat in deps {
 | 
						|
			match dat.source {
 | 
						|
				.apt { install_apt_package(dat.name) }
 | 
						|
				.mpr { install_mpr_package(dat.name) }
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
fn install_mpr_package(package string) {
 | 
						|
	exe := os.execute('apt install -y $package')
 | 
						|
	if apt_search.exit_code != 0 {
 | 
						|
		println('Warn: $package not found on MPR or apt!!')
 | 
						|
		exit(1)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
fn install_apt_package(package string) {
 | 
						|
	exe := os.execute('apt install -y $package')
 | 
						|
	if apt_search.exit_code != 0 {
 | 
						|
		println('Warn: $package not found on MPR or apt!!')
 | 
						|
		exit(1)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
fn collect_deps(deps []string, mut result map[string]Dependency) {
 | 
						|
	for dep in deps {
 | 
						|
		if dep in result {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		ddata := get_pkg_data(dep)
 | 
						|
		if ddata.resultcount == 1 {
 | 
						|
			result[dep] = Dependency{dep, Source.mpr}
 | 
						|
		} else {
 | 
						|
			apt_search := os.execute('apt-cache show $dep')
 | 
						|
			if apt_search.exit_code != 0 {
 | 
						|
				println('Warn: $dep not found on MPR or apt!!')
 | 
						|
				return
 | 
						|
			}
 | 
						|
			println('aaaaa $apt_search')
 | 
						|
			result[dep] = Dependency{dep, Source.apt}
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
fn search(cmd cli.Command) ? {
 | 
						|
	pkg := cmd.args[0]
 | 
						|
	resp := http.get('$mpr_url/rpc?v=5&type=search&arg=$pkg') or {
 | 
						|
		println(err)
 | 
						|
		exit(1)
 | 
						|
	}
 | 
						|
	data := json.decode(Rpc_resp, resp.text) ?
 | 
						|
	println('Packages found $data.resultcount')
 | 
						|
	for res in data.results {
 | 
						|
		println('\t Name: $res.name')
 | 
						|
		println('\t Description: $res.description')
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
enum Source {
 | 
						|
	mpr
 | 
						|
	apt
 | 
						|
}
 | 
						|
 | 
						|
struct Dependency {
 | 
						|
	name   string
 | 
						|
	source Source
 | 
						|
}
 | 
						|
 | 
						|
struct Rpc_resp {
 | 
						|
	resultcount int
 | 
						|
	results     []Package
 | 
						|
	version     int
 | 
						|
}
 | 
						|
 | 
						|
struct Package {
 | 
						|
	conflicts       []string [json: Conflicts]
 | 
						|
	depends         []string [json: Depends]
 | 
						|
	make_depends    []string [json: MakeDepends]
 | 
						|
	description     string   [json: Description]
 | 
						|
	first_submitted int      [json: FirstSubmitted]
 | 
						|
	id              int      [json: ID]
 | 
						|
	keywords        []string [json: Keywords]
 | 
						|
	lastmodified    int      [json: LastModified]
 | 
						|
	license         []string [json: License]
 | 
						|
	maintainer      string   [json: Maintainer]
 | 
						|
	name            string   [json: Name]
 | 
						|
	num_votes       int      [json: NumVotes]
 | 
						|
	out_of_date     string   [json: OutOfDate]
 | 
						|
	package_base    string   [json: PackageBase]
 | 
						|
	package_base_id int      [json: PackageBaseID]
 | 
						|
	popularity      int      [json: Popularity]
 | 
						|
	provides        []string [json: Provides]
 | 
						|
	replaces        []string [json: Replaces]
 | 
						|
	url             string   [json: URL]
 | 
						|
	url_path        string   [json: URLPath]
 | 
						|
	version         string   [json: Version]
 | 
						|
}
 |