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);
let sender = BROKER.sender();
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();
return 0;
return exit_code;
}
this.activate();
sender.emit(Msg::HandleCommandLine(CmdLineOpts::from_cmdline(cmdline)));
sender.emit(Msg::HandleCommandLine(opts));
0
});
let app = RelmApp::from_app(main_app.clone()).with_broker(&BROKER);

View file

@ -10,8 +10,10 @@ use gtk::{
#[derive(Debug, Clone)]
pub struct CmdLineOpts {
pub start: bool,
pub list_profiles: bool,
pub profile_uuid: Option<String>,
pub skip_depcheck: bool,
pub check_dependencies_for: Option<String>,
}
impl CmdLineOpts {
@ -19,6 +21,7 @@ impl CmdLineOpts {
const OPT_LIST_PROFILES: (&'static str, char) = ("list-profiles", 'l');
const OPT_PROFILE: (&'static str, char) = ("profile", 'p');
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>) {
app.add_main_option(
@ -53,31 +56,57 @@ impl CmdLineOpts {
"Skip dependency checks when building profiles",
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
pub fn handle_non_activating_opts(cmdline: &ApplicationCommandLine) -> bool {
if cmdline.options_dict().contains(Self::OPT_LIST_PROFILES.0) {
/// returns an exit code if the application should quit immediately
pub fn handle_non_activating_opts(&self) -> Option<i32> {
if self.list_profiles {
println!("Available profiles\nUUID: \"name\"");
let profiles = Config::get_config().profiles();
profiles.iter().for_each(|p| {
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 {
let opts = cmdline.options_dict();
Self {
start: opts.contains(Self::OPT_START.0),
profile_uuid: match opts.lookup::<String>(Self::OPT_PROFILE.0) {
Err(_) => None,
Ok(None) => None,
Ok(Some(variant)) => Some(variant),
},
list_profiles: opts.contains(Self::OPT_LIST_PROFILES.0),
profile_uuid: opts
.lookup::<String>(Self::OPT_PROFILE.0)
.unwrap_or_default(),
skip_depcheck: opts.contains(Self::OPT_SKIP_DEPCHECK.0),
check_dependencies_for: opts
.lookup::<String>(Self::OPT_CHECK_DEPS_FOR.0)
.unwrap_or_default(),
}
}
}