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: clap_completion #1056

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bzip2 = { version = "*", features = ["static"] }
cookie = "0.18.1"
chrono = "0.4"
chrono-humanize = "0.2"
clap = { version = "4.1", default-features = false, features = [
clap = { version = "4.5", default-features = false, features = [
"std",
"color",
"help",
Expand All @@ -51,6 +51,7 @@ clap = { version = "4.1", default-features = false, features = [
"cargo",
"error-context",
] }
clap_complete = "4.5"
clokwerk = "0.4"
crossterm = "0.28.1"
derive_more = "0.99.18"
Expand Down
41 changes: 41 additions & 0 deletions src/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

use clap::Command;
use clap_complete::{generate, Shell};
use std::io;
use std::path::PathBuf;

/// Generates a completion script for the specified shell.
///
/// If `output` is `Some(PathBuf)`, the script is written to the specified file.
/// Otherwise, it's written to `stdout`.
pub fn generate_completion_script(
cmd: &mut Command,
bin_name: &str,
shell: Shell,
output: Option<PathBuf>,
) -> Result<(), io::Error> {
if let Some(file_path) = output {
let mut file = std::fs::File::create(file_path)?;
generate(shell, cmd, bin_name, &mut file);
} else {
generate(shell, cmd, bin_name, &mut io::stdout());
}
Ok(())
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod analytics;
pub mod banner;
mod catalog;
mod cli;
mod completions;
mod event;
pub mod handlers;
pub mod hottier;
Expand Down
63 changes: 42 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,49 @@ async fn main() -> anyhow::Result<()> {
.compact()
.init();

// these are empty ptrs so mem footprint should be minimal
let server: Box<dyn ParseableServer> = match CONFIG.parseable.mode {
Mode::Query => Box::new(QueryServer),
Mode::Ingest => Box::new(IngestServer),
Mode::All => Box::new(Server),
};

// load metadata from persistence
let parseable_json = server.load_metadata().await?;
let metadata = storage::resolve_parseable_metadata(&parseable_json).await?;
banner::print(&CONFIG, &metadata).await;
// initialize the rbac map
rbac::map::init(&metadata);
// keep metadata info in mem
metadata.set_global();

// load kafka server
if CONFIG.parseable.mode != Mode::Query {
tokio::task::spawn(kafka::setup_integration());
}
match CONFIG.as_ref() {
ParseableCommand::Completion { shell, output } => {
let mut cmd = parseable::create_parseable_cli_command();
let bin_name = cmd.get_name().to_owned();

parseable::completions::generate_completion_script(
&mut cmd,
&bin_name,
*shell,
output.clone(),
)?;
return Ok(());
}
ParseableCommand::LocalStore(config)
| ParseableCommand::S3Store(config)
| ParseableCommand::BlobStore(config) => {
// these are empty ptrs so mem footprint should be minimal
let server: Box<dyn ParseableServer> = match config.parseable.mode {
Mode::Query => Box::new(QueryServer),
Mode::Ingest => Box::new(IngestServer),
Mode::All => Box::new(Server),
};

// Load metadata from persistence
let parseable_json = server.load_metadata().await?;
let metadata = storage::resolve_parseable_metadata(&parseable_json).await?;
banner::print(&CONFIG, &metadata).await;

// Initialize the RBAC map
rbac::map::init(&metadata);

server.init().await?;
// Keep metadata info in memory
metadata.set_global();

// Load Kafka server if not in Query mode
if config.parseable.mode != Mode::Query {
tokio::task::spawn(kafka::setup_integration());
}

// Initialize the server
server.init().await?;
}
}

Ok(())
}
Loading
Loading