Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //! `aconfig` is a build time tool to manage build time configurations, such as feature flags. |
| 18 | |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 19 | use anyhow::{anyhow, bail, Context, Result}; |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 20 | use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command}; |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 21 | use core::any::Any; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 22 | use std::fs; |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 23 | use std::io; |
| 24 | use std::io::Write; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 25 | use std::path::{Path, PathBuf}; |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 26 | |
Mårten Kongstad | 00cf045 | 2023-05-26 16:48:01 +0200 | [diff] [blame] | 27 | mod codegen; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 28 | mod commands; |
Mårten Kongstad | ee58f98 | 2023-12-15 08:31:51 +0100 | [diff] [blame] | 29 | mod dump; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 30 | mod protos; |
Dennis Shen | 0d1c562 | 2023-12-01 21:04:29 +0000 | [diff] [blame] | 31 | mod storage; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 32 | |
Mårten Kongstad | ee58f98 | 2023-12-15 08:31:51 +0100 | [diff] [blame] | 33 | use dump::DumpFormat; |
| 34 | |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 35 | #[cfg(test)] |
| 36 | mod test; |
| 37 | |
Mårten Kongstad | ee58f98 | 2023-12-15 08:31:51 +0100 | [diff] [blame] | 38 | use commands::{CodegenMode, Input, OutputFile}; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 39 | |
Mårten Kongstad | 49e4d6e | 2023-12-15 10:21:57 +0100 | [diff] [blame^] | 40 | const HELP_DUMP_FILTER: &str = r#" |
| 41 | Limit which flags to output. If multiple --filter arguments are provided, the output will be |
| 42 | limited to flags that match any of the filters. |
| 43 | "#; |
| 44 | |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 45 | fn cli() -> Command { |
| 46 | Command::new("aconfig") |
| 47 | .subcommand_required(true) |
| 48 | .subcommand( |
| 49 | Command::new("create-cache") |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 50 | .arg(Arg::new("package").long("package").required(true)) |
Oriol Prieto Gasco | 7afc7e7 | 2023-11-22 13:26:02 +0000 | [diff] [blame] | 51 | // TODO(b/312769710): Make this argument required. |
| 52 | .arg(Arg::new("container").long("container")) |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 53 | .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append)) |
| 54 | .arg(Arg::new("values").long("values").action(ArgAction::Append)) |
Zhi Dou | 24a0b6a | 2023-08-10 21:39:59 +0000 | [diff] [blame] | 55 | .arg( |
| 56 | Arg::new("default-permission") |
| 57 | .long("default-permission") |
| 58 | .value_parser(protos::flag_permission::parse_from_str) |
| 59 | .default_value(protos::flag_permission::to_string( |
| 60 | &commands::DEFAULT_FLAG_PERMISSION, |
| 61 | )), |
| 62 | ) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 63 | .arg(Arg::new("cache").long("cache").required(true)), |
| 64 | ) |
| 65 | .subcommand( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 66 | Command::new("create-java-lib") |
| 67 | .arg(Arg::new("cache").long("cache").required(true)) |
Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 68 | .arg(Arg::new("out").long("out").required(true)) |
| 69 | .arg( |
| 70 | Arg::new("mode") |
| 71 | .long("mode") |
| 72 | .value_parser(EnumValueParser::<commands::CodegenMode>::new()) |
| 73 | .default_value("production"), |
| 74 | ), |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 75 | ) |
| 76 | .subcommand( |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 77 | Command::new("create-cpp-lib") |
| 78 | .arg(Arg::new("cache").long("cache").required(true)) |
Dennis Shen | 8d544f7 | 2023-06-29 00:45:42 +0000 | [diff] [blame] | 79 | .arg(Arg::new("out").long("out").required(true)) |
| 80 | .arg( |
| 81 | Arg::new("mode") |
| 82 | .long("mode") |
| 83 | .value_parser(EnumValueParser::<commands::CodegenMode>::new()) |
| 84 | .default_value("production"), |
| 85 | ), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 86 | ) |
| 87 | .subcommand( |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 88 | Command::new("create-rust-lib") |
| 89 | .arg(Arg::new("cache").long("cache").required(true)) |
Dennis Shen | 3cfbcf5 | 2023-07-17 14:57:23 +0000 | [diff] [blame] | 90 | .arg(Arg::new("out").long("out").required(true)) |
| 91 | .arg( |
| 92 | Arg::new("mode") |
| 93 | .long("mode") |
| 94 | .value_parser(EnumValueParser::<commands::CodegenMode>::new()) |
| 95 | .default_value("production"), |
| 96 | ), |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 97 | ) |
| 98 | .subcommand( |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 99 | Command::new("create-device-config-defaults") |
| 100 | .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) |
| 101 | .arg(Arg::new("out").long("out").default_value("-")), |
| 102 | ) |
| 103 | .subcommand( |
Mårten Kongstad | c31a6ff | 2023-06-02 11:54:36 +0200 | [diff] [blame] | 104 | Command::new("create-device-config-sysprops") |
| 105 | .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) |
| 106 | .arg(Arg::new("out").long("out").default_value("-")), |
| 107 | ) |
| 108 | .subcommand( |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 109 | Command::new("dump") |
Colin Cross | 6befb34 | 2023-11-28 15:55:07 -0800 | [diff] [blame] | 110 | .arg(Arg::new("cache").long("cache").action(ArgAction::Append)) |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 111 | .arg( |
| 112 | Arg::new("format") |
| 113 | .long("format") |
Mårten Kongstad | ee58f98 | 2023-12-15 08:31:51 +0100 | [diff] [blame] | 114 | .value_parser(|s: &str| DumpFormat::try_from(s)) |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 115 | .default_value("text"), |
| 116 | ) |
Mårten Kongstad | 49e4d6e | 2023-12-15 10:21:57 +0100 | [diff] [blame^] | 117 | .arg(Arg::new("filter").long("filter").action(ArgAction::Append).help(HELP_DUMP_FILTER.trim())) |
Colin Cross | 6befb34 | 2023-11-28 15:55:07 -0800 | [diff] [blame] | 118 | .arg(Arg::new("dedup").long("dedup").num_args(0).action(ArgAction::SetTrue)) |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 119 | .arg(Arg::new("out").long("out").default_value("-")), |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 120 | ) |
Dennis Shen | 0d1c562 | 2023-12-01 21:04:29 +0000 | [diff] [blame] | 121 | .subcommand( |
| 122 | Command::new("create-storage") |
| 123 | .arg( |
| 124 | Arg::new("container") |
| 125 | .long("container") |
| 126 | .required(true) |
| 127 | .help("The target container for the generated storage file."), |
| 128 | ) |
| 129 | .arg(Arg::new("cache").long("cache").required(true)) |
| 130 | .arg(Arg::new("out").long("out").required(true)), |
| 131 | ) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 132 | } |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 133 | |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 134 | fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T> |
| 135 | where |
| 136 | T: Any + Clone + Send + Sync + 'static, |
| 137 | { |
| 138 | matches |
| 139 | .get_one::<T>(arg_name) |
| 140 | .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name)) |
| 141 | } |
| 142 | |
Oriol Prieto Gasco | 7afc7e7 | 2023-11-22 13:26:02 +0000 | [diff] [blame] | 143 | fn get_optional_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Option<&'a T> |
| 144 | where |
| 145 | T: Any + Clone + Send + Sync + 'static, |
| 146 | { |
| 147 | matches.get_one::<T>(arg_name) |
| 148 | } |
| 149 | |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 150 | fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> { |
| 151 | let mut opened_files = vec![]; |
| 152 | for path in matches.get_many::<String>(arg_name).unwrap_or_default() { |
| 153 | let file = Box::new(fs::File::open(path)?); |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 154 | opened_files.push(Input { source: path.to_string(), reader: file }); |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 155 | } |
| 156 | Ok(opened_files) |
| 157 | } |
| 158 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 159 | fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> { |
| 160 | let Some(path) = matches.get_one::<String>(arg_name) else { |
| 161 | bail!("missing argument {}", arg_name); |
| 162 | }; |
| 163 | let file = Box::new(fs::File::open(path)?); |
| 164 | Ok(Input { source: path.to_string(), reader: file }) |
| 165 | } |
| 166 | |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 167 | fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> { |
Mårten Kongstad | b5133f6 | 2023-09-11 11:10:02 +0200 | [diff] [blame] | 168 | let path = root.join(&output_file.path); |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 169 | let parent = path |
| 170 | .parent() |
| 171 | .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 172 | fs::create_dir_all(parent) |
| 173 | .with_context(|| format!("failed to create directory {}", parent.display()))?; |
Mårten Kongstad | b5133f6 | 2023-09-11 11:10:02 +0200 | [diff] [blame] | 174 | let mut file = |
| 175 | fs::File::create(&path).with_context(|| format!("failed to open {}", path.display()))?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 176 | file.write_all(&output_file.contents) |
| 177 | .with_context(|| format!("failed to write to {}", path.display()))?; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 178 | Ok(()) |
| 179 | } |
| 180 | |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 181 | fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> { |
| 182 | if path == "-" { |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 183 | io::stdout().write_all(data).context("failed to write to stdout")?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 184 | } else { |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 185 | fs::File::create(path) |
| 186 | .with_context(|| format!("failed to open {}", path))? |
| 187 | .write_all(data) |
| 188 | .with_context(|| format!("failed to write to {}", path))?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 189 | } |
| 190 | Ok(()) |
| 191 | } |
| 192 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 193 | fn main() -> Result<()> { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 194 | let matches = cli().get_matches(); |
| 195 | match matches.subcommand() { |
| 196 | Some(("create-cache", sub_matches)) => { |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 197 | let package = get_required_arg::<String>(sub_matches, "package")?; |
Oriol Prieto Gasco | 7afc7e7 | 2023-11-22 13:26:02 +0000 | [diff] [blame] | 198 | let container = |
| 199 | get_optional_arg::<String>(sub_matches, "container").map(|c| c.as_str()); |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 200 | let declarations = open_zero_or_more_files(sub_matches, "declarations")?; |
| 201 | let values = open_zero_or_more_files(sub_matches, "values")?; |
Zhi Dou | 24a0b6a | 2023-08-10 21:39:59 +0000 | [diff] [blame] | 202 | let default_permission = |
| 203 | get_required_arg::<protos::ProtoFlagPermission>(sub_matches, "default-permission")?; |
Oriol Prieto Gasco | 7afc7e7 | 2023-11-22 13:26:02 +0000 | [diff] [blame] | 204 | let output = commands::parse_flags( |
| 205 | package, |
| 206 | container, |
| 207 | declarations, |
| 208 | values, |
| 209 | *default_permission, |
| 210 | ) |
| 211 | .context("failed to create cache")?; |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 212 | let path = get_required_arg::<String>(sub_matches, "cache")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 213 | write_output_to_file_or_stdout(path, &output)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 214 | } |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 215 | Some(("create-java-lib", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 216 | let cache = open_single_file(sub_matches, "cache")?; |
Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 217 | let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 218 | let generated_files = |
| 219 | commands::create_java_lib(cache, *mode).context("failed to create java lib")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 220 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 221 | generated_files |
| 222 | .iter() |
| 223 | .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 224 | } |
| 225 | Some(("create-cpp-lib", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 226 | let cache = open_single_file(sub_matches, "cache")?; |
Dennis Shen | 8d544f7 | 2023-06-29 00:45:42 +0000 | [diff] [blame] | 227 | let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 228 | let generated_files = |
| 229 | commands::create_cpp_lib(cache, *mode).context("failed to create cpp lib")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 230 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
Dennis Shen | 8d544f7 | 2023-06-29 00:45:42 +0000 | [diff] [blame] | 231 | generated_files |
| 232 | .iter() |
| 233 | .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 234 | } |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 235 | Some(("create-rust-lib", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 236 | let cache = open_single_file(sub_matches, "cache")?; |
Dennis Shen | 3cfbcf5 | 2023-07-17 14:57:23 +0000 | [diff] [blame] | 237 | let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 238 | let generated_file = |
| 239 | commands::create_rust_lib(cache, *mode).context("failed to create rust lib")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 240 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 241 | write_output_file_realtive_to_dir(&dir, &generated_file)?; |
| 242 | } |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 243 | Some(("create-device-config-defaults", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 244 | let cache = open_single_file(sub_matches, "cache")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 245 | let output = commands::create_device_config_defaults(cache) |
| 246 | .context("failed to create device config defaults")?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 247 | let path = get_required_arg::<String>(sub_matches, "out")?; |
| 248 | write_output_to_file_or_stdout(path, &output)?; |
| 249 | } |
Mårten Kongstad | c31a6ff | 2023-06-02 11:54:36 +0200 | [diff] [blame] | 250 | Some(("create-device-config-sysprops", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 251 | let cache = open_single_file(sub_matches, "cache")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 252 | let output = commands::create_device_config_sysprops(cache) |
| 253 | .context("failed to create device config sysprops")?; |
Mårten Kongstad | c31a6ff | 2023-06-02 11:54:36 +0200 | [diff] [blame] | 254 | let path = get_required_arg::<String>(sub_matches, "out")?; |
| 255 | write_output_to_file_or_stdout(path, &output)?; |
| 256 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 257 | Some(("dump", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 258 | let input = open_zero_or_more_files(sub_matches, "cache")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 259 | let format = get_required_arg::<DumpFormat>(sub_matches, "format") |
| 260 | .context("failed to dump previously parsed flags")?; |
Mårten Kongstad | 49e4d6e | 2023-12-15 10:21:57 +0100 | [diff] [blame^] | 261 | let filters = sub_matches |
| 262 | .get_many::<String>("filter") |
| 263 | .unwrap_or_default() |
| 264 | .map(String::as_ref) |
| 265 | .collect::<Vec<_>>(); |
Colin Cross | 6befb34 | 2023-11-28 15:55:07 -0800 | [diff] [blame] | 266 | let dedup = get_required_arg::<bool>(sub_matches, "dedup")?; |
Mårten Kongstad | 49e4d6e | 2023-12-15 10:21:57 +0100 | [diff] [blame^] | 267 | let output = commands::dump_parsed_flags(input, format.clone(), &filters, *dedup)?; |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 268 | let path = get_required_arg::<String>(sub_matches, "out")?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 269 | write_output_to_file_or_stdout(path, &output)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 270 | } |
Dennis Shen | 0d1c562 | 2023-12-01 21:04:29 +0000 | [diff] [blame] | 271 | Some(("create-storage", sub_matches)) => { |
| 272 | let cache = open_zero_or_more_files(sub_matches, "cache")?; |
| 273 | let container = get_required_arg::<String>(sub_matches, "container")?; |
| 274 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
| 275 | let generated_files = commands::create_storage(cache, container) |
| 276 | .context("failed to create storage files")?; |
| 277 | generated_files |
| 278 | .iter() |
| 279 | .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?; |
| 280 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 281 | _ => unreachable!(), |
| 282 | } |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 283 | Ok(()) |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 284 | } |