Skip to content

Commit

Permalink
Add a new supertd audit command
Browse files Browse the repository at this point in the history
Summary: We are increasingly adding new things to the audit command line. E.g. we might want to grab ignore config so we can get that right. If we have `supertd audit` that centralises all of that we can easily change what we do in future.

Reviewed By: aniketmathur

Differential Revision: D55204654

fbshipit-source-id: 014c20b734f90cab5ca09ef3f718890c546cdb5d
  • Loading branch information
ndmitchell authored and facebook-github-bot committed Mar 22, 2024
1 parent 786a082 commit 5164efe
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 12 deletions.
15 changes: 15 additions & 0 deletions audit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "audit"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "audit"
path = "bin/main.rs"

[dependencies]
anyhow = "1.0"
clap = {version = "4.1.4", features = ["derive"]}
fbinit = { workspace = true }

td_util = {path = "../td_util"}
15 changes: 15 additions & 0 deletions audit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Audit

Run `buck2 audit` with all the special arguments encouraged for use with BTD.
Using this helper ensures that as BTD evolves your scripts will continue to work
correctly.

As an example:

```shell
supertd audit cells
supertd audit config
```

Within Meta a precompiled version of `supertd` is available at
`~/fbsource/tools/utd/supertd/supertd`.
18 changes: 18 additions & 0 deletions audit/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/

#![forbid(unsafe_code)]

use td_util::cli::parse_args;

#[fbinit::main]
pub fn main(fb: fbinit::FacebookInit) -> anyhow::Result<()> {
let _guard = td_util::init(fb);
audit::main(parse_args()?)
}
85 changes: 85 additions & 0 deletions audit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/

// We use a separate lib since doctests in a binary are ignored,
// and we'd like to use doctests.

#![forbid(unsafe_code)]

use std::process;

use clap::Parser;
use td_util::command::display_command;

/// Run `buck2 audit` with all the arguments required for BTD/Citadel.
#[derive(Parser, Debug)]
pub struct Args {
#[clap(subcommand)]
mode: AuditMode,
}

#[derive(Parser, Debug)]
enum AuditMode {
/// Run `buck2 audit cell` with the right arguments.
Cell(Common),
/// Run `buck2 audit config` with the right arguments.
Config(Common),
}

#[derive(Parser, Debug)]
struct Common {
/// The command for running Buck
#[arg(long, default_value = "buck2")]
buck: String,

#[arg(long)]
dry_run: bool,
}

/// It doesn't matter which config we run cells in, they should all be the same,
/// so avoid invaliding the daemon.
const REUSE_CONFIG: &str = "--reuse-current-config";

pub fn audit_cell_arguments() -> &'static [&'static str] {
&["audit", "cell", "--json", REUSE_CONFIG]
}

pub fn audit_config_arguments() -> &'static [&'static str] {
&[
"audit",
"config",
"--json",
"--all-cells",
"buildfile.name",
"buildfile.name_v2",
REUSE_CONFIG,
]
}

pub fn main(args: Args) -> anyhow::Result<()> {
let (common, arguments) = match args.mode {
AuditMode::Cell(common) => (common, audit_cell_arguments()),
AuditMode::Config(common) => (common, audit_config_arguments()),
};

let mut command = std::process::Command::new(common.buck);
command.args(arguments);

if common.dry_run {
println!("{}", display_command(&command));
return Ok(());
}

let status = command.status()?;
if status.success() {
Ok(())
} else {
process::exit(status.code().unwrap_or(1));
}
}
1 change: 1 addition & 0 deletions btd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ thiserror = "1.0.36"
tracing = "0.1.22"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

audit = {path = "../audit"}
td_util = {path = "../td_util"}
targets = {path = "../targets"}
16 changes: 4 additions & 12 deletions btd/src/buck/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use std::path::PathBuf;
use std::process::Command;

use anyhow::Context as _;
use audit::audit_cell_arguments;
use audit::audit_config_arguments;
use itertools::Itertools;
use targets::targets_arguments;
use td_util::command::with_command;
Expand Down Expand Up @@ -89,9 +91,7 @@ impl Buck2 {

pub fn cells(&mut self) -> anyhow::Result<String> {
let mut command = self.command();
// It doesn't matter which config we run cells in, they should all be the same,
// so avoid invaliding the daemon.
command.args(["audit", "cell", "--json", "--reuse-current-config"]);
command.args(audit_cell_arguments());
command.current_dir(self.root()?);
let res = with_command(command, |mut command| {
let res = command.output()?;
Expand All @@ -105,15 +105,7 @@ impl Buck2 {

pub fn audit_config(&mut self) -> anyhow::Result<String> {
let mut command = self.command();
command.args([
"audit",
"config",
"--json",
"--all-cells",
"buildfile.name",
"buildfile.name_v2",
"--reuse-current-config",
]);
command.args(audit_config_arguments());
command.current_dir(self.root()?);
let res = with_command(command, |mut command| {
let res = command.output()?;
Expand Down
1 change: 1 addition & 0 deletions supertd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ clap = {version = "4.1.4", features = ["derive"]}
fbinit = { workspace = true }

td_util = {path = "../td_util"}
audit = {path = "../audit"}
btd = {path = "../btd"}
targets = {path = "../targets"}
2 changes: 2 additions & 0 deletions supertd/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use td_util::cli::get_args;
#[derive(Parser)]
#[command(name = "supertd", version = get_version())]
enum Args {
Audit(audit::Args),
Btd(btd::Args),
#[cfg(fbcode_build)]
Citadel(verifiable_matcher::Args),
Expand Down Expand Up @@ -50,6 +51,7 @@ pub fn main(fb: FacebookInit) -> anyhow::Result<()> {
match Args::from_arg_matches(&matches) {
Err(err) => err.format(&mut Args::command()).exit(),
Ok(args) => match args {
Args::Audit(args) => audit::main(args),
Args::Btd(args) => btd::main(args),
#[cfg(fbcode_build)]
Args::Citadel(args) => verifiable_matcher::main(args),
Expand Down

0 comments on commit 5164efe

Please sign in to comment.