Skip to content

Commit

Permalink
feat: add shell completions (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsrohitsingh682 authored Jul 23, 2024
1 parent 1cab3ca commit 1b2caf3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
40 changes: 28 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nix_rs = { version = "0.5.0", features = ["clap"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.37"
nix_health = "0.4.1"
clap_complete = "4.4.0"

[dev-dependencies]
regex = "1.9"
Expand Down
10 changes: 9 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@
};

# Flake outputs
packages.default = self'.packages.nixci;
packages.default = self'.packages.nixci.overrideAttrs (oa: {
nativeBuildInputs = (oa.nativeBuildInputs or [ ]) ++ [ pkgs.installShellFiles pkgs.nix ];
postInstall = ''
installShellCompletion --cmd nixci \
--bash <($out/bin/nixci completion bash) \
--zsh <($out/bin/nixci completion zsh) \
--fish <($out/bin/nixci completion fish)
'';
});
overlayAttrs.nixci = self'.packages.default;

devShells.default = pkgs.mkShell {
Expand Down
12 changes: 7 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ pub enum Command {
#[arg(long, value_parser, value_delimiter = ',')]
systems: Vec<System>,
},

/// Generates shell completion scripts
Completion {
#[arg(value_enum)]
shell: clap_complete::Shell,
},
}

impl Command {
/// Get the nixci [config::Config] associated with this subcommand
pub async fn get_config(&self, cmd: &NixCmd) -> anyhow::Result<config::Config> {
let flake_ref = match self {
Command::Build(build_cfg) => &build_cfg.flake_ref,
Command::DumpGithubActionsMatrix { flake_ref, .. } => flake_ref,
};
pub async fn get_config(cmd: &NixCmd, flake_ref: &FlakeRef) -> anyhow::Result<config::Config> {
let url = flake_ref.to_flake_url().await?;
tracing::info!("{}", format!("🍏 {}", url.0).bold());
let cfg = config::Config::from_flake_url(cmd, &url).await?;
Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ pub mod github;
pub mod logging;
pub mod nix;

use anyhow::{Context, Ok};
use clap::CommandFactory;
use clap_complete::generate;
use std::collections::HashSet;
use std::io;

use anyhow::Context;
use cli::{BuildConfig, CliArgs};
use colored::Colorize;
use nix::{
Expand All @@ -21,9 +24,10 @@ use tracing::instrument;
#[instrument(name = "nixci", skip(args))]
pub async fn nixci(args: CliArgs) -> anyhow::Result<Vec<StorePath>> {
tracing::debug!("Args: {args:?}");
let cfg = args.command.get_config(&args.nixcmd).await?;

match args.command {
cli::Command::Build(build_cfg) => {
let cfg = cli::Command::get_config(&args.nixcmd, &build_cfg.flake_ref).await?;
let nix_info = NixInfo::from_nix(&args.nixcmd)
.await
.with_context(|| "Unable to gather nix info")?;
Expand All @@ -39,11 +43,20 @@ pub async fn nixci(args: CliArgs) -> anyhow::Result<Vec<StorePath>> {
)
.await
}
cli::Command::DumpGithubActionsMatrix { systems, .. } => {
cli::Command::DumpGithubActionsMatrix {
systems, flake_ref, ..
} => {
let cfg = cli::Command::get_config(&args.nixcmd, &flake_ref).await?;
let matrix = github::matrix::GitHubMatrix::from(systems, &cfg.subflakes);
println!("{}", serde_json::to_string(&matrix)?);
Ok(vec![])
}
cli::Command::Completion { shell } => {
let mut cli = CliArgs::command();
let name = cli.get_name().to_string();
generate(shell, &mut cli, name, &mut io::stdout());
Ok(vec![])
}
}
}

Expand Down

0 comments on commit 1b2caf3

Please sign in to comment.