blob: 79ac92094e55968cb90d74ea8a1691ab58e38ced [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")
36 .arg(Arg::new("aconfig").long("aconfig").action(ArgAction::Append))
37 .arg(Arg::new("override").long("override").action(ArgAction::Append))
38 .arg(Arg::new("cache").long("cache").required(true)),
39 )
40 .subcommand(
41 Command::new("dump").arg(Arg::new("cache").long("cache").required(true)).arg(
42 Arg::new("format")
43 .long("format")
44 .value_parser(EnumValueParser::<commands::Format>::new())
45 .default_value("text"),
46 ),
47 )
48}
Mårten Kongstad867a3492023-04-25 15:06:30 +020049
Mårten Kongstadbb520722023-04-26 13:16:41 +020050fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020051 let matches = cli().get_matches();
52 match matches.subcommand() {
53 Some(("create-cache", sub_matches)) => {
54 let mut aconfigs = vec![];
55 for path in
56 sub_matches.get_many::<String>("aconfig").unwrap_or_default().collect::<Vec<_>>()
57 {
58 let file = Box::new(fs::File::open(path)?);
59 aconfigs.push(Input { source: Source::File(path.to_string()), reader: file });
60 }
61 let mut overrides = vec![];
62 for path in
63 sub_matches.get_many::<String>("override").unwrap_or_default().collect::<Vec<_>>()
64 {
65 let file = Box::new(fs::File::open(path)?);
66 overrides.push(Input { source: Source::File(path.to_string()), reader: file });
67 }
68 let cache = commands::create_cache(aconfigs, overrides)?;
69 let path = sub_matches.get_one::<String>("cache").unwrap();
70 let file = fs::File::create(path)?;
71 cache.write_to_writer(file)?;
72 }
73 Some(("dump", sub_matches)) => {
74 let path = sub_matches.get_one::<String>("cache").unwrap();
75 let file = fs::File::open(path)?;
76 let cache = Cache::read_from_reader(file)?;
77 let format = sub_matches.get_one("format").unwrap();
78 commands::dump_cache(cache, *format)?;
79 }
80 _ => unreachable!(),
81 }
Mårten Kongstadbb520722023-04-26 13:16:41 +020082 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +020083}