summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2cff8d4dffadf933d879f974cf6d89f73e13d29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
mod cfg;
mod net;
mod pkgtoolkit;

use crate::cfg::config::Config;
use crate::net::{http_package::HTTPPackage, i2p_package::I2PPackage};
#[allow(unused_imports)]
use crate::pkgtoolkit::pkgtools::Package;

use clap::{Args, Parser, Subcommand};
use std::fs::File;
use std::fs::create_dir_all;
use std::io::Write;
use std::path::Path;

#[derive(Parser)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    #[command(about = "Validate .mesk package archive")]
    Validate { path: String },
    #[command(about = "Update all repositories index")]
    Update,
    #[command(about = "Upgrade all packages or a specific package")]
    Upgrade { pkgname: Option<String> },
    #[command(about = "Build package from .mesk ")]
    Build { pkgname: String },
    #[command(about = "Install package from remote or local sources")]
    Install {
        pkgname: String,
        source: Option<String>,
        #[command(flatten)]
        args: RemoteInstallArgs,
    },
    #[command(about = "Uninstall package")]
    Uninstall { pkgname: String },
    #[command(about = "Get package source")]
    GetSource { pkgname: String },
    #[command(about = "Generator of config file")]
    DefaultConfig {
        repo: Option<String>,
        cachedir: Option<String>,
        buildir: Option<String>,
    },
    #[command(about = "Maintaners, links, developers and more info")]
    Credits,
}

#[derive(Args, Clone)]
#[command(about = "Remote install arguments")]
struct RemoteInstallArgs {
    #[arg(short = 'b', long = "bin")]
    #[arg(help = "Install binary package")]
    bin: bool,

    #[arg(short = 't', long = "http")]
    #[arg(help = "Use non-i2p mirror")]
    http: bool,

    #[arg(short = 'c', long = "clean")]
    #[arg(help = "Clean cache before install")]
    clean: bool,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Changed return type to be more general
    let cli: Cli = Cli::parse();

    match &cli.command {
        Commands::Validate { path } => {
            println!("Validating {}", path);
            return Ok(());
        }
        Commands::Build { pkgname } => {
            println!("Building {}", pkgname);
            return Ok(());
        }
        Commands::Install {
            pkgname,
            source: _,
            args,
        } => {
            let config = Config::parse().unwrap();

            if args.http {
                println!("Installing {} via HTTP", pkgname);
                let mut http_client = HTTPPackage::new(config);
                match http_client.fetch_index_http().await {
                    Ok(_) => {
                        log::info!("Index fetched successfully.");
                    }
                    Err(e) => {
                        log::error!("Failed to fetch index: {}", e);
                        return Err(e);
                    }
                }
                match http_client.fetch_package_http(pkgname).await {
                    Ok(_) => {
                        log::info!("Package '{}' installed successfully.", pkgname);
                    }
                    Err(e) => {
                        log::error!("Failed to install package '{}': {}", pkgname, e);
                        return Err(e);
                    }
                }
            } else {
                println!("Installing {} via I2P", pkgname);
                let mut i2p_client = I2PPackage::new(config);

                match i2p_client.fetch_index().await {
                    Ok(_) => {
                        log::info!("Index fetched successfully.");
                    }
                    Err(e) => {
                        log::error!("Failed to fetch index: {}", e);
                        return Err(e);
                    }
                }
                match i2p_client.fetch_package(pkgname).await {
                    Ok(_) => {
                        log::info!("Package '{}' installed successfully.", pkgname);
                    }
                    Err(e) => {
                        log::error!("Failed to install package '{}': {}", pkgname, e);
                        return Err(e);
                    }
                }
            }
            return Ok(());
        }
        Commands::Uninstall { pkgname } => {
            println!("Uninstalling {}", pkgname);
            return Ok(());
        }
        Commands::GetSource { pkgname } => {
            println!("Getting source of {}", pkgname);
            return Ok(());
        }
        Commands::DefaultConfig {
            repo,
            cachedir,
            buildir,
        } => {
            println!("Generating config file");
            if cachedir.is_none() && repo.is_none() && buildir.is_none() {
                let config = Config::default().unwrap();

                println!("---- Start of generated config ----");
                println!("{}", config);
                println!("---- End of generated config ----");

                log::warn!("Writing the default config to /etc/mesk/mesk.toml");

                let path = Path::new("/etc/mesk/mesk.toml");
                create_dir_all(path.parent().unwrap())?;
                let mut file = File::create(path)?;
                file.write_all(config.as_bytes())?;
                println!("Config tool ending work.");
            } else {
                let config = Config::generate(repo, cachedir, buildir).unwrap();

                println!("---- Start of generated config ----");
                println!("{:?}", config);
                println!("---- End of generated config ----");

                log::warn!("Writing the default config to /etc/mesk/mesk.toml");

                let path = Path::new("/etc/mesk/mesk.toml");
                create_dir_all(path.parent().unwrap())?;
                let mut file = File::create(path)?;
                file.write_all(config.as_bytes())?;
                println!("Config tool ending work.");
            }
            return Ok(());
        }
        Commands::Update => {
            let config = Config::parse().unwrap();
            println!("Updating index from {}", config.repo.repo_url);

            let mut i2p_client = I2PPackage::new(config);
            match i2p_client.fetch_index().await {
                Ok(_) => {
                    println!("Index updated successfully.");
                }
                Err(e) => {
                    log::error!("Failed to update index: {}", e);
                    return Err(e);
                }
            }
            return Ok(());
        }
        Commands::Upgrade { pkgname } => {
            println!("Upgrading {}", pkgname.as_deref().unwrap_or("all packages"));
            return Ok(());
        }
        Commands::Credits => {
            println!(
                "CREATED BY: Asya and Namilsk as part of the Anthrill independent Global network distribution project"
            );
            println!("          ");
            println!("The Anthrill project repos: https://codeberg.org/NamelessTeam  ");
        }
    }

    Ok(())
}