blob: 3ce974781e4ff0c1174ccb11b8517e1ab32eaaba [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 Kongstad4d2b4b02023-04-27 16:05:58 +020020use clap::{builder::ArgAction, builder::EnumValueParser, Arg, Command};
21use 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 Kongstadbb520722023-04-26 13:16:41 +020056fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020057 let matches = cli().get_matches();
58 match matches.subcommand() {
59 Some(("create-cache", sub_matches)) => {
60 let mut aconfigs = vec![];
Mårten Kongstad09c28d12023-05-04 13:29:26 +020061 let build_id = *sub_matches.get_one::<u32>("build-id").unwrap();
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020062 for path in
63 sub_matches.get_many::<String>("aconfig").unwrap_or_default().collect::<Vec<_>>()
64 {
65 let file = Box::new(fs::File::open(path)?);
66 aconfigs.push(Input { source: Source::File(path.to_string()), reader: file });
67 }
68 let mut overrides = vec![];
69 for path in
70 sub_matches.get_many::<String>("override").unwrap_or_default().collect::<Vec<_>>()
71 {
72 let file = Box::new(fs::File::open(path)?);
73 overrides.push(Input { source: Source::File(path.to_string()), reader: file });
74 }
Mårten Kongstad09c28d12023-05-04 13:29:26 +020075 let cache = commands::create_cache(build_id, aconfigs, overrides)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020076 let path = sub_matches.get_one::<String>("cache").unwrap();
77 let file = fs::File::create(path)?;
78 cache.write_to_writer(file)?;
79 }
80 Some(("dump", sub_matches)) => {
81 let path = sub_matches.get_one::<String>("cache").unwrap();
82 let file = fs::File::open(path)?;
83 let cache = Cache::read_from_reader(file)?;
84 let format = sub_matches.get_one("format").unwrap();
85 commands::dump_cache(cache, *format)?;
86 }
87 _ => unreachable!(),
88 }
Mårten Kongstadbb520722023-04-26 13:16:41 +020089 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +020090}