feat: check missing deps command line option; handle non activating opts is a proper method and all opts are part of the struct
Some checks are pending
/ cargo-test (push) Waiting to run
/ cargo-fmtcheck (push) Waiting to run
/ cargo-clippy (push) Waiting to run
/ appimage (push) Waiting to run

This commit is contained in:
Gabriele Musco 2024-11-27 21:41:13 +01:00
parent afbebfc2ec
commit e514e55008
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
2 changed files with 43 additions and 13 deletions

View file

@ -110,12 +110,13 @@ fn main() -> Result<()> {
CmdLineOpts::init(&main_app); CmdLineOpts::init(&main_app);
let sender = BROKER.sender(); let sender = BROKER.sender();
main_app.connect_command_line(move |this, cmdline| { main_app.connect_command_line(move |this, cmdline| {
if CmdLineOpts::handle_non_activating_opts(cmdline) { let opts = CmdLineOpts::from_cmdline(cmdline);
if let Some(exit_code) = opts.handle_non_activating_opts() {
this.quit(); this.quit();
return 0; return exit_code;
} }
this.activate(); this.activate();
sender.emit(Msg::HandleCommandLine(CmdLineOpts::from_cmdline(cmdline))); sender.emit(Msg::HandleCommandLine(opts));
0 0
}); });
let app = RelmApp::from_app(main_app.clone()).with_broker(&BROKER); let app = RelmApp::from_app(main_app.clone()).with_broker(&BROKER);

View file

@ -10,8 +10,10 @@ use gtk::{
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CmdLineOpts { pub struct CmdLineOpts {
pub start: bool, pub start: bool,
pub list_profiles: bool,
pub profile_uuid: Option<String>, pub profile_uuid: Option<String>,
pub skip_depcheck: bool, pub skip_depcheck: bool,
pub check_dependencies_for: Option<String>,
} }
impl CmdLineOpts { impl CmdLineOpts {
@ -19,6 +21,7 @@ impl CmdLineOpts {
const OPT_LIST_PROFILES: (&'static str, char) = ("list-profiles", 'l'); const OPT_LIST_PROFILES: (&'static str, char) = ("list-profiles", 'l');
const OPT_PROFILE: (&'static str, char) = ("profile", 'p'); const OPT_PROFILE: (&'static str, char) = ("profile", 'p');
const OPT_SKIP_DEPCHECK: (&'static str, char) = ("skip-dependency-check", 'd'); const OPT_SKIP_DEPCHECK: (&'static str, char) = ("skip-dependency-check", 'd');
const OPT_CHECK_DEPS_FOR: (&'static str, char) = ("check-deps-for", 'c');
pub fn init(app: &impl IsA<Application>) { pub fn init(app: &impl IsA<Application>) {
app.add_main_option( app.add_main_option(
@ -53,31 +56,57 @@ impl CmdLineOpts {
"Skip dependency checks when building profiles", "Skip dependency checks when building profiles",
None, None,
); );
app.add_main_option(
Self::OPT_CHECK_DEPS_FOR.0,
glib::Char::try_from(Self::OPT_CHECK_DEPS_FOR.1).unwrap(),
glib::OptionFlags::IN_MAIN,
glib::OptionArg::String,
"Prints missing dependencies for given profile id; returns nothing if all dependencies are satisfied",
None,
);
} }
/// returns true if the application should quit /// returns an exit code if the application should quit immediately
pub fn handle_non_activating_opts(cmdline: &ApplicationCommandLine) -> bool { pub fn handle_non_activating_opts(&self) -> Option<i32> {
if cmdline.options_dict().contains(Self::OPT_LIST_PROFILES.0) { if self.list_profiles {
println!("Available profiles\nUUID: \"name\""); println!("Available profiles\nUUID: \"name\"");
let profiles = Config::get_config().profiles(); let profiles = Config::get_config().profiles();
profiles.iter().for_each(|p| { profiles.iter().for_each(|p| {
println!("{}: \"{}\"", p.uuid, p.name); println!("{}: \"{}\"", p.uuid, p.name);
}); });
return true; return Some(0);
} }
false if let Some(prof_id) = self.check_dependencies_for.as_ref() {
let profiles = Config::get_config().profiles();
if let Some(prof) = profiles.iter().find(|p| &p.uuid == prof_id) {
let deps = prof.missing_dependencies();
if deps.is_empty() {
return Some(0);
}
for dep in deps {
println!("{}", dep.name);
}
return Some(1);
} else {
eprintln!("No profile found for uuid: `{prof_id}`");
return Some(404);
}
}
None
} }
pub fn from_cmdline(cmdline: &ApplicationCommandLine) -> Self { pub fn from_cmdline(cmdline: &ApplicationCommandLine) -> Self {
let opts = cmdline.options_dict(); let opts = cmdline.options_dict();
Self { Self {
start: opts.contains(Self::OPT_START.0), start: opts.contains(Self::OPT_START.0),
profile_uuid: match opts.lookup::<String>(Self::OPT_PROFILE.0) { list_profiles: opts.contains(Self::OPT_LIST_PROFILES.0),
Err(_) => None, profile_uuid: opts
Ok(None) => None, .lookup::<String>(Self::OPT_PROFILE.0)
Ok(Some(variant)) => Some(variant), .unwrap_or_default(),
},
skip_depcheck: opts.contains(Self::OPT_SKIP_DEPCHECK.0), skip_depcheck: opts.contains(Self::OPT_SKIP_DEPCHECK.0),
check_dependencies_for: opts
.lookup::<String>(Self::OPT_CHECK_DEPS_FOR.0)
.unwrap_or_default(),
} }
} }
} }