Skip to content

Commit

Permalink
Add shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Mar 27, 2024
1 parent 2d9da0a commit 1512b6b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 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 @@ -15,6 +15,7 @@ anyhow = "1.0.81"
async-trait = "0.1.79"
chrono = "0.4.35"
clap = { version = "~4.4", features = ["derive"] }
clap_complete = "~4.4"
crossbeam-channel = "0.5.12"
enum_dispatch = "0.3.8"
env_logger = "0.10.2"
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod util;

use crate::config::{Config, RepositoryMappingProducer};
use anyhow::Result;
use clap::Parser;
use clap::{CommandFactory, Parser};
use std::path::PathBuf;

/// Tool for selectively mirroring Git repositories
Expand All @@ -29,6 +29,13 @@ async fn main() -> Result<()> {

let cli = Cli::parse();

if let operation::Command::Completions { shell } = cli.command {
let mut cmd = Cli::command();
let name = cmd.get_name().to_string();
clap_complete::generate(shell, &mut cmd, name, &mut std::io::stdout());
return Ok(());
}

let config = Config::load(&cli.config)?;
log::trace!("Config = {:#?}", config);

Expand Down
10 changes: 10 additions & 0 deletions src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod stale;
use crate::config::RepositoryMapping;
use anyhow::{anyhow, Result};
use clap::Subcommand;
use clap_complete::Shell;
use crossbeam_channel::{select, unbounded};
use std::{fmt, thread};

Expand All @@ -25,6 +26,12 @@ pub(crate) enum Command {
/// Identify stale/unmanaged local mirrors
#[clap(name = "stale")]
IdentifyStale(stale::Cli),

/// Generate shell completions
Completions {
/// Shell to generate completions for
shell: Shell,
},
}

pub(crate) type CommandResult =
Expand Down Expand Up @@ -81,6 +88,9 @@ impl Command {
Command::Mirror => mirror::run(&mappings, s),
Command::GarbageCollect => garbage_collect::run(&mappings, s),
Command::IdentifyStale(args) => stale::run(&mappings, args),
Command::Completions { .. } => {
panic!("Should not reach here when the completions subcommand was used")
}
};

match result {
Expand Down

0 comments on commit 1512b6b

Please sign in to comment.