Skip to main content

easy_install/
lib.rs

1mod artifact;
2mod download;
3mod env;
4mod install;
5mod manfiest;
6mod tool;
7mod ty;
8
9use anyhow::Result;
10use clap::Parser;
11use github_proxy::Proxy;
12use guess_target::Target;
13use tool::add_output_to_path;
14
15#[derive(Debug, Clone)]
16pub struct InstallConfig {
17    pub dir: Option<String>,
18    pub name: Vec<String>,
19    pub alias: Option<String>,
20    pub target: Option<Target>,
21    pub retry: usize,
22    pub proxy: Proxy,
23    pub timeout: u64,
24}
25
26impl Default for InstallConfig {
27    fn default() -> Self {
28        Self {
29            dir: None,
30            name: Vec::new(),
31            alias: None,
32            target: None,
33            retry: 3,
34            proxy: Proxy::Github,
35            timeout: 600,
36        }
37    }
38}
39
40impl InstallConfig {
41    pub fn new(
42        dir: Option<String>,
43        name: Vec<String>,
44        alias: Option<String>,
45        target: Option<Target>,
46        retry: usize,
47        proxy: Proxy,
48        timeout: u64,
49    ) -> Self {
50        Self {
51            dir,
52            name,
53            alias,
54            target,
55            retry,
56            proxy,
57            timeout,
58        }
59    }
60}
61
62#[derive(Parser, Debug, Clone)]
63#[command(version, about, long_about = None)]
64pub struct Args {
65    #[arg()]
66    pub url: String,
67
68    #[arg(short, long)]
69    pub dir: Option<String>,
70
71    #[arg(long, default_value_t = false)]
72    pub install_only: bool,
73
74    #[arg(long, value_delimiter = ',')]
75    pub name: Vec<String>,
76
77    #[arg(long)]
78    pub alias: Option<String>,
79
80    #[arg(long)]
81    pub target: Option<Target>,
82
83    #[arg(long, default_value_t = 3)]
84    pub retry: usize,
85
86    #[arg(long, default_value = "github")]
87    pub proxy: Proxy,
88
89    #[arg(
90        long,
91        default_value_t = 600,
92        help = "Network request timeout in seconds"
93    )]
94    pub timeout: u64,
95}
96
97impl Default for Args {
98    fn default() -> Self {
99        Self {
100            url: String::new(),
101            dir: None,
102            install_only: false,
103            name: vec![],
104            alias: None,
105            target: None,
106            retry: 3,
107            proxy: Proxy::default(),
108            timeout: 600,
109        }
110    }
111}
112
113impl Args {
114    pub fn to_install_config(&self) -> InstallConfig {
115        InstallConfig::new(
116            self.dir.clone(),
117            self.name.clone(),
118            self.alias.clone(),
119            self.target,
120            self.retry,
121            self.proxy,
122            self.timeout,
123        )
124    }
125}
126
127pub async fn run_main(args: Args) -> Result<()> {
128    let url = args.url.clone();
129    let install_only = args.install_only;
130    let config = args.to_install_config();
131
132    let output = install::install(&url, &config).await?;
133    if !install_only {
134        add_output_to_path(&output);
135    }
136    if output.is_empty() {
137        println!("No file installed from {url}");
138    }
139    Ok(())
140}