mod add;
mod changelog;
mod ci;
mod config;
mod coverage;
mod detect_tools;
mod fs;
mod generate;
mod git;
mod github;
mod handoff;
mod inherited;
mod init;
mod install_pin;
mod intent;
mod markdown;
mod migrate;
mod migrate_config;
mod migrate_gates;
mod migrate_output;
mod migrate_source_plan;
mod owned_files;
mod preconditions;
mod release;
mod release_state;
mod render;
mod repository;
mod status;
mod tag_shape;
mod tags;
mod template;
mod upgrade;
mod verdict;
mod version;
mod write_set;
use std::ffi::OsString;
use std::fmt;
use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(name = "oakum", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Debug, Subcommand)]
enum Commands {
Add(add::AddArgs),
Check(preconditions::CheckArgs),
Generate(generate::GenerateArgs),
Init(init::InitArgs),
Migrate(migrate::MigrateArgs),
Release(release::ReleaseArgs),
Version(version::VersionArgs),
Ci(ci::CiArgs),
#[command(name = "plan-intent", hide = true)]
PlanIntent(intent::PlanIntentArgs),
#[command(name = "detect-release-tools", hide = true)]
DetectReleaseTools,
Status(status::StatusArgs),
#[command(name = "reachable-tags", hide = true)]
ReachableTags,
#[command(name = "tag-drift", hide = true)]
TagDrift,
Upgrade,
}
pub(crate) fn say_err(line: &str) {
use std::io::Write;
let _ = writeln!(std::io::stderr(), "{line}");
}
pub(crate) fn say_out(line: &str) {
use std::io::Write;
let _ = writeln!(std::io::stdout(), "{line}");
}
pub(crate) fn deliver_out(line: &str) -> std::io::Result<()> {
use std::io::Write;
writeln!(std::io::stdout(), "{line}")
}
pub(crate) fn deliver_block(text: &str) -> std::io::Result<()> {
use std::io::Write;
let mut stdout = std::io::stdout();
stdout.write_all(text.as_bytes())?;
stdout.flush()
}
pub(crate) fn ask(prompt: &str) -> std::io::Result<()> {
use std::io::Write;
let mut stderr = std::io::stderr();
stderr.write_all(prompt.as_bytes())?;
stderr.flush()
}
pub fn run() -> Result<(), CliError> {
run_with(std::env::args_os())
}
fn run_with<I, T>(args: I) -> Result<(), CliError>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let cli = Cli::parse_from(args);
match cli.command {
None => {
say_out("oakum");
Ok(())
}
Some(Commands::Add(args)) => add::run(args).map_err(CliError::from_boxed),
Some(Commands::Check(args)) => preconditions::run(&args),
Some(Commands::TagDrift) => preconditions::run_tags_only(),
Some(Commands::Generate(args)) => generate::run(&args).map_err(CliError::from_boxed),
Some(Commands::Init(args)) => init::run(&args).map_err(CliError::from_boxed),
Some(Commands::Migrate(args)) => migrate::run(&args).map_err(CliError::from_boxed),
Some(Commands::PlanIntent(args)) => intent::run(&args).map_err(CliError::from_boxed),
Some(Commands::DetectReleaseTools) => detect_tools::run(),
Some(Commands::Status(args)) => status::run(&args).map_err(CliError::from_boxed),
Some(Commands::ReachableTags) => tags::run(),
Some(Commands::Upgrade) => upgrade::run().map_err(CliError::from_boxed),
Some(Commands::Version(args)) => version::run(&args).map_err(CliError::from_boxed),
Some(Commands::Release(args)) => release::run(&args),
Some(Commands::Ci(args)) => ci::run(&args),
}
}
pub(super) use oakum::template::quoted;
#[cfg(test)]
pub(crate) fn schema_names_every_variable(surface: &str, context: impl serde::Serialize) {
let schema = oakum::config::schema();
let described = schema["properties"][surface]["description"]
.as_str()
.unwrap_or_else(|| panic!("`{surface}` has a schema description"))
.to_owned();
let rendered = serde_json::to_value(context).expect("the context serializes");
let keys = rendered
.as_object()
.unwrap_or_else(|| panic!("`{surface}` renders with a map context"));
assert!(!keys.is_empty(), "`{surface}` renders with no variables");
for key in keys.keys() {
assert!(
described.contains(&format!("`{key}`")),
"`{surface}` receives `{key}`, which its schema description does not name: {described}"
);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Outcome {
Error,
Unverified,
}
impl Outcome {
fn code(self) -> i32 {
match self {
Self::Unverified => 2,
Self::Error => 1,
}
}
fn token(self) -> &'static str {
match self {
Self::Unverified => "unverified",
Self::Error => "error",
}
}
}
#[derive(Clone, Debug)]
pub(crate) enum CliError {
Unverified { detail: String },
TagDrift { count: usize },
Uncovered { count: usize },
Forbidden { path: String },
MissingActionsToken,
MissingPullNumber,
Other(String),
}
impl CliError {
pub(crate) fn new(message: impl Into<String>) -> Self {
Self::Other(message.into())
}
pub(crate) fn unverified(detail: impl Into<String>) -> Self {
Self::Unverified {
detail: detail.into(),
}
}
pub(crate) fn recast(&self, detail: impl Into<String>) -> Self {
match self.class() {
Outcome::Unverified => Self::unverified(detail),
Outcome::Error => Self::new(detail),
}
}
pub(crate) fn undelivered(what: impl std::fmt::Display, err: &std::io::Error) -> Self {
Self::unverified(format!("{what} could not be delivered to stdout: {err}"))
}
pub(crate) fn tag_drift(count: usize) -> Self {
Self::TagDrift { count }
}
pub(crate) fn uncovered(count: usize) -> Self {
Self::Uncovered { count }
}
pub(crate) fn from_boxed(err: Box<dyn std::error::Error>) -> Self {
match err.downcast::<Self>() {
Ok(typed) => *typed,
Err(other) => Self::Other(other.to_string()),
}
}
pub(crate) fn class(&self) -> Outcome {
match self {
Self::Unverified { .. } => Outcome::Unverified,
Self::TagDrift { .. }
| Self::Uncovered { .. }
| Self::Forbidden { .. }
| Self::MissingActionsToken
| Self::MissingPullNumber
| Self::Other(_) => Outcome::Error,
}
}
pub(crate) fn exit_code(&self) -> i32 {
self.class().code()
}
pub(crate) fn outcome(&self) -> &'static str {
self.class().token()
}
pub(crate) fn detail(&self) -> String {
let message = self.to_string();
message
.strip_prefix("unverified: ")
.map(str::to_owned)
.unwrap_or(message)
}
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unverified { detail } => f.write_str(detail),
Self::TagDrift { count } => {
write!(f, "{count} package(s) bumped without a tag")
}
Self::Uncovered { count } => {
write!(f, "{count} package(s) changed with no covering intent")
}
Self::Forbidden { path } => write!(f, "GitHub {path} returned 403"),
Self::MissingActionsToken => {
write!(
f,
"`oakum ci pr-status` needs GITHUB_TOKEN to post a comment"
)
}
Self::MissingPullNumber => write!(
f,
"`oakum ci pr-status` needs a pull request number (GITHUB_EVENT_PATH or GITHUB_REF)"
),
Self::Other(message) => f.write_str(message),
}
}
}
impl std::error::Error for CliError {}
impl From<github::Error> for CliError {
fn from(err: github::Error) -> Self {
match err {
github::Error::Unverified { detail } => Self::Unverified { detail },
github::Error::Forbidden { path } => Self::Forbidden { path },
unauthorized @ github::Error::Unauthorized { .. } => {
Self::Other(unauthorized.to_string())
}
github::Error::Other(message) => Self::Other(message),
}
}
}
#[cfg(test)]
mod tests {
use super::CliError;
#[test]
fn detail_strips_a_leading_unverified_token() {
let nested = CliError::unverified("unverified: no remotes");
assert_eq!(nested.detail(), "no remotes");
let plain = CliError::new("boom");
assert_eq!(plain.detail(), "boom");
}
#[test]
fn check_outcomes_are_distinct_variants() {
let unverified = CliError::unverified("unverified: no remotes");
let drift = CliError::tag_drift(1);
let uncovered = CliError::uncovered(2);
assert!(matches!(unverified, CliError::Unverified { .. }));
assert!(matches!(drift, CliError::TagDrift { count: 1 }));
assert!(matches!(uncovered, CliError::Uncovered { count: 2 }));
assert_ne!(
std::mem::discriminant(&unverified),
std::mem::discriminant(&drift)
);
}
const EXIT_CODED: usize = 7;
fn exit_codes() -> [(CliError, i32); EXIT_CODED] {
[
(CliError::unverified("unverified: no remotes"), 2),
(CliError::tag_drift(1), 1),
(CliError::uncovered(2), 1),
(
CliError::Forbidden {
path: String::from("/comments"),
},
1,
),
(CliError::MissingActionsToken, 1),
(CliError::MissingPullNumber, 1),
(CliError::new("boom"), 1),
]
}
#[test]
fn the_exit_code_table_names_every_variant_once() {
let named: std::collections::HashSet<_> = exit_codes()
.iter()
.map(|(err, _)| std::mem::discriminant(err))
.collect();
assert_eq!(
named.len(),
EXIT_CODED,
"every row must name a different variant"
);
}
#[test]
fn only_unverified_exits_two() {
for (err, expected) in exit_codes() {
assert_eq!(err.exit_code(), expected, "{err}");
assert_eq!(
expected == 2,
matches!(err, CliError::Unverified { .. }),
"only `Unverified` exits 2: {err}"
);
}
}
#[test]
fn the_printed_token_agrees_with_the_exit_code() {
for (err, expected) in exit_codes() {
assert_eq!(
err.outcome() == "unverified",
expected == 2,
"`{}` beside exit {expected}: {err}",
err.outcome()
);
}
}
#[test]
fn the_printed_line_carries_one_outcome_token() {
let line = {
let err = CliError::unverified("unverified: no remotes");
format!("{}: {}", err.outcome(), err.detail())
};
assert_eq!(line, "unverified: no remotes");
assert_eq!(line.matches("unverified").count(), 1, "{line}");
}
#[test]
fn every_variant_states_an_exit_code() {
let source = include_str!("mod.rs");
let opener = concat!("pub(crate) ", "enum CliError {");
let body = source
.split_once(opener)
.expect("the CliError enum")
.1
.split_once("\n}\n")
.expect("the end of the CliError enum")
.0;
let declared = body
.lines()
.filter(|line| {
let trimmed = line.trim_start();
line.len() - trimmed.len() == 4
&& trimmed.starts_with(|c: char| c.is_ascii_uppercase())
})
.count();
assert_eq!(
declared,
exit_codes().len(),
"`CliError` declares {declared} variants and the exit-code table lists {}",
exit_codes().len()
);
}
#[test]
fn from_boxed_keeps_unverified() {
let boxed: Box<dyn std::error::Error> =
Box::new(CliError::unverified("unverified: no remotes"));
assert!(matches!(
CliError::from_boxed(boxed),
CliError::Unverified { .. }
));
}
#[test]
fn from_boxed_does_not_treat_io_as_unverified() {
let boxed: Box<dyn std::error::Error> = Box::new(std::io::Error::other("boom"));
assert!(matches!(CliError::from_boxed(boxed), CliError::Other(_)));
}
#[test]
fn github_unverified_maps_to_cli_unverified() {
let err = CliError::from(super::github::Error::Unverified {
detail: String::from("unverified: GitHub /graphql returned 502"),
});
assert!(matches!(err, CliError::Unverified { .. }));
}
#[test]
fn github_forbidden_maps_to_cli_forbidden() {
let err = CliError::from(super::github::Error::Forbidden {
path: String::from("/repos/oakoss/oakum/issues/4/comments"),
});
assert!(matches!(err, CliError::Forbidden { .. }));
assert_eq!(
err.to_string(),
"GitHub /repos/oakoss/oakum/issues/4/comments returned 403"
);
}
#[test]
fn missing_comment_preconditions_are_distinct_variants() {
assert!(matches!(
CliError::MissingActionsToken,
CliError::MissingActionsToken
));
assert!(matches!(
CliError::MissingPullNumber,
CliError::MissingPullNumber
));
assert_ne!(
std::mem::discriminant(&CliError::MissingActionsToken),
std::mem::discriminant(&CliError::MissingPullNumber)
);
assert_eq!(
CliError::MissingActionsToken.to_string(),
"`oakum ci pr-status` needs GITHUB_TOKEN to post a comment"
);
}
#[test]
fn tag_drift_and_uncovered_display_match_the_check_summaries() {
assert_eq!(
CliError::tag_drift(1).to_string(),
"1 package(s) bumped without a tag"
);
assert_eq!(
CliError::uncovered(2).to_string(),
"2 package(s) changed with no covering intent"
);
}
}