Merge changes from topic "aconfig-part-3"

* changes:
  aconfig: simplify argument parsing in main
  aconfig: improve flag value tracing
This commit is contained in:
Treehugger Robot
2023-05-08 18:06:52 +00:00
committed by Gerrit Code Review
3 changed files with 28 additions and 21 deletions

View File

@@ -21,13 +21,20 @@ use std::io::{Read, Write};
use crate::aconfig::{Flag, FlagState, Override, Permission};
use crate::commands::Source;
#[derive(Serialize, Deserialize, Debug)]
pub struct TracePoint {
pub source: Source,
pub state: FlagState,
pub permission: Permission,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Item {
pub id: String,
pub description: String,
pub state: FlagState,
pub permission: Permission,
pub debug: Vec<String>,
pub trace: Vec<TracePoint>,
}
#[derive(Serialize, Deserialize, Debug)]
@@ -63,7 +70,7 @@ impl Cache {
description: flag.description,
state,
permission,
debug: vec![format!("{}:{:?} {:?}", source, state, permission)],
trace: vec![TracePoint { source, state, permission }],
});
Ok(())
}
@@ -74,9 +81,11 @@ impl Cache {
};
existing_item.state = override_.state;
existing_item.permission = override_.permission;
existing_item
.debug
.push(format!("{}:{:?} {:?}", source, override_.state, override_.permission));
existing_item.trace.push(TracePoint {
source,
state: override_.state,
permission: override_.permission,
});
Ok(())
}

View File

@@ -16,13 +16,14 @@
use anyhow::{Context, Result};
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::io::Read;
use crate::aconfig::{Flag, Override};
use crate::cache::Cache;
#[derive(Clone)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Source {
#[allow(dead_code)] // only used in unit tests
Memory,

View File

@@ -17,7 +17,7 @@
//! `aconfig` is a build time tool to manage build time configurations, such as feature flags.
use anyhow::Result;
use clap::{builder::ArgAction, builder::EnumValueParser, Arg, Command};
use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command};
use std::fs;
mod aconfig;
@@ -53,25 +53,22 @@ fn cli() -> Command {
)
}
fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
let mut opened_files = vec![];
for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
let file = Box::new(fs::File::open(path)?);
opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
}
Ok(opened_files)
}
fn main() -> Result<()> {
let matches = cli().get_matches();
match matches.subcommand() {
Some(("create-cache", sub_matches)) => {
let mut aconfigs = vec![];
let build_id = *sub_matches.get_one::<u32>("build-id").unwrap();
for path in
sub_matches.get_many::<String>("aconfig").unwrap_or_default().collect::<Vec<_>>()
{
let file = Box::new(fs::File::open(path)?);
aconfigs.push(Input { source: Source::File(path.to_string()), reader: file });
}
let mut overrides = vec![];
for path in
sub_matches.get_many::<String>("override").unwrap_or_default().collect::<Vec<_>>()
{
let file = Box::new(fs::File::open(path)?);
overrides.push(Input { source: Source::File(path.to_string()), reader: file });
}
let aconfigs = open_zero_or_more_files(sub_matches, "aconfig")?;
let overrides = open_zero_or_more_files(sub_matches, "override")?;
let cache = commands::create_cache(build_id, aconfigs, overrides)?;
let path = sub_matches.get_one::<String>("cache").unwrap();
let file = fs::File::create(path)?;