blob: 62750ae9a1b56d08272dbe6b8661aac17201ed95 [file] [log] [blame]
Mårten Kongstad867a3492023-04-25 15:06:30 +02001/*
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 Kongstadbb520722023-04-26 13:16:41 +020019use anyhow::Result;
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020020use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020021use std::fs;
Mårten Kongstad867a3492023-04-25 15:06:30 +020022
Mårten Kongstadbb520722023-04-26 13:16:41 +020023mod aconfig;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020024mod cache;
25mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020026mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020027
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020028use crate::cache::Cache;
29use commands::{Input, Source};
30
31fn cli() -> Command {
32 Command::new("aconfig")
33 .subcommand_required(true)
34 .subcommand(
35 Command::new("create-cache")
Mårten Kongstad09c28d12023-05-04 13:29:26 +020036 .arg(
37 Arg::new("build-id")
38 .long("build-id")
39 .value_parser(clap::value_parser!(u32))
40 .required(true),
41 )
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020042 .arg(Arg::new("aconfig").long("aconfig").action(ArgAction::Append))
43 .arg(Arg::new("override").long("override").action(ArgAction::Append))
44 .arg(Arg::new("cache").long("cache").required(true)),
45 )
46 .subcommand(
47 Command::new("dump").arg(Arg::new("cache").long("cache").required(true)).arg(
48 Arg::new("format")
49 .long("format")
50 .value_parser(EnumValueParser::<commands::Format>::new())
51 .default_value("text"),
52 ),
53 )
54}
Mårten Kongstad867a3492023-04-25 15:06:30 +020055
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020056fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
57 let mut opened_files = vec![];
58 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
59 let file = Box::new(fs::File::open(path)?);
60 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
61 }
62 Ok(opened_files)
63}
64
Mårten Kongstadbb520722023-04-26 13:16:41 +020065fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020066 let matches = cli().get_matches();
67 match matches.subcommand() {
68 Some(("create-cache", sub_matches)) => {
Mårten Kongstad09c28d12023-05-04 13:29:26 +020069 let build_id = *sub_matches.get_one::<u32>("build-id").unwrap();
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020070 let aconfigs = open_zero_or_more_files(sub_matches, "aconfig")?;
71 let overrides = open_zero_or_more_files(sub_matches, "override")?;
Mårten Kongstad09c28d12023-05-04 13:29:26 +020072 let cache = commands::create_cache(build_id, aconfigs, overrides)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020073 let path = sub_matches.get_one::<String>("cache").unwrap();
74 let file = fs::File::create(path)?;
75 cache.write_to_writer(file)?;
76 }
77 Some(("dump", sub_matches)) => {
78 let path = sub_matches.get_one::<String>("cache").unwrap();
79 let file = fs::File::open(path)?;
80 let cache = Cache::read_from_reader(file)?;
81 let format = sub_matches.get_one("format").unwrap();
82 commands::dump_cache(cache, *format)?;
83 }
84 _ => unreachable!(),
85 }
Mårten Kongstadbb520722023-04-26 13:16:41 +020086 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +020087}