diff --git a/tools/aconfig/src/commands.rs b/tools/aconfig/src/commands.rs index f7a641776c..a35ad08730 100644 --- a/tools/aconfig/src/commands.rs +++ b/tools/aconfig/src/commands.rs @@ -30,7 +30,8 @@ use crate::protos::{ ParsedFlagExt, ProtoFlagMetadata, ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag, ProtoParsedFlags, ProtoTracepoint, }; -use crate::storage::generate_storage_files; +use crate::storage::generate_storage_file; +use crate::storage::StorageFileSelection; pub struct Input { pub source: String, @@ -223,7 +224,7 @@ pub fn create_rust_lib(mut input: Input, codegen_mode: CodegenMode) -> Result, container: &str) -> Result> { +pub fn create_storage(caches: Vec, container: &str, file: &StorageFileSelection) -> Result> { let parsed_flags_vec: Vec = caches .into_iter() .map(|mut input| input.try_parse_flags()) @@ -231,7 +232,7 @@ pub fn create_storage(caches: Vec, container: &str) -> Result Result> { diff --git a/tools/aconfig/src/main.rs b/tools/aconfig/src/main.rs index 7d719f0c4d..120e98caa7 100644 --- a/tools/aconfig/src/main.rs +++ b/tools/aconfig/src/main.rs @@ -32,6 +32,7 @@ mod storage; use codegen::CodegenMode; use dump::DumpFormat; +use storage::StorageFileSelection; #[cfg(test)] mod test; @@ -135,6 +136,11 @@ fn cli() -> Command { .required(true) .help("The target container for the generated storage file."), ) + .arg( + Arg::new("file") + .long("file") + .value_parser(|s: &str| StorageFileSelection::try_from(s)), + ) .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) .arg(Arg::new("out").long("out").required(true)), ) @@ -278,14 +284,14 @@ fn main() -> Result<()> { write_output_to_file_or_stdout(path, &output)?; } Some(("create-storage", sub_matches)) => { + let file = get_required_arg::(sub_matches, "file") + .context("Invalid storage file selection")?; let cache = open_zero_or_more_files(sub_matches, "cache")?; let container = get_required_arg::(sub_matches, "container")?; - let dir = PathBuf::from(get_required_arg::(sub_matches, "out")?); - let generated_files = commands::create_storage(cache, container) + let path = get_required_arg::(sub_matches, "out")?; + let output = commands::create_storage(cache, container, file) .context("failed to create storage files")?; - generated_files - .iter() - .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?; + write_output_to_file_or_stdout(path, &output)?; } _ => unreachable!(), } diff --git a/tools/aconfig/src/storage/mod.rs b/tools/aconfig/src/storage/mod.rs index 36ea3094d3..b4a8b5e5aa 100644 --- a/tools/aconfig/src/storage/mod.rs +++ b/tools/aconfig/src/storage/mod.rs @@ -21,14 +21,32 @@ pub mod package_table; use anyhow::{anyhow, Result}; use std::collections::{hash_map::DefaultHasher, HashMap, HashSet}; use std::hash::{Hash, Hasher}; -use std::path::PathBuf; -use crate::commands::OutputFile; use crate::protos::{ProtoParsedFlag, ProtoParsedFlags}; use crate::storage::{ flag_table::FlagTable, flag_value::FlagValueList, package_table::PackageTable, }; +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum StorageFileSelection { + PackageMap, + FlagMap, + FlagVal, +} + +impl TryFrom<&str> for StorageFileSelection { + type Error = anyhow::Error; + + fn try_from(value: &str) -> std::result::Result { + match value { + "package_map" => Ok(Self::PackageMap), + "flag_map" => Ok(Self::FlagMap), + "flag_val" => Ok(Self::FlagVal), + _ => Err(anyhow!("Invalid storage file to create")), + } + } +} + pub const FILE_VERSION: u32 = 1; pub const HASH_PRIMES: [u32; 29] = [ @@ -110,34 +128,30 @@ where packages } -pub fn generate_storage_files<'a, I>( +pub fn generate_storage_file<'a, I>( container: &str, parsed_flags_vec_iter: I, -) -> Result> + file: &StorageFileSelection, +) -> Result> where I: Iterator, { let packages = group_flags_by_package(parsed_flags_vec_iter); - // create and serialize package map - let package_table = PackageTable::new(container, &packages)?; - let package_table_file_path = PathBuf::from("package.map"); - let package_table_file = - OutputFile { contents: package_table.as_bytes(), path: package_table_file_path }; - - // create and serialize flag map - let flag_table = FlagTable::new(container, &packages)?; - let flag_table_file_path = PathBuf::from("flag.map"); - let flag_table_file = - OutputFile { contents: flag_table.as_bytes(), path: flag_table_file_path }; - - // create and serialize flag value - let flag_value = FlagValueList::new(container, &packages)?; - let flag_value_file_path = PathBuf::from("flag.val"); - let flag_value_file = - OutputFile { contents: flag_value.as_bytes(), path: flag_value_file_path }; - - Ok(vec![package_table_file, flag_table_file, flag_value_file]) + match file { + StorageFileSelection::PackageMap => { + let package_table = PackageTable::new(container, &packages)?; + Ok(package_table.as_bytes()) + } + StorageFileSelection::FlagMap => { + let flag_table = FlagTable::new(container, &packages)?; + Ok(flag_table.as_bytes()) + } + StorageFileSelection::FlagVal => { + let flag_value = FlagValueList::new(container, &packages)?; + Ok(flag_value.as_bytes()) + } + } } #[cfg(test)]