compos: switch to clap's derive pattern
Since clap can handle the string->enum automatically, it's nicer to
avoid the string matching in code.
Bug: 246385183
Test: atest ComposHostTestCases
Change-Id: If127392e375a425821a34dcc644aa46765a5b133
diff --git a/compos/Android.bp b/compos/Android.bp
index 6aa9d3d..ea7c4d6 100644
--- a/compos/Android.bp
+++ b/compos/Android.bp
@@ -13,7 +13,6 @@
"libandroid_logger",
"libanyhow",
"libbinder_rs",
- "libclap",
"libcompos_common",
"liblibc",
"liblog_rust",
diff --git a/compos/composd_cmd/composd_cmd.rs b/compos/composd_cmd/composd_cmd.rs
index d5feed8..b6d82aa 100644
--- a/compos/composd_cmd/composd_cmd.rs
+++ b/compos/composd_cmd/composd_cmd.rs
@@ -31,30 +31,32 @@
},
};
use anyhow::{bail, Context, Result};
+use clap::Parser;
use compos_common::timeouts::TIMEOUTS;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
+#[derive(Parser)]
+enum Actions {
+ /// Compile classpath for real. Output can be used after a reboot.
+ StagedApexCompile {},
+
+ /// Compile classpath in a debugging VM. Output is ignored.
+ TestCompile {
+ /// If any APEX is staged, prefer the staged version.
+ #[clap(long)]
+ prefer_staged: bool,
+ },
+}
+
fn main() -> Result<()> {
- #[rustfmt::skip]
- let app = clap::App::new("composd_cmd")
- .subcommand(
- clap::SubCommand::with_name("staged-apex-compile"))
- .subcommand(
- clap::SubCommand::with_name("test-compile")
- .arg(clap::Arg::with_name("prefer-staged").long("prefer-staged")),
- );
- let args = app.get_matches();
+ let action = Actions::parse();
ProcessState::start_thread_pool();
- match args.subcommand() {
- Some(("staged-apex-compile", _)) => run_staged_apex_compile()?,
- Some(("test-compile", sub_matches)) => {
- let prefer_staged = sub_matches.is_present("prefer-staged");
- run_test_compile(prefer_staged)?;
- }
- _ => panic!("Unrecognized subcommand"),
+ match action {
+ Actions::StagedApexCompile {} => run_staged_apex_compile()?,
+ Actions::TestCompile { prefer_staged } => run_test_compile(prefer_staged)?,
}
println!("All Ok!");
diff --git a/compos/verify/verify.rs b/compos/verify/verify.rs
index 2ece8f5..3abdc74 100644
--- a/compos/verify/verify.rs
+++ b/compos/verify/verify.rs
@@ -20,6 +20,7 @@
use android_logger::LogId;
use anyhow::{bail, Context, Result};
use binder::ProcessState;
+use clap::{Parser, ValueEnum};
use compos_common::compos_client::{ComposClient, VmParameters};
use compos_common::odrefresh::{
CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, PENDING_ARTIFACTS_SUBDIR,
@@ -37,6 +38,24 @@
const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024;
+#[derive(Parser)]
+struct Args {
+ /// Type of the VM instance
+ #[clap(long, value_enum)]
+ instance: Instance,
+
+ /// Starts the VM in debug mode
+ #[clap(long, action)]
+ debug: bool,
+}
+
+#[derive(ValueEnum, Clone)]
+enum Instance {
+ Current,
+ Pending,
+ Test,
+}
+
fn main() {
android_logger::init_once(
android_logger::Config::default()
@@ -57,23 +76,11 @@
}
fn try_main() -> Result<()> {
- let matches = clap::App::new("compos_verify")
- .arg(
- clap::Arg::with_name("instance")
- .long("instance")
- .takes_value(true)
- .required(true)
- .possible_values(&["current", "pending", "test"]),
- )
- .arg(clap::Arg::with_name("debug").long("debug"))
- .get_matches();
-
- let debug_mode = matches.is_present("debug");
- let (instance_dir, artifacts_dir) = match matches.value_of("instance").unwrap() {
- "current" => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
- "pending" => (CURRENT_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR),
- "test" => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
- _ => unreachable!("Unexpected instance name"),
+ let args = Args::parse();
+ let (instance_dir, artifacts_dir) = match args.instance {
+ Instance::Current => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
+ Instance::Pending => (CURRENT_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR),
+ Instance::Test => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
};
let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir);
@@ -104,7 +111,7 @@
instance_image,
&idsig,
&idsig_manifest_apk,
- &VmParameters { debug_mode, ..Default::default() },
+ &VmParameters { debug_mode: args.debug, ..Default::default() },
)?;
let service = vm_instance.connect_service()?;