146 lines
3.5 KiB
Rust
146 lines
3.5 KiB
Rust
mod autoremove;
|
|
mod clean;
|
|
mod common;
|
|
mod info;
|
|
mod initdb;
|
|
mod install;
|
|
mod print;
|
|
mod reconfigure;
|
|
mod remove;
|
|
mod repo_admin;
|
|
mod select;
|
|
mod update;
|
|
mod upgrade;
|
|
|
|
use clap::Parser;
|
|
use console::style;
|
|
use rust_i18n::t;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
rust_i18n::i18n!("../locales/packie-cli");
|
|
|
|
#[derive(Debug, Parser)]
|
|
struct Cli {
|
|
#[arg(long)]
|
|
chroot: Option<PathBuf>,
|
|
|
|
#[command(subcommand)]
|
|
subcommand: Subcommand,
|
|
}
|
|
|
|
#[derive(Debug, Parser)]
|
|
enum Subcommand {
|
|
/// Clean cache data
|
|
Clean(clean::Cli),
|
|
|
|
/// Get package information
|
|
Info(info::Cli),
|
|
|
|
/// Explicitly initialize the database
|
|
InitDb(initdb::Cli),
|
|
|
|
/// Install one or more packages
|
|
Install(install::Cli),
|
|
|
|
/// Upgrade installed packages
|
|
Upgrade(upgrade::Cli),
|
|
|
|
/// Print configuration information
|
|
Print(print::Cli),
|
|
|
|
/// Remove one or more packages
|
|
Remove(remove::Cli),
|
|
|
|
/// Auto-remove orphan packages
|
|
Autoremove(autoremove::Cli),
|
|
|
|
/// Administrate repository service
|
|
RepoAdmin(repo_admin::Cli),
|
|
|
|
/// Update repository indexes
|
|
Update(update::Cli),
|
|
|
|
/// Select global link
|
|
Select(select::Cli),
|
|
|
|
/// Run configure scripts
|
|
Reconfigure(reconfigure::Cli),
|
|
}
|
|
|
|
fn main() {
|
|
rust_i18n::set_locale(match std::env::var("LANG").as_deref() {
|
|
Ok("zh_CN.UTF-8") => "zh-CN",
|
|
Ok("ja_JP.UTF-8") => "ja",
|
|
_ => "en",
|
|
});
|
|
|
|
let cli = Cli::parse();
|
|
if let Some(new_root) = cli.chroot
|
|
&& let Err(err) = chroot(&new_root)
|
|
{
|
|
eprintln!("{}", style(t!("main.error", error = err)).red());
|
|
std::process::exit(1);
|
|
}
|
|
let result = match cli.subcommand {
|
|
Subcommand::Clean(cli) => clean::main(cli),
|
|
Subcommand::Info(cli) => info::main(cli),
|
|
Subcommand::InitDb(cli) => initdb::main(cli),
|
|
Subcommand::Install(cli) => install::main(cli),
|
|
Subcommand::Upgrade(cli) => upgrade::main(cli),
|
|
Subcommand::Print(cli) => print::main(cli),
|
|
Subcommand::Remove(cli) => remove::main(cli),
|
|
Subcommand::Autoremove(cli) => autoremove::main(cli),
|
|
Subcommand::RepoAdmin(cli) => repo_admin::main(cli),
|
|
Subcommand::Update(cli) => update::main(cli),
|
|
Subcommand::Select(cli) => select::main(cli),
|
|
Subcommand::Reconfigure(cli) => reconfigure::main(cli),
|
|
};
|
|
if let Err(err) = result {
|
|
eprintln!("{}", style(t!("main.error", error = err.message)).red());
|
|
if let Some(note) = err.note {
|
|
eprintln!("{}", style(t!("main.note", note = note)).blue());
|
|
}
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn chroot(new_root: &Path) -> std::io::Result<()> {
|
|
nix::sched::unshare(nix::sched::CloneFlags::CLONE_NEWUSER)?;
|
|
nix::sched::unshare(nix::sched::CloneFlags::CLONE_NEWNS)?;
|
|
std::os::unix::fs::chroot(new_root)?;
|
|
std::env::set_current_dir("/")?;
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "linux")))]
|
|
fn chroot(new_root: &Path) -> std::io::Result<()> {
|
|
std::os::unix::fs::chroot(new_root)?;
|
|
std::env::set_current_dir("/")?;
|
|
Ok(())
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct Error {
|
|
message: String,
|
|
note: Option<String>,
|
|
}
|
|
impl<E: ToString> From<E> for Error {
|
|
fn from(value: E) -> Self {
|
|
Self {
|
|
message: value.to_string(),
|
|
note: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! error {
|
|
($($all:tt)*) => {
|
|
$crate::Error {
|
|
message: ::rust_i18n::t!($($all)*).into(),
|
|
note: ::std::option::Option::None,
|
|
}
|
|
};
|
|
}
|