blob: f25373533a2e10d769523d77e77db03270ac584d [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 Kongstada1029092023-05-08 11:51:59 +020022use std::io;
23use std::io::Write;
Mårten Kongstad867a3492023-04-25 15:06:30 +020024
Mårten Kongstadbb520722023-04-26 13:16:41 +020025mod aconfig;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020026mod cache;
27mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020028mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020029
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020030use crate::cache::Cache;
31use commands::{Input, Source};
32
33fn cli() -> Command {
34 Command::new("aconfig")
35 .subcommand_required(true)
36 .subcommand(
37 Command::new("create-cache")
Mårten Kongstad09c28d12023-05-04 13:29:26 +020038 .arg(
39 Arg::new("build-id")
40 .long("build-id")
41 .value_parser(clap::value_parser!(u32))
42 .required(true),
43 )
Mårten Kongstad30950782023-05-09 13:31:29 +020044 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020045 .arg(Arg::new("aconfig").long("aconfig").action(ArgAction::Append))
46 .arg(Arg::new("override").long("override").action(ArgAction::Append))
47 .arg(Arg::new("cache").long("cache").required(true)),
48 )
49 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020050 Command::new("dump")
51 .arg(Arg::new("cache").long("cache").required(true))
52 .arg(
53 Arg::new("format")
54 .long("format")
55 .value_parser(EnumValueParser::<commands::Format>::new())
56 .default_value("text"),
57 )
58 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020059 )
60}
Mårten Kongstad867a3492023-04-25 15:06:30 +020061
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020062fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
63 let mut opened_files = vec![];
64 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
65 let file = Box::new(fs::File::open(path)?);
66 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
67 }
68 Ok(opened_files)
69}
70
Mårten Kongstadbb520722023-04-26 13:16:41 +020071fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020072 let matches = cli().get_matches();
73 match matches.subcommand() {
74 Some(("create-cache", sub_matches)) => {
Mårten Kongstad09c28d12023-05-04 13:29:26 +020075 let build_id = *sub_matches.get_one::<u32>("build-id").unwrap();
Mårten Kongstad30950782023-05-09 13:31:29 +020076 let namespace = sub_matches.get_one::<String>("namespace").unwrap();
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020077 let aconfigs = open_zero_or_more_files(sub_matches, "aconfig")?;
78 let overrides = open_zero_or_more_files(sub_matches, "override")?;
Mårten Kongstad30950782023-05-09 13:31:29 +020079 let cache = commands::create_cache(build_id, namespace, aconfigs, overrides)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020080 let path = sub_matches.get_one::<String>("cache").unwrap();
81 let file = fs::File::create(path)?;
82 cache.write_to_writer(file)?;
83 }
84 Some(("dump", sub_matches)) => {
85 let path = sub_matches.get_one::<String>("cache").unwrap();
86 let file = fs::File::open(path)?;
87 let cache = Cache::read_from_reader(file)?;
88 let format = sub_matches.get_one("format").unwrap();
Mårten Kongstada1029092023-05-08 11:51:59 +020089 let output = commands::dump_cache(cache, *format)?;
90 let path = sub_matches.get_one::<String>("out").unwrap();
91 let mut file: Box<dyn Write> = if path == "-" {
92 Box::new(io::stdout())
93 } else {
94 Box::new(fs::File::create(path)?)
95 };
96 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020097 }
98 _ => unreachable!(),
99 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200100 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200101}