diff --git a/Cargo.lock b/Cargo.lock index b1cc509d..cac3ea11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,6 +114,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c757a3b7e39161a4e56f9365141ada2a6c915a8622c408ab6bb4b5d047371031" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.55" @@ -268,6 +277,7 @@ name = "pollard-cli" version = "0.0.0" dependencies = [ "clap", + "clap_complete", "glob", "pollard-core", "pollard-session", diff --git a/crates/pollard-cli/Cargo.toml b/crates/pollard-cli/Cargo.toml index e06adf68..a7597f9d 100644 --- a/crates/pollard-cli/Cargo.toml +++ b/crates/pollard-cli/Cargo.toml @@ -6,6 +6,7 @@ license.workspace = true [dependencies] clap = { version = "4", features = ["derive", "env"] } +clap_complete = "4" glob = "0.3.3" pollard-core = { path = "../pollard-core" } pollard-session = { path = "../pollard-session" } diff --git a/crates/pollard-cli/src/main.rs b/crates/pollard-cli/src/main.rs index 4019b52f..719ca837 100644 --- a/crates/pollard-cli/src/main.rs +++ b/crates/pollard-cli/src/main.rs @@ -2,12 +2,11 @@ mod io; mod mutate; mod steps; -use clap::{Parser, Subcommand}; +use clap::{CommandFactory, Parser, Subcommand}; +use clap_complete::Shell; use io::{Action, Report}; use pollard_core::Hash; -use pollard_session::{ - PartialSession, Session, SessionSkipped, discover_config, read_config, -}; +use pollard_session::{PartialSession, Session, SessionSkipped, discover_config, read_config}; use std::path::PathBuf; #[derive(Debug, Parser)] @@ -34,6 +33,10 @@ enum Command { action: StepAction, }, DumpSession, + Completions { + #[arg(value_enum)] + shell: Shell, + }, } #[derive(Debug, Subcommand)] @@ -95,6 +98,9 @@ fn render_and_apply(actions: Vec, reports: Vec>, session if !session.exec { eprintln!("{} action(s) pending, pass --exec to apply", actions.len()); + for action in actions { + action.render(&session.style, session.no_color, 0); + } return; } @@ -124,20 +130,26 @@ fn build_session(cli: Cli) -> (Session, Command) { let merged = cli.settings.merge(config_partial); - let session = merged - .resolve(SessionSkipped { - config_path, - }) + let mut session = merged + .resolve(SessionSkipped { config_path }) .unwrap_or_else(|e| { eprintln!("{e}"); std::process::exit(1); }); + session.normalize_paths(); + (session, cli.command) } fn main() { let cli = Cli::parse(); + + if let Command::Completions { shell } = &cli.command { + clap_complete::generate(*shell, &mut Cli::command(), "pollard", &mut std::io::stdout()); + return; + } + let (session, command) = build_session(cli); let (actions, reports): (Vec, Vec>) = match &command { @@ -244,6 +256,7 @@ fn main() { let report = io::SessionReport::new(&session); (vec![], vec![Box::new(report)]) } + Command::Completions { .. } => unreachable!(), }; render_and_apply(actions, reports, &session); diff --git a/crates/pollard-session/src/lib.rs b/crates/pollard-session/src/lib.rs index e1a48f59..ccb0b415 100644 --- a/crates/pollard-session/src/lib.rs +++ b/crates/pollard-session/src/lib.rs @@ -5,7 +5,26 @@ pub use config::{discover_config, read_config, ConfigError, SEARCH_PATHS}; use pollard_core::config::{LanguageId, Ordering, Vcs}; use pollard_session_derive::Settings; use serde::Serialize; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; + +fn absolutize(path: &Path) -> PathBuf { + if path.is_absolute() { + path.to_path_buf() + } else { + std::env::current_dir() + .expect("failed to get current directory") + .join(path) + } +} + +impl Session { + pub fn normalize_paths(&mut self) { + self.working_dir = absolutize(&self.working_dir); + self.report_dir = absolutize(&self.report_dir); + self.sub_dir = absolutize(&self.sub_dir); + self.config_path = absolutize(&self.config_path); + } +} #[derive(Debug, Clone, Serialize, serde::Deserialize, clap::ValueEnum)] #[serde(rename_all = "lowercase")]