Skip to content

Commit

Permalink
CLEO -> Implemented auto-completion.
Browse files Browse the repository at this point in the history
  • Loading branch information
vv-ab authored and mbfm committed Jun 11, 2024
1 parent 7d8bdb7 commit a2628a5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
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 @@ -56,6 +56,7 @@ cargo_toml = "0.18.0"
cfg-if = "1.0.0"
chrono = { version = "0.4.35", default-features = false }
clap = "4.4.18"
clap_complete = "4.5.2"
cli-table = "0.4"
config = { version = "0.14.0", default-features = false, features = ["toml"] }
console = "0.15.8"
Expand Down
1 change: 1 addition & 0 deletions opendut-cleo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ opendut-util = { workspace = true }


clap = { workspace = true, features = ["derive"] }
clap_complete = { workspace = true}
cli-table = { workspace = true }
config = { workspace = true }
console = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions opendut-cleo/src/commands/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::io;

use clap::Command;
use clap_complete::{generate, Generator};

pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}
1 change: 1 addition & 0 deletions opendut-cleo/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod network_interface;
pub mod executor;
pub mod decode_setup_string;
pub mod generate_setup_string;
pub mod completions;
26 changes: 21 additions & 5 deletions opendut-cleo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use std::path::PathBuf;
use std::process::ExitCode;
use std::str::FromStr;

use clap::{Parser, Subcommand, ValueEnum};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use console::Style;

use opendut_carl_api::carl::CarlClient;
use opendut_types::peer::PeerSetup;
use opendut_types::topology::DeviceName;
use opendut_util::settings::{FileFormat, load_config};
use opendut_util::settings::{FileFormat, load_config, LoadedConfig};

mod commands;
pub mod parse;
Expand Down Expand Up @@ -73,6 +74,12 @@ enum Commands {
resource: DeleteResource,
},
Config,
/// Generates shell completion
Completions {
/// Shell to generate completions for
#[arg(value_enum)]
shell: Shell
},
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -199,7 +206,7 @@ async fn execute() -> Result<()> {
.expect("Failed to load config"); // TODO: Point the user to the source of the error.


let mut carl = {
let carl = {

let host = settings.config.get_string("network.carl.host")
.expect("Configuration should contain a valid host name to connect to CARL");
Expand All @@ -220,7 +227,12 @@ async fn execute() -> Result<()> {

let args = Args::parse();

match args.command {
execute_command(args.command, carl, &settings).await?;
Ok(())
}

async fn execute_command(commands: Commands, mut carl: CarlClient, settings: &LoadedConfig) -> Result<()>{
match commands {
Commands::List { resource, output } => {
match resource {
ListResource::ClusterConfigurations(implementation) => {
Expand Down Expand Up @@ -313,6 +325,10 @@ async fn execute() -> Result<()> {
Commands::Config => {
println!("Show cleo configuration: {:?}", settings);
}
Commands::Completions { shell } => {
let mut cmd = Args::command();
commands::completions::print_completions(shell, &mut cmd);
}
}
Ok(())
}
Expand All @@ -326,4 +342,4 @@ impl FromStr for ParseableSetupString {
.map(|setup| ParseableSetupString(Box::new(setup)))
.map_err(|error| error.to_string())
}
}
}

0 comments on commit a2628a5

Please sign in to comment.