apple_clis/cli/
app_path.rs

1use crate::prelude::*;
2
3/// Arguments to specify a path to a .app directory
4#[derive(clap::Args, Debug)]
5#[group(required = true, multiple = false)]
6pub struct AppPathArgs {
7	/// Provide an absolute path to the .app file
8	#[arg(long, long = "path")]
9	app_path: Option<types::AppDirectory>,
10
11	/// Use glob to find the first .app file in the current directory or any subdirectories
12	#[arg(long)]
13	glob: bool,
14}
15
16impl AppPathArgs {
17	#[tracing::instrument(level = "trace", skip(self))]
18	pub fn resolve(self) -> color_eyre::Result<types::AppDirectory> {
19		let path = match self.app_path {
20			Some(p) => p,
21			None => match self.glob {
22				false => Err(eyre!(
23					"Clap should have enforced that either `app_path` or `glob` was set"
24				))?,
25				true => {
26					let matches = glob::glob("**/*.app")
27						.map_err(|err| eyre!("Error running glob: {}", err))?
28						.filter_map(|p| p.ok())
29						.filter_map(|p| Utf8PathBuf::try_from(p).ok())
30						.collect::<Vec<_>>();
31
32					if matches.len() > 1 {
33						warn!(
34							globbed = ?matches,
35							"More than one .app file found, using the first match",
36						);
37					}
38
39					match matches.first() {
40						Some(p) => {
41							info!(message = "Using the first matched .app file", "match" = ?p);
42							types::AppDirectory::new(p)?
43						}
44						None => Err(eyre!(
45							"No .app files found in the current directory or any subdirectories"
46						))?,
47					}
48				}
49			},
50		};
51		if !path.get().exists() {
52			Err(eyre!("Provided app path does not exist: {:?}", path))?
53		}
54		Ok(path)
55	}
56}