Skip to content

Commit

Permalink
solo2 completion to generate scripts at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Mar 23, 2022
1 parent d93865c commit 19034e5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/build-release-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,36 @@ jobs:
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v2
with:
name: 70-solo2.rules
path: 70-solo2.rules

- name: Bash completions
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v2
with:
name: solo2.completions.bash
path: target/release/solo2.bash
path: target/x86_64-unknown-linux-gnu/release/solo2.bash

- name: Fish completions
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v2
with:
name: solo2.completions.fish
path: target/release/solo2.fish
path: target/x86_64-unknown-linux-gnu/release/solo2.fish

- name: Powershell completions
- name: PowerShell completions
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v2
with:
name: solo2.completions.powershell
path: target/release/_solo2.ps1
path: target/x86_64-unknown-linux-gnu/release/_solo2.ps1

- name: Zsh completions
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v2
with:
name: solo2.completions.zsh
path: target/release/_solo2
path: target/x86_64-unknown-linux-gnu/release/_solo2

###
# Below this line, steps will only be ran if a tag was pushed.
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ureq = { version = "2.1.1", features = ["json"] }
# cli
atty = { version = "0.2.14", optional = true }
clap = { version = "3", features = ["cargo", "derive"], optional = true }
clap_complete = { version = "3", optional = true }
ctrlc = { version = "3.2.0", optional = true }
lazy_static = { version = "1.4.0", optional = true }
pretty_env_logger = { version = "0.4.0", optional = true }
Expand All @@ -64,7 +65,7 @@ lazy_static = "1.4.0"

[features]
default = ["cli"]
cli = ["atty", "clap", "ctrlc", "lazy_static", "pretty_env_logger"]
cli = ["atty", "clap", "clap_complete", "ctrlc", "lazy_static", "pretty_env_logger"]
dev-pki = ["p256", "pkcs8", "rand_core", "rcgen", "yasna"]
# It's not allowed to use the network when building for docs.rs, and the same
# for most corporate networks. The tests behind this flag do things like downloading
Expand Down
18 changes: 17 additions & 1 deletion src/bin/solo2/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::{self, crate_authors, crate_version, AppSettings, ArgEnum, Args, Parse
///
/// Print more logs by setting env SOLO2_LOG='info' or SOLO2_LOG='debug'.
///
/// Project homepage: https://github.com/solokeys/solo2-cli
/// Project homepage: <https://github.com/solokeys/solo2-cli>.
#[derive(Parser)]
#[clap(setting(AppSettings::InferSubcommands))]
#[clap(author = crate_authors!())]
Expand Down Expand Up @@ -36,6 +36,9 @@ pub enum Subcommands {
#[clap(subcommand)]
Bootloader(Bootloader),

#[clap(subcommand)]
Completion(Completion),

/// List all available devices
#[clap(visible_alias = "ls")]
List,
Expand Down Expand Up @@ -71,6 +74,19 @@ pub enum Bootloader {
// },
}

#[derive(Subcommand)]
/// Generate shell completion scripts
pub enum Completion {
/// Print completion script for Bash
Bash,
/// Print completion script for Fish
Fish,
/// Print completion script for PowerShell
PowerShell,
/// Print completion script for Zsh
Zsh,
}

#[derive(Subcommand)]
/// PKI-related
pub enum Pki {
Expand Down
13 changes: 12 additions & 1 deletion src/bin/solo2/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ fn main() {
pretty_env_logger::init_custom_env("SOLO2_LOG");
restore_cursor_on_ctrl_c();

// let args = cli::cli().get_matches();
use clap::Parser;
let args = cli::Cli::parse();

Expand Down Expand Up @@ -315,6 +314,18 @@ fn try_main(args: cli::Cli) -> anyhow::Result<()> {
}
}
},
cli::Subcommands::Completion(args) => {
use clap_complete::{generate, shells::*};
use clap::IntoApp as _;
use std::io::stdout;
let mut app = cli::Cli::into_app();
match args {
cli::Completion::Bash => generate(Bash, &mut app, "solo2", &mut stdout()),
cli::Completion::Fish => generate(Fish, &mut app, "solo2", &mut stdout()),
cli::Completion::PowerShell => generate(PowerShell, &mut app, "solo2", &mut stdout()),
cli::Completion::Zsh => generate(Zsh, &mut app, "solo2", &mut stdout()),
}
}
cli::Subcommands::List => {
let devices = solo2::Device::list();
for device in devices {
Expand Down

0 comments on commit 19034e5

Please sign in to comment.