-
-
Notifications
You must be signed in to change notification settings - Fork 51
feat!: consolidate cot cli commands into one #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ElijahAhianyo
wants to merge
18
commits into
master
Choose a base branch
from
elijah/cot-proxy-cmd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fc8f8f1
consolidate cot cli commands into one
ElijahAhianyo 683b2a6
remove dead code
ElijahAhianyo 8a0c6b8
- improve error messages.
ElijahAhianyo 2a4bfba
Merge branch 'master' into elijah/cot-proxy-cmd
ElijahAhianyo d73cd10
unit tests initial
ElijahAhianyo 219d898
Merge branch 'master' into elijah/cot-proxy-cmd
ElijahAhianyo 0cfd155
some minor refactor
ElijahAhianyo 9beb8ea
fix clippy
ElijahAhianyo 48353cb
fix the snapshot tests
ElijahAhianyo 00016d3
gate unix::process::CommandExt behind unix flag
ElijahAhianyo 7f85e45
fix exec error on windows
ElijahAhianyo 86cfd37
chore(pre-commit.ci): auto fixes from pre-commit hooks
pre-commit-ci[bot] 9c9b467
make serde_json a required dep
ElijahAhianyo 4e7b5d6
comment improve
ElijahAhianyo ea45e71
Merge branch 'master' into elijah/cot-proxy-cmd
ElijahAhianyo 464b3c0
Merge branch 'master' into elijah/cot-proxy-cmd
ElijahAhianyo 8001727
update lock files
ElijahAhianyo 0412722
update snapshots
ElijahAhianyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| use std::ffi::OsString; | ||
| #[cfg(unix)] | ||
| use std::os::unix::process::CommandExt; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::Context; | ||
| use clap::CommandFactory; | ||
| use cot::metadata::CommandMeta; | ||
|
|
||
| use crate::args::{ | ||
| Cli, CompletionsArgs, ManpagesArgs, MigrationListArgs, MigrationMakeArgs, MigrationNewArgs, | ||
|
|
@@ -11,6 +15,7 @@ use crate::migration_generator::{ | |
| MigrationGeneratorOptions, create_new_migration, list_migrations, make_migrations, | ||
| }; | ||
| use crate::new_project::{CotSource, new_project}; | ||
| use crate::project::ProjectBinary; | ||
|
|
||
| pub fn handle_new_project( | ||
| ProjectNewArgs { path, name, source }: ProjectNewArgs, | ||
|
|
@@ -95,6 +100,90 @@ pub fn handle_cli_completions(CompletionsArgs { shell }: CompletionsArgs) -> any | |
| Ok(()) | ||
| } | ||
|
|
||
| pub fn handle_external( | ||
| args: &[OsString], | ||
| project: Option<ProjectBinary>, | ||
| _release: bool, | ||
| ) -> anyhow::Result<()> { | ||
| let subcmd = args[0].to_string_lossy(); | ||
|
|
||
| let Some(proj) = project else { | ||
| anyhow::bail!( | ||
| "Unknown command `{subcmd}` and no project binary was found in target/.\n\ | ||
| Hint: run `cargo build` first, or `cargo build --release`." | ||
| ); | ||
| }; | ||
|
|
||
| let known = proj | ||
| .metadata | ||
| .commands | ||
| .iter() | ||
| .any(|c| c.name == subcmd.as_ref() || c.aliases.iter().any(|a| a == subcmd.as_ref())); | ||
|
|
||
| if !known { | ||
| anyhow::bail!( | ||
| "Unknown command `{subcmd}`.\n\ | ||
| Run `cot --help` to see all available commands." | ||
| ); | ||
| } | ||
|
|
||
| exec(&proj, args) | ||
| } | ||
|
|
||
| fn exec(proj: &ProjectBinary, args: &[OsString]) -> anyhow::Result<()> { | ||
| #[cfg(unix)] | ||
| { | ||
| let err = std::process::Command::new(&proj.path).args(args).exec(); | ||
| anyhow::bail!("Failed to exec {}: {err}", proj.path.display()); | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| { | ||
| let status = std::process::Command::new(&proj.path).args(args).status()?; | ||
| std::process::exit(status.code().unwrap_or(1)); | ||
| } | ||
|
Comment on lines
+134
to
+144
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where's the difference between Unix-like and non-Unix-like platforms coming from? Why do we need separate code paths here? |
||
| } | ||
|
|
||
| /// Build a fresh [`clap::Command`] and inject the project's subcommands into | ||
| /// it before printing. | ||
| pub fn handle_combined_help(project: Option<&ProjectBinary>) -> anyhow::Result<()> { | ||
| let mut cmd = combined_help_command(project); | ||
|
|
||
| cmd.print_long_help()?; | ||
| println!(); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn combined_help_command(project: Option<&ProjectBinary>) -> clap::Command { | ||
| let mut cmd = Cli::command(); | ||
|
|
||
| if let Some(proj) = project { | ||
| for meta_cmd in &proj.metadata.commands { | ||
| cmd = cmd.subcommand(build_clap_subcommand(meta_cmd)); | ||
| } | ||
| } | ||
|
|
||
| cmd | ||
| } | ||
|
|
||
| fn build_clap_subcommand(meta: &CommandMeta) -> clap::Command { | ||
| let mut cmd = clap::Command::new(&meta.name); | ||
|
|
||
| if let Some(about) = &meta.about { | ||
| cmd = cmd.about(about.clone()); | ||
| } | ||
|
|
||
| for alias in &meta.aliases { | ||
| cmd = cmd.visible_alias(alias.clone()); | ||
| } | ||
|
|
||
| for sub in &meta.subcommands { | ||
| cmd = cmd.subcommand(build_clap_subcommand(sub)); | ||
| } | ||
|
|
||
| cmd | ||
| } | ||
|
|
||
| fn generate_completions(shell: clap_complete::Shell, writer: &mut impl std::io::Write) { | ||
| clap_complete::generate(shell, &mut Cli::command(), "cot", writer); | ||
| } | ||
|
|
@@ -180,4 +269,98 @@ mod tests { | |
|
|
||
| assert!(!output.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn external_command_without_project_reports_build_hint() { | ||
| let result = handle_external(&[OsString::from("serve")], None, false); | ||
|
|
||
| assert!(result.is_err()); | ||
| let message = result.unwrap_err().to_string(); | ||
| assert!(message.contains("Unknown command `serve`")); | ||
| assert!(message.contains("run `cargo build` first")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn external_command_unknown_to_project_reports_unknown_command() { | ||
| let project = ProjectBinary { | ||
| path: PathBuf::from("target/debug/example"), | ||
| metadata: cot::metadata::ProjectMetadata { | ||
| binary_name: "example".to_string(), | ||
| commands: vec![CommandMeta { | ||
| name: "check".to_string(), | ||
| about: None, | ||
| aliases: vec![], | ||
| subcommands: vec![], | ||
| }], | ||
| }, | ||
| }; | ||
|
|
||
| let result = handle_external(&[OsString::from("foo")], Some(project), false); | ||
|
|
||
| assert!(result.is_err()); | ||
| let message = result.unwrap_err().to_string(); | ||
| assert!(message.contains("Unknown command `foo`")); | ||
| assert!(message.contains("cot --help")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn build_clap_subcommand_preserves_about_aliases_and_nested_subcommands() { | ||
| let meta = CommandMeta { | ||
| name: "migration".to_string(), | ||
| about: Some("Migration commands".to_string()), | ||
| aliases: vec!["database".to_string()], | ||
| subcommands: vec![CommandMeta { | ||
| name: "rollback".to_string(), | ||
| about: Some("Rollback migrations".to_string()), | ||
| aliases: vec!["rbk".to_string()], | ||
| subcommands: vec![], | ||
| }], | ||
| }; | ||
|
|
||
| let cmd = build_clap_subcommand(&meta); | ||
|
|
||
| assert_eq!(cmd.get_name(), "migration"); | ||
| assert_eq!(cmd.get_about().unwrap().to_string(), "Migration commands"); | ||
| assert!(cmd.get_all_aliases().any(|alias| alias == "database")); | ||
| let nested = cmd | ||
| .get_subcommands() | ||
| .find(|subcommand| subcommand.get_name() == "rollback") | ||
| .unwrap(); | ||
| assert_eq!( | ||
| nested.get_about().unwrap().to_string(), | ||
| "Rollback migrations" | ||
| ); | ||
| assert!(nested.get_all_aliases().any(|alias| alias == "rbk")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn combined_help_command_includes_project_commands_and_builtin_commands() { | ||
| let project = ProjectBinary { | ||
| path: PathBuf::from("target/debug/example"), | ||
| metadata: cot::metadata::ProjectMetadata { | ||
| binary_name: "example".to_string(), | ||
| commands: vec![CommandMeta { | ||
| name: "health".to_string(), | ||
| about: Some("Check the server health".to_string()), | ||
| aliases: vec![], | ||
| subcommands: vec![], | ||
| }], | ||
| }, | ||
| }; | ||
|
|
||
| let cmd = combined_help_command(Some(&project)); | ||
|
|
||
| assert!( | ||
| cmd.get_subcommands() | ||
| .any(|subcommand| subcommand.get_name() == "new") | ||
| ); | ||
| let health = cmd | ||
| .get_subcommands() | ||
| .find(|subcommand| subcommand.get_name() == "health") | ||
| .unwrap(); | ||
| assert_eq!( | ||
| health.get_about().unwrap().to_string(), | ||
| "Check the server health" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit