SHA256
Changing how we parsing CMD arguments
This commit is contained in:
+64
-36
@@ -14,16 +14,17 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// 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 signal_hook::{consts::SIGINT, iterator::Signals};
|
||||||
|
use std;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std;
|
|
||||||
|
|
||||||
mod hive_system;
|
mod hive_system;
|
||||||
|
|
||||||
mod hive_config;
|
mod hive_config;
|
||||||
|
|
||||||
|
use crate::hive_config::HiveConfig;
|
||||||
use crate::hive_system::SysUpgradeError;
|
use crate::hive_system::SysUpgradeError;
|
||||||
|
|
||||||
mod daemon;
|
mod daemon;
|
||||||
@@ -108,34 +109,72 @@ impl std::error::Error for HiveUpError {}
|
|||||||
|
|
||||||
// Define the mutually exclusive group
|
// Define the mutually exclusive group
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
#[group(required = true, multiple = false)] // 'multiple = false' enforces exclusivity
|
#[group(required = true, multiple = false)]
|
||||||
struct HiveUpMode {
|
struct RemoteArgs {
|
||||||
/// Check for updates, prints number of updates available
|
/// Ask daemon to chack for updates
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
check_updates: bool,
|
check_updates: bool,
|
||||||
|
|
||||||
/// Check for available updates, prints number of updates available
|
/// Ask daemon if there is any updates available
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
updates_available: bool,
|
updates_avail: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Args, Debug)]
|
||||||
|
#[group(required = true, multiple = false)]
|
||||||
|
struct CliArgs {
|
||||||
/// Perform system upgrade
|
/// Perform system upgrade
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
upgrade: bool,
|
upgrade: bool,
|
||||||
|
/// Install new kernel into boot partition
|
||||||
/// Run hive-up in daemon mode
|
|
||||||
#[arg(long)]
|
#[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
|
/// Update program fot Kira Linux :3
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(author, version, about)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
/// hive-up operation mode
|
/// hive-up operation mode
|
||||||
#[command(flatten)]
|
#[command(subcommand)]
|
||||||
mode: HiveUpMode,
|
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> {
|
fn main() -> Result<(), HiveUpError> {
|
||||||
// registering signals handling
|
// registering signals handling
|
||||||
let term_flag: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
|
let term_flag: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
|
||||||
@@ -153,36 +192,25 @@ fn main() -> Result<(), HiveUpError> {
|
|||||||
// read config
|
// read config
|
||||||
let bzz_config = hive_config::read_config()?;
|
let bzz_config = hive_config::read_config()?;
|
||||||
|
|
||||||
// Check CMD flags
|
match args.mode {
|
||||||
if args.mode.check_updates {
|
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()?;
|
let up_num = hive_intercom::client_check_for_updates()?;
|
||||||
println!("Updates awailable: {}", up_num);
|
println!("Updates awailable: {}", up_num);
|
||||||
} else if args.mode.updates_available {
|
} else if remote_args.updates_avail {
|
||||||
let up_num = hive_intercom::client_updates_available()?;
|
let up_num = hive_intercom::client_updates_available()?;
|
||||||
println!("Updates awailable: {}", up_num);
|
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()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if args.mode.daemon {
|
|
||||||
daemon::run(bzz_config, term_flag.clone(), reload_flag.clone())?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user