diff --git a/Cargo.lock b/Cargo.lock index f405873..18b5474 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,11 +418,14 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.6.7" +version = "4.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8b397918185f0161ff3d6fcaa9e4bfc09b8367caf6e1d4a2848e5477ed027b" +checksum = "b1f84a88507dbd05c695f2cb5e8558e747179134005e9893882dec964190ed89" dependencies = [ "clap", + "clap_lex", + "is_executable", + "shlex", ] [[package]] @@ -933,7 +936,16 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", +] + +[[package]] +name = "is_executable" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baabb8b4867b26294d818bf3f651a454b6901431711abb96e296245888d6e8c4" +dependencies = [ + "windows-sys 0.60.2", ] [[package]] @@ -1717,7 +1729,16 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "windows-targets", + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", ] [[package]] @@ -1735,13 +1756,30 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", ] [[package]] @@ -1750,42 +1788,90 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + [[package]] name = "windows_i686_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + [[package]] name = "windows_i686_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + [[package]] name = "yansi" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index 621e6c9..894e572 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,8 @@ path = "src/main.rs" [dependencies] async-trait = "0.1.91" -clap = { version = "4.6.1", features = ["derive"] } -clap_complete = "4.6.7" +clap = { version = "4.6.1", features = ["derive", "unstable-ext"] } +clap_complete = { version = "4.6.8", features = ["unstable-dynamic"] } color-eyre = "0.6.5" ctrlc = { version = "3.5.2", features = ["termination"] } dotenvy = "0.15.7" diff --git a/debug b/debug new file mode 100644 index 0000000..ba988f7 --- /dev/null +++ b/debug @@ -0,0 +1 @@ +ArgMatches { args: FlatMap { keys: [], values: [] }, subcommand: Some(SubCommand { name: "run", matches: ArgMatches { args: FlatMap { keys: ["workflow", "Run", "jobs", "all_jobs", "abort", "eval", "checkout_strategy", "nixpkgs_expr", "use_cache", "tracing"], values: [MatchedArg { source: Some(CommandLine), indices: [1], type_id: Some(TypeId(0xbdd4bacae454a0eeb45f0a86f4028494)), vals: [[AnyValue { inner: TypeId(0xbdd4bacae454a0eeb45f0a86f4028494) }]], raw_vals: [["now.nix"]], ignore_case: false }, MatchedArg { source: Some(CommandLine), indices: [], type_id: None, vals: [[AnyValue { inner: TypeId(0x100a94dcede1da3ad4486e900f33bc93) }], [AnyValue { inner: TypeId(0x100a94dcede1da3ad4486e900f33bc93) }]], raw_vals: [["workflow"], ["jobs"]], ignore_case: false }, MatchedArg { source: Some(CommandLine), indices: [3], type_id: Some(TypeId(0x6dc61d9a0d3fc02b1b1d20f99efa0dad)), vals: [[AnyValue { inner: TypeId(0x6dc61d9a0d3fc02b1b1d20f99efa0dad) }]], raw_vals: [[""]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [4], type_id: Some(TypeId(0x380942019dd8a6fe137938170a0ccf21)), vals: [[AnyValue { inner: TypeId(0x380942019dd8a6fe137938170a0ccf21) }]], raw_vals: [["false"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [5], type_id: Some(TypeId(0x380942019dd8a6fe137938170a0ccf21)), vals: [[AnyValue { inner: TypeId(0x380942019dd8a6fe137938170a0ccf21) }]], raw_vals: [["false"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [6], type_id: Some(TypeId(0x380942019dd8a6fe137938170a0ccf21)), vals: [[AnyValue { inner: TypeId(0x380942019dd8a6fe137938170a0ccf21) }]], raw_vals: [["false"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [7], type_id: Some(TypeId(0xcce5c1f7e996684f43d27485af45e80f)), vals: [[AnyValue { inner: TypeId(0xcce5c1f7e996684f43d27485af45e80f) }]], raw_vals: [["default"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [8], type_id: Some(TypeId(0x6dc61d9a0d3fc02b1b1d20f99efa0dad)), vals: [[AnyValue { inner: TypeId(0x6dc61d9a0d3fc02b1b1d20f99efa0dad) }]], raw_vals: [[""]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [9], type_id: Some(TypeId(0x380942019dd8a6fe137938170a0ccf21)), vals: [[AnyValue { inner: TypeId(0x380942019dd8a6fe137938170a0ccf21) }]], raw_vals: [["false"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [10], type_id: Some(TypeId(0x380942019dd8a6fe137938170a0ccf21)), vals: [[AnyValue { inner: TypeId(0x380942019dd8a6fe137938170a0ccf21) }]], raw_vals: [["false"]], ignore_case: false }] }, subcommand: None } }) } \ No newline at end of file diff --git a/package.nix b/package.nix index 5b4f47f..37f6873 100644 --- a/package.nix +++ b/package.nix @@ -64,9 +64,9 @@ rustPlatform.buildRustPackage { '' + lib.optionalString (stdenv.buildPlatform.canExecute stdenv.hostPlatform) '' installShellCompletion --cmd now \ - --bash <($out/bin/now completions bash) \ - --fish <($out/bin/now completions fish) \ - --zsh <($out/bin/now completions zsh) + --bash <(COMPLETE=bash $out/bin/now) \ + --fish <(COMPLETE=fish $out/bin/now) \ + --zsh <(COMPLETE=zsh $out/bin/now) ''; meta = { diff --git a/src/environment.rs b/src/environment.rs index 17832bf..f14fc3d 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -15,7 +15,7 @@ // with this program. If not, see . use std::{ - collections::{HashMap, HashSet}, + collections::{BTreeMap, BTreeSet, HashMap, HashSet}, ffi::OsString, io::Write, os::unix::ffi::OsStringExt, @@ -38,6 +38,7 @@ pub(crate) struct NowEnvironment { pub(crate) nix_project_source: ProjectSource, pub(crate) secrets: HashMap, pub(crate) vars: HashMap, + pub(crate) jobs: BTreeMap, pub(crate) local_env: HashMap, pub(crate) uploads: Mutex>, } @@ -45,6 +46,7 @@ pub(crate) struct NowEnvironment { struct ParsedWorkflow { vars: HashSet, secrets: HashSet, + jobs: BTreeMap, } impl NowEnvironment { @@ -137,6 +139,7 @@ impl NowEnvironment { nix_project_source, secrets, vars, + jobs: parsed_workflow.jobs, local_env: env_vars, uploads: Default::default(), }) @@ -237,7 +240,34 @@ impl NowEnvironment { } } - Ok(ParsedWorkflow { vars, secrets }) + let jobs = workflow + .jobs + .iter() + .map(|(key, value)| { + let value: String = match value { + NowJobContainer::Single(job) => job.name.clone(), + NowJobContainer::Multiple(job_vec) => { + let job_names: BTreeSet = + job_vec.iter().map(|job| job.name.clone()).collect(); + let mut joined_name = String::new(); + for name in job_names { + if !joined_name.is_empty() { + joined_name.push_str(", "); + } + joined_name.push_str(&name); + } + joined_name + } + }; + (key.clone(), value) + }) + .collect(); + + Ok(ParsedWorkflow { + vars, + secrets, + jobs, + }) } pub(crate) fn generate_env_vars_for_step( diff --git a/src/main.rs b/src/main.rs index 591720b..a448612 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,9 +14,15 @@ // You should have received a copy of the GNU Affero General Public License along // with this program. If not, see . -use std::{path::PathBuf, str::FromStr, time::Duration}; +use std::{ + collections::HashSet, + path::{Path, PathBuf}, + str::FromStr, + time::Duration, +}; use clap::{CommandFactory, Parser, ValueEnum}; +use clap_complete::{ArgValueCandidates, ArgValueCompleter, CompletionCandidate, PathCompleter}; use color_eyre::eyre::Context; use tracing::level_filters::LevelFilter; use tracing_duper::DuperLayer; @@ -41,8 +47,8 @@ mod workflow; pub(crate) enum CheckoutStrategy { /// Don't checkout; create a fresh directory for every job. None, - /// On the local builder, run commands at the local directory. - /// On remote builders, copy non-ignored files from the local directory via rsync. + /// Run commands at the local directory. + /// On remote builders, copy non-ignored files from the local directory. Default, } @@ -70,7 +76,7 @@ static LONG_ABOUT: &str = "now - Nix-based distributed command runner. .now/fresh-dir.nix"; #[derive(Parser)] -#[command(version, about, long_about = LONG_ABOUT)] +#[command(name = "now", version, about, long_about = LONG_ABOUT)] enum Command { /// Initialize a basic workflow. Init { @@ -81,14 +87,21 @@ enum Command { /// Run a workflow. Run { /// Path to the workflow. + #[arg(add = ArgValueCompleter::new(PathCompleter::any().filter(workflow_filter)))] workflow: PathBuf, /// Jobs to target in this run. + /// /// If unspecified, the default jobs of the workflow are run. /// If there are no default jobs in the workflow, all jobs are run. /// /// Cannot be used together with the `--all-jobs` option. - #[arg(short, long = "job", value_name = "JOB")] + #[arg( + short, + long = "job", + value_name = "JOB", + add = ArgValueCandidates::new(job_completer) + )] jobs: Option>, /// If set, all jobs are run. @@ -123,6 +136,7 @@ enum Command { checkout_strategy: CheckoutStrategy, /// A semicolon-separated list of build machines. + /// /// When specified, overrides the remote builders configuration of the host. /// /// For more information on the syntax, see: @@ -151,12 +165,6 @@ enum Command { #[arg(long)] tracing: bool, }, - - /// Generate shell completions. - Completions { - /// Which shell to generate completions for. - shell: clap_complete::Shell, - }, } fn validate_duration(value: &str) -> color_eyre::Result { @@ -166,6 +174,8 @@ fn validate_duration(value: &str) -> color_eyre::Result { } fn main() -> color_eyre::Result<()> { + clap_complete::CompleteEnv::with_factory(Command::command).complete(); + match Command::parse() { Command::Init { workflow } => { let mut path = workflow.unwrap_or(PathBuf::from(".")); @@ -274,15 +284,49 @@ fn main() -> color_eyre::Result<()> { builders, })?; } - - Command::Completions { shell } => { - clap_complete::generate( - shell, - &mut Command::command(), - env!("CARGO_BIN_NAME"), - &mut std::io::stdout(), - ); - } } Ok(()) } + +fn workflow_filter(path: &Path) -> bool { + path.is_dir() || path.extension().is_some_and(|extension| extension == "nix") +} + +fn job_completer() -> Vec { + let Ok(command_matches) = Command::command().try_get_matches_from(std::env::args_os().skip(2)) + else { + return vec![]; + }; + let Some(matches) = command_matches.subcommand_matches("run") else { + return vec![]; + }; + + let Ok(Some(workflow)) = matches.try_get_one::("workflow") else { + return vec![]; + }; + let Ok(Some(jobs_iter)) = matches.try_get_many::("jobs") else { + return vec![]; + }; + + let Ok(environment) = NowEnvironment::get(workflow, None, "", false) else { + return vec![]; + }; + + let mut jobs_iter = jobs_iter.rev(); + let current_job = jobs_iter.next(); + let jobs: HashSet<&String> = jobs_iter.collect(); + + environment + .jobs + .into_iter() + .filter_map(|(job, help)| { + if current_job.is_none_or(|current_job| job.starts_with(current_job)) + && !jobs.contains(&job) + { + Some(CompletionCandidate::new(job).help(Some(help.into()))) + } else { + None + } + }) + .collect() +}