Skip to content
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

feat: add shell completions #87

Merged
merged 8 commits into from
Jul 23, 2024
52 changes: 40 additions & 12 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ 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"
async-trait = "0.1.73"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency is no longer used.


[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
29 changes: 15 additions & 14 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
use std::str::FromStr;

use anyhow::Result;
use clap::{Parser, Subcommand};
use colored::Colorize;
use nix_rs::{
command::NixCmd,
config::NixConfig,
flake::{system::System, url::FlakeUrl},
};

use crate::{
config,
github::pull_request::{PullRequest, PullRequestRef},
Expand All @@ -17,6 +8,14 @@ use crate::{
system_list::{SystemsList, SystemsListFlakeRef},
},
};
use anyhow::Result;
use clap::{Parser, Subcommand};
use colored::Colorize;
use nix_rs::{
command::NixCmd,
config::NixConfig,
flake::{system::System, url::FlakeUrl},
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo this re-arrangement of imports as it is not necessary for this PR.


/// A reference to some flake living somewhere
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -108,15 +107,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();
srid marked this conversation as resolved.
Show resolved Hide resolved
generate(shell, &mut cli, name, &mut io::stdout());
Ok(vec![])
}
}
}

Expand Down