1use crate::error::ConversionError;
2use crate::proxy::Proxy;
3use crate::resource::Resource;
4use std::str::FromStr as _;
5
6pub fn run() -> Result<(), Box<dyn std::error::Error>> {
7 let args: Vec<String> = std::env::args().collect();
8
9 if args.len() < 3 {
10 print_usage();
11 std::process::exit(1);
12 }
13
14 let proxy_type_str = &args[1];
15 let resource_type_str = &args[2];
16
17 let proxy_type = Proxy::from_str(proxy_type_str)?;
19
20 let resource = match resource_type_str.to_lowercase().as_str() {
22 "file" => {
23 if args.len() != 7 {
24 return Err(ConversionError::InvalidArguments(
25 "file requires 4 arguments: owner repo reference path".to_string(),
26 )
27 .into());
28 }
29 Resource::file(
30 args[3].clone(),
31 args[4].clone(),
32 args[5].clone(),
33 args[6].clone(),
34 )
35 }
36 "release" => {
37 if args.len() != 7 {
38 return Err(ConversionError::InvalidArguments(
39 "release requires 4 arguments: owner repo tag name".to_string(),
40 )
41 .into());
42 }
43 Resource::release(
44 args[3].clone(),
45 args[4].clone(),
46 args[5].clone(),
47 args[6].clone(),
48 )
49 }
50 _ => {
51 return Err(ConversionError::InvalidResourceType(resource_type_str.clone()).into());
52 }
53 };
54
55 match resource.url(&proxy_type) {
57 Some(url) => {
58 println!("{}", url);
59 Ok(())
60 }
61 None => {
62 eprintln!(
63 "Error: {} proxy does not support {:?} resources",
64 proxy_type, resource
65 );
66 eprintln!(
67 "Note: jsdelivr and statically do not support release assets from /releases/download/"
68 );
69 std::process::exit(1);
70 }
71 }
72}
73
74fn print_usage() {
75 eprintln!("Usage: github-proxy <proxy-type> <resource-type> <args...>");
76 eprintln!();
77 eprintln!("Proxy Types:");
78 eprintln!(" github Native Github (no proxy)");
79 eprintln!(" gh-proxy gh-proxy.com service");
80 eprintln!(" xget xget.xi-xu.me service");
81 eprintln!(" jsdelivr cdn.jsdelivr.net service (files only)");
82 eprintln!(" statically cdn.statically.io service (files only)");
83 eprintln!();
84 eprintln!("Resource Types:");
85 eprintln!(" file <owner> <repo> <reference> <path>");
86 eprintln!(" Generate URL for a raw file in repository");
87 eprintln!(" reference can be: branch, tag, commit hash, or refs/heads/branch");
88 eprintln!();
89 eprintln!(" release <owner> <repo> <tag> <name>");
90 eprintln!(" Generate URL for a release asset");
91 eprintln!();
92 eprintln!("Examples:");
93 eprintln!(" github-proxy xget file easy-install easy-install main install.sh");
94 eprintln!(" github-proxy xget file owner repo refs/heads/main src/lib.rs");
95 eprintln!(
96 " github-proxy gh-proxy release easy-install easy-install nightly ei-aarch64-apple-darwin.tar.gz"
97 );
98}