Changing how we parsing CMD arguments

This commit is contained in:
2026-09-01 01:32:18 +02:00
parent a6e0134d2e
commit eb6ef2a19f
+68 -40
View File
@@ -14,16 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use clap::{Args, Parser};
use clap::{Args, Parser, Subcommand};
use signal_hook::{consts::SIGINT, iterator::Signals};
use std;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std;
mod hive_system;
mod hive_config;
use crate::hive_config::HiveConfig;
use crate::hive_system::SysUpgradeError;
mod daemon;
@@ -108,34 +109,72 @@ impl std::error::Error for HiveUpError {}
// Define the mutually exclusive group
#[derive(Args, Debug)]
#[group(required = true, multiple = false)] // 'multiple = false' enforces exclusivity
struct HiveUpMode {
/// Check for updates, prints number of updates available
#[group(required = true, multiple = false)]
struct RemoteArgs {
/// Ask daemon to chack for updates
#[arg(long)]
check_updates: bool,
/// Check for available updates, prints number of updates available
/// Ask daemon if there is any updates available
#[arg(long)]
updates_available: bool,
updates_avail: bool,
}
#[derive(Args, Debug)]
#[group(required = true, multiple = false)]
struct CliArgs {
/// Perform system upgrade
#[arg(long)]
upgrade: bool,
/// Run hive-up in daemon mode
/// Install new kernel into boot partition
#[arg(long)]
daemon: bool,
update_boot: bool,
}
#[derive(Subcommand, Debug)]
#[group(required = true)] // 'multiple = false' enforces exclusivity
enum HiveUpMode {
/// Perform local operations
Cli(CliArgs),
/// Ask hive-up daemon something
Remote(RemoteArgs),
/// Run hive-up in daemon mode
Daemon,
}
/// Update program fot Kira Linux :3
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
#[command(author, version, about)]
struct Cli {
/// hive-up operation mode
#[command(flatten)]
#[command(subcommand)]
mode: HiveUpMode,
}
fn cli_upgrade(bzz_config: HiveConfig) -> Result<(), HiveUpError> {
let mut bzz_db = hive_db::read_db()?;
if bzz_db.update_on_reboot || bzz_config.update.auto_update {
println!("Starting full system upgrade");
match hive_system::sys_upgrade() {
Ok(snap_time) => {
bzz_db.update_status = true;
bzz_db.update_on_reboot = false;
bzz_db.last_snapshot_time = snap_time;
hive_db::write_db(&bzz_db)?;
}
Err(SysUpgradeError::PkgUpgradeErr((snap_time, ex))) => {
bzz_db.update_status = false;
bzz_db.update_on_reboot = false;
bzz_db.last_snapshot_time = snap_time;
hive_db::write_db(&bzz_db)?;
return Err(ex.into());
}
Err(ex) => return Err(ex.to_string().into()),
}
}
Ok(())
}
fn main() -> Result<(), HiveUpError> {
// registering signals handling
let term_flag: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
@@ -153,36 +192,25 @@ fn main() -> Result<(), HiveUpError> {
// read config
let bzz_config = hive_config::read_config()?;
// Check CMD flags
if args.mode.check_updates {
let up_num = hive_intercom::client_check_for_updates()?;
println!("Updates awailable: {}", up_num);
} else if args.mode.updates_available {
let up_num = hive_intercom::client_updates_available()?;
println!("Updates awailable: {}", up_num);
} else if args.mode.upgrade {
let mut bzz_db = hive_db::read_db()?;
if bzz_db.update_on_reboot {
println!("Starting full system upgrade");
match hive_system::sys_upgrade() {
Ok(snap_time) => {
bzz_db.update_status = true;
bzz_db.update_on_reboot = false;
bzz_db.last_snapshot_time = snap_time;
hive_db::write_db(&bzz_db)?;
}
Err(SysUpgradeError::PkgUpgradeErr((snap_time, ex))) => {
bzz_db.update_status = false;
bzz_db.update_on_reboot = false;
bzz_db.last_snapshot_time = snap_time;
hive_db::write_db(&bzz_db)?;
return Err(ex.into());
}
Err(ex) => return Err(ex.to_string().into()),
match args.mode {
HiveUpMode::Daemon => daemon::run(bzz_config, term_flag.clone(), reload_flag.clone())?,
HiveUpMode::Cli(cli_args) => {
if cli_args.upgrade {
cli_upgrade(bzz_config)?;
} else if cli_args.update_boot {
}
}
HiveUpMode::Remote(remote_args) => {
if remote_args.check_updates {
let up_num = hive_intercom::client_check_for_updates()?;
println!("Updates awailable: {}", up_num);
} else if remote_args.updates_avail {
let up_num = hive_intercom::client_updates_available()?;
println!("Updates awailable: {}", up_num);
}
}
} else if args.mode.daemon {
daemon::run(bzz_config, term_flag.clone(), reload_flag.clone())?;
}
Ok(())
}