mod add;
mod base_tree;
mod changelog;
mod check_report;
mod ci;
mod config;
mod consume;
mod coverage;
mod dependency_updates;
mod detect_tools;
mod fs;
mod generate;
mod git;
mod github;
mod handoff;
mod inherited;
mod init;
mod install_pin;
mod intent;
mod lockfiles;
mod markdown;
mod migrate;
mod migrate_config;
mod migrate_gates;
mod migrate_output;
mod migrate_source_plan;
mod owned_files;
mod preconditions;
mod printable;
mod reasons;
mod release;
mod release_state;
mod render;
mod repository;
mod retired;
mod root_files;
mod stale;
mod status;
mod tag_shape;
mod tags;
mod template;
mod update_release;
mod upgrade;
mod verdict;
mod version;
mod workflow;
mod workspace;
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,
after_help = "Typical flow: `oakum init`, `oakum workflow` saved under `.github/workflows/`, `oakum add` for each change, \
`oakum status` to see the plan, and `oakum check --strict` on pull requests. \
CI runs `oakum ci version-pr` and `oakum release`.\n\n\
Guides: https://github.com/oakoss/oakum/tree/main/docs/guide"
)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
macro_rules! merge_base {
() => {
"the merge-base with `origin/main`, else `main`, else `master`"
};
}
macro_rules! since_tags {
() => {
"each package oakum tags starts at its highest reachable tag"
};
}
pub(super) const FROM_MERGE_BASE: &str = concat!(
"Git ref to scan from (exclusive). Default: ",
merge_base!(),
"."
);
pub(super) const FROM_PLAN: &str = concat!(
"Git ref to scan from (exclusive). Default: ",
merge_base!(),
"; for intent read from conventional commits, ",
since_tags!(),
" instead."
);
pub(super) const FROM_INTENT: &str = concat!(
"Git ref to read conventional commits from (exclusive); unused with bump files. Default: ",
since_tags!(),
", and every other package at ",
merge_base!(),
"."
);
#[derive(Debug, Subcommand)]
enum Commands {
Init(init::InitArgs),
Workflow(workflow::WorkflowArgs),
Add(add::AddArgs),
Generate(generate::GenerateArgs),
Status(status::StatusArgs),
Check(preconditions::CheckArgs),
Version(version::VersionArgs),
Release(release::ReleaseArgs),
Ci(ci::CiArgs),
Migrate(migrate::MigrateArgs),
#[command(name = "plan-intent", hide = true)]
PlanIntent(intent::PlanIntentArgs),
#[command(name = "detect-release-tools", hide = true)]
DetectReleaseTools,
#[command(name = "reachable-tags", hide = true)]
ReachableTags,
#[command(name = "tag-drift", hide = true)]
TagDrift,
Upgrade,
}
pub(crate) fn say_err(line: &str) {
let _ = printable::say(std::io::stderr(), line);
}
pub(crate) fn say_out(line: &str) {
let _ = printable::say(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 deliver_report(text: &str) -> std::io::Result<()> {
deliver_block(&printable::for_terminal(text))
}
pub(crate) fn ask(prompt: &str) -> std::io::Result<()> {
printable::prompt(std::io::stderr(), prompt)
}
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 {
Commands::Add(args) => add::run(args).map_err(CliError::from_boxed),
Commands::Check(args) => preconditions::run(&args),
Commands::TagDrift => preconditions::run_tags_only(),
Commands::Generate(args) => generate::run(&args).map_err(CliError::from_boxed),
Commands::Init(args) => init::run(&args).map_err(CliError::from_boxed),
Commands::Workflow(args) => workflow::run(&args).map_err(CliError::from_boxed),
Commands::Migrate(args) => migrate::run(&args).map_err(CliError::from_boxed),
Commands::PlanIntent(args) => intent::run(&args).map_err(CliError::from_boxed),
Commands::DetectReleaseTools => detect_tools::run(),
Commands::Status(args) => status::run(&args).map_err(CliError::from_boxed),
Commands::ReachableTags => tags::run(),
Commands::Upgrade => upgrade::run().map_err(CliError::from_boxed),
Commands::Version(args) => version::run(&args).map_err(CliError::from_boxed),
Commands::Release(args) => release::run(&args),
Commands::Ci(args) => ci::run(&args),
}
}
pub(super) fn binary_version() -> Result<semver::Version, Box<dyn std::error::Error>> {
env!("CARGO_PKG_VERSION")
.parse::<semver::Version>()
.map_err(|err| {
Box::new(CliError::new(format!(
"this binary reports a non-semver version: {err}"
)))
.into()
})
}
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) struct CliError {
class: Outcome,
message: String,
unanswered: bool,
}
impl CliError {
pub(crate) fn new(message: impl Into<String>) -> Self {
Self {
class: Outcome::Error,
message: message.into(),
unanswered: false,
}
}
pub(crate) fn unverified(detail: impl Into<String>) -> Self {
Self {
class: Outcome::Unverified,
message: detail.into(),
unanswered: false,
}
}
pub(crate) fn unanswered(detail: impl Into<String>) -> Self {
Self {
unanswered: true,
..Self::unverified(detail)
}
}
pub(crate) const fn is_unanswered(&self) -> bool {
self.unanswered
}
#[cfg(test)]
pub(crate) const fn is_unverified(&self) -> bool {
matches!(self.class, Outcome::Unverified)
}
pub(crate) fn recast(&self, detail: impl Into<String>) -> Self {
Self {
message: detail.into(),
..self.clone()
}
}
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::new(format!("{count} package(s) bumped without a tag"))
}
pub(crate) fn uncovered(count: usize) -> Self {
Self::new(format!(
"{count} package(s) changed with no covering intent"
))
}
pub(crate) fn from_boxed(err: Box<dyn std::error::Error>) -> Self {
match err.downcast::<Self>() {
Ok(typed) => *typed,
Err(other) => Self::new(other.to_string()),
}
}
pub(crate) const fn class(&self) -> Outcome {
self.class
}
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 {
f.write_str(&self.message)
}
}
impl std::error::Error for CliError {}
impl From<Box<dyn std::error::Error>> for CliError {
fn from(err: Box<dyn std::error::Error>) -> Self {
Self::from_boxed(err)
}
}
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::new(format!("GitHub {path} returned 403")),
unauthorized @ github::Error::Unauthorized { .. } => {
Self::new(unauthorized.to_string())
}
github::Error::Other(message) => Self::new(message),
}
}
}
#[cfg(test)]
mod tests {
use super::{CliError, Outcome};
#[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 recast_keeps_an_unanswered_mark() {
let wrapped = CliError::unanswered("unverified: git log failed").recast("context");
assert!(wrapped.is_unanswered());
assert_eq!(wrapped.exit_code(), 2);
assert!(!CliError::unverified("looked")
.recast("context")
.is_unanswered());
}
fn exit_codes() -> [(CliError, i32); 6] {
[
(CliError::unverified("unverified: no remotes"), 2),
(CliError::unanswered("unverified: git log failed"), 2),
(CliError::tag_drift(1), 1),
(CliError::uncovered(2), 1),
(
CliError::from(super::github::Error::Forbidden {
path: String::from("/comments"),
}),
1,
),
(CliError::new("boom"), 1),
]
}
#[test]
fn each_outcome_has_one_code_and_one_token() {
assert_eq!(
(Outcome::Unverified.code(), Outcome::Unverified.token()),
(2, "unverified")
);
assert_eq!(
(Outcome::Error.code(), Outcome::Error.token()),
(1, "error")
);
}
#[test]
fn only_unverified_exits_two() {
for (err, expected) in exit_codes() {
assert_eq!(err.exit_code(), expected, "{err}");
assert_eq!(expected == 2, err.is_unverified(), "{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 from_boxed_keeps_unverified() {
let boxed: Box<dyn std::error::Error> =
Box::new(CliError::unverified("unverified: no remotes"));
assert!(CliError::from_boxed(boxed).is_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"));
let err = CliError::from_boxed(boxed);
assert!(!err.is_unverified());
assert_eq!(err.to_string(), "boom");
}
#[test]
fn github_unverified_maps_to_cli_unverified() {
let err = CliError::from(super::github::Error::Unverified {
detail: String::from("unverified: GitHub /repos/oakoss/oakum returned 502"),
});
assert!(err.is_unverified());
}
#[test]
fn github_forbidden_is_an_error_naming_the_path() {
let err = CliError::from(super::github::Error::Forbidden {
path: String::from("/repos/oakoss/oakum/issues/4/comments"),
});
assert!(!err.is_unverified());
assert_eq!(
err.to_string(),
"GitHub /repos/oakoss/oakum/issues/4/comments returned 403"
);
}
#[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"
);
}
}