Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions twoliter/embedded/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PUBLISH_INFRA_CONFIG_PATH = "${BUILDSYS_ROOT_DIR}/Infra.toml"
# Default repo to read from PUBLISH_INFRA_CONFIG_PATH
PUBLISH_REPO = "default"

PUBLISH_LOG_LEVEL = "info"
PUBLISH_LOG_LEVEL = { script = ["if [ -n \"${TWOLITER_QUIET}\" ]; then echo warn; else echo info; fi"], condition = { env_not_set = ["PUBLISH_LOG_LEVEL"] } }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-n checks if $TWOLITER_QUIET exists. But the implementation sets "1" or "0" for it. Should we check the value here directly instead?


# This can be overridden with -e to change the path to the file containing SSM
# parameter templates. This file determines the parameter names and values
Expand Down Expand Up @@ -142,7 +142,7 @@ TESTSYS_STARTING_COMMIT = { script = ["git describe --tag ${TESTSYS_STARTING_VER
TESTSYS_TESTS_DIR = "${BUILDSYS_ROOT_DIR}/tests"
TESTSYS_TEST_CONFIG_PATH = "${BUILDSYS_ROOT_DIR}/Test.toml"

TESTSYS_LOG_LEVEL = "info"
TESTSYS_LOG_LEVEL = { script = ["if [ -n \"${TWOLITER_QUIET}\" ]; then echo warn; else echo info; fi"], condition = { env_not_set = ["TESTSYS_LOG_LEVEL"] } }

[env.development]
# Certain variables are defined here to allow us to override a component value
Expand Down
44 changes: 25 additions & 19 deletions twoliter/src/cargo_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct CargoMake {
makefile_path: Option<PathBuf>,
project_dir: Option<PathBuf>,
args: Vec<String>,
quiet: bool,
}

impl CargoMake {
Expand Down Expand Up @@ -74,6 +75,12 @@ impl CargoMake {
self
}

/// Enable quiet mode
pub(crate) fn quiet(mut self, quiet: bool) -> Self {
self.quiet = quiet;
self
}

/// Specify environment variables that should be applied for this comand
pub(crate) fn env<S1, S2>(mut self, key: S1, value: S2) -> Self
where
Expand Down Expand Up @@ -113,26 +120,25 @@ impl CargoMake {
S2: Into<String>,
I: IntoIterator<Item = S2>,
{
exec_log(
Command::new("cargo")
.arg("make")
.arg("--disable-check-for-updates")
.args(
self.makefile_path.iter().flat_map(|path| {
vec!["--makefile".to_string(), path.display().to_string()]
}),
)
.args(
self.project_dir
.iter()
.flat_map(|path| vec!["--cwd".to_string(), path.display().to_string()]),
)
.args(build_system_env_vars()?)
.args(&self.args)
.arg(task.into())
.args(args.into_iter().map(Into::into)),
let mut cmd = Command::new("cargo");
cmd.arg("make").arg("--disable-check-for-updates");

cmd.args(
self.makefile_path
.iter()
.flat_map(|path| vec!["--makefile".to_string(), path.display().to_string()]),
)
.args(
self.project_dir
.iter()
.flat_map(|path| vec!["--cwd".to_string(), path.display().to_string()]),
)
.await
.args(build_system_env_vars()?)
.args(&self.args)
.arg(task.into())
.args(args.into_iter().map(Into::into));

exec_log(&mut cmd).await
}
}

Expand Down
15 changes: 10 additions & 5 deletions twoliter/src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::build_clean::BuildClean;
use super::GlobalOpts;
use crate::cargo_make::CargoMake;
use crate::common::fs;
use crate::project::{self, Locked};
Expand All @@ -16,11 +17,11 @@ pub(crate) enum BuildCommand {
}

impl BuildCommand {
pub(crate) async fn run(self) -> Result<()> {
pub(crate) async fn run(self, global_opts: &GlobalOpts) -> Result<()> {
match self {
BuildCommand::Clean(command) => command.run().await,
BuildCommand::Kit(command) => command.run().await,
BuildCommand::Variant(command) => command.run().await,
BuildCommand::Kit(command) => command.run(global_opts).await,
BuildCommand::Variant(command) => command.run(global_opts).await,
}
}
}
Expand Down Expand Up @@ -50,7 +51,7 @@ pub(crate) struct BuildKit {
}

impl BuildKit {
pub(super) async fn run(&self) -> Result<()> {
pub(super) async fn run(&self, global_opts: &GlobalOpts) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let project = project.load_lock::<Locked>().await?;
project.fetch(self.arch.as_str()).await?;
Expand All @@ -76,9 +77,11 @@ impl BuildKit {
"BUILDSYS_UPSTREAM_SOURCE_FALLBACK",
self.upstream_source_fallback.to_string(),
)
.env("TWOLITER_QUIET", if global_opts.quiet { "1" } else { "0" })
.envs(optional_envs.into_iter())
.makefile(makefile_path)
.project_dir(project.project_dir())
.quiet(global_opts.quiet)
.exec("build-kit")
.await
}
Expand Down Expand Up @@ -113,7 +116,7 @@ pub(crate) struct BuildVariant {
}

impl BuildVariant {
pub(super) async fn run(&self) -> Result<()> {
pub(super) async fn run(&self, global_opts: &GlobalOpts) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let project = project.load_lock::<Locked>().await?;
project.fetch(self.arch.as_str()).await?;
Expand Down Expand Up @@ -150,9 +153,11 @@ impl BuildVariant {
"BUILDSYS_UPSTREAM_SOURCE_FALLBACK",
self.upstream_source_fallback.to_string(),
)
.env("TWOLITER_QUIET", if global_opts.quiet { "1" } else { "0" })
.envs(optional_envs.into_iter())
.makefile(makefile_path)
.project_dir(project.project_dir())
.quiet(global_opts.quiet)
.exec("build")
.await
}
Expand Down
5 changes: 4 additions & 1 deletion twoliter/src/cmd/make.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::GlobalOpts;
use crate::cargo_make::CargoMake;
use crate::project::{self, Locked, SDKLocked, Unlocked};
use crate::tools::install_tools;
Expand Down Expand Up @@ -52,7 +53,7 @@ pub(crate) struct Make {
}

impl Make {
pub(super) async fn run(&self) -> Result<()> {
pub(super) async fn run(&self, global_opts: &GlobalOpts) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let sdk_source = self.lock_and_fetch(&project).await?;
let toolsdir = project.project_dir().join("build/tools");
Expand All @@ -62,8 +63,10 @@ impl Make {
.env("CARGO_HOME", self.cargo_home.display().to_string())
.env("TWOLITER_TOOLS_DIR", toolsdir.display().to_string())
.env("BUILDSYS_VERSION_IMAGE", project.release_version())
.env("TWOLITER_QUIET", if global_opts.quiet { "1" } else { "0" })
.makefile(makefile_path)
.project_dir(project.project_dir())
.quiet(global_opts.quiet)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q - looking at the code, it seems that the CargoMake.quiet field is not really used? Only the env var was used.

.exec_with_args(&self.makefile_task, self.additional_args.clone())
.await
}
Expand Down
42 changes: 28 additions & 14 deletions twoliter/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ use crate::cmd::make::Make;
use crate::cmd::publish_kit::PublishCommand;
use crate::cmd::update::Update;
use anyhow::Result;
use clap::Parser;
use clap::{Args as ClapArgs, Parser};
use env_logger::Builder;
use log::LevelFilter;

const DEFAULT_LEVEL_FILTER: LevelFilter = LevelFilter::Info;

#[derive(Debug, ClapArgs)]
pub(crate) struct GlobalOpts {
/// Reduce output to essential status messages only
#[arg(short, long, global = true)]
pub(crate) quiet: bool,
}

/// A tool for building custom variants of Bottlerocket.
#[derive(Debug, Parser)]
#[clap(about, long_about = None, version)]
pub(crate) struct Args {
/// Set the logging level. One of [off|error|warn|info|debug|trace]. Defaults to warn. You can
#[command(flatten)]
pub(crate) global: GlobalOpts,

/// Set the logging level. One of [off|error|warn|info|debug|trace]. Defaults to info. You can
/// also leave this unset and use the RUST_LOG env variable. See
/// https://github.com/rust-cli/env_logger/
#[clap(long = "log-level")]
Expand Down Expand Up @@ -58,19 +68,26 @@ pub(crate) enum Subcommand {
/// Entrypoint for the `twoliter` command line program.
pub(super) async fn run(args: Args) -> Result<()> {
match args.subcommand {
Subcommand::Build(build_command) => build_command.run().await,
Subcommand::Build(build_command) => build_command.run(&args.global).await,
Subcommand::Fetch(fetch_args) => fetch_args.run().await,
Subcommand::Make(make_args) => make_args.run().await,
Subcommand::Make(make_args) => make_args.run(&args.global).await,
Subcommand::Update(update_args) => update_args.run().await,
Subcommand::Publish(publish_command) => publish_command.run().await,
Subcommand::Debug(debug_action) => debug_action.run().await,
}
}

/// use `level` if present, or else use `RUST_LOG` if present, or else use a default.
pub(super) fn init_logger(level: Option<LevelFilter>) {
/// If `quiet` is true, use Warn level unless explicitly overridden.
pub(super) fn init_logger(level: Option<LevelFilter>, quiet: bool) {
let effective_level = if quiet && level.is_none() {
LevelFilter::Warn
} else {
level.unwrap_or(DEFAULT_LEVEL_FILTER)
};

match (std::env::var(env_logger::DEFAULT_FILTER_ENV).ok(), level) {
(Some(_), None) => {
(Some(_), None) if !quiet => {
// RUST_LOG exists and level does not; use the environment variable.
Builder::from_default_env().init();
}
Expand All @@ -79,10 +96,7 @@ pub(super) fn init_logger(level: Option<LevelFilter>) {
// use provided log level or default for this crate only.
Builder::new()
.parse_default_env()
.filter(
Some(env!("CARGO_CRATE_NAME")),
level.unwrap_or(DEFAULT_LEVEL_FILTER),
)
.filter(Some(env!("CARGO_CRATE_NAME")), effective_level)
.init();
}
}
Expand Down Expand Up @@ -165,7 +179,7 @@ mod test {
upstream_source_fallback: false,
};

command.run().await.unwrap();
command.run(&GlobalOpts { quiet: false }).await.unwrap();
expect_kit(project_dir, "core-kit", arch, &["pkg-a"]).await;
}

Expand All @@ -188,7 +202,7 @@ mod test {
upstream_source_fallback: false,
};

command.run().await.unwrap();
command.run(&GlobalOpts { quiet: false }).await.unwrap();
expect_kit(project_dir, "core-kit", arch, &["pkg-a"]).await;
expect_kit(project_dir, "extra-1-kit", arch, &["pkg-b", "pkg-d"]).await;
}
Expand All @@ -212,7 +226,7 @@ mod test {
upstream_source_fallback: false,
};

command.run().await.unwrap();
command.run(&GlobalOpts { quiet: false }).await.unwrap();
expect_kit(project_dir, "core-kit", arch, &["pkg-a"]).await;
expect_kit(project_dir, "extra-2-kit", arch, &["pkg-c"]).await;
}
Expand All @@ -236,7 +250,7 @@ mod test {
upstream_source_fallback: false,
};

command.run().await.unwrap();
command.run(&GlobalOpts { quiet: false }).await.unwrap();
expect_kit(project_dir, "core-kit", arch, &["pkg-a"]).await;
expect_kit(project_dir, "extra-1-kit", arch, &["pkg-b", "pkg-d"]).await;
expect_kit(project_dir, "extra-2-kit", arch, &["pkg-c"]).await;
Expand Down
2 changes: 1 addition & 1 deletion twoliter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod tools;
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
init_logger(args.log_level);
init_logger(args.log_level, args.global.quiet);
preflight::preflight().await?;
cmd::run(args).await
}
Loading