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: support shell completions in tedge cli #3332

Merged
merged 2 commits into from
Jan 16, 2025
Merged
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
77 changes: 60 additions & 17 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 @@ -89,13 +89,14 @@ base64 = "0.13"
bytes = "1.4"
camino = "1.1"
cap = "0.1"
clap = { version = "4.4", features = [
clap = { version = "4.5", features = [
"cargo",
"derive",
"string",
"env",
"unstable-styles",
] }
clap_complete = { version = "4.5", features = ["unstable-dynamic"] }
csv = "1.1"
darling = "0.20"
doku = "0.21"
Expand Down
1 change: 1 addition & 0 deletions crates/common/tedge_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ anyhow = { workspace = true }
camino = { workspace = true, features = ["serde", "serde1"] }
certificate = { workspace = true, features = ["reqwest"] }
clap = { workspace = true }
clap_complete = { workspace = true }
doku = { workspace = true }
figment = { workspace = true, features = ["env", "toml"] }
humantime = { workspace = true }
Expand Down
14 changes: 14 additions & 0 deletions crates/common/tedge_config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use camino::Utf8PathBuf;
use clap::Args;
use clap::ValueHint;
use clap_complete::ArgValueCandidates;
use clap_complete::CompletionCandidate;

/// CLI arguments that should be handled by all thin-edge components.
#[derive(Args, Debug, PartialEq, Eq, Clone)]
Expand All @@ -13,6 +16,7 @@ pub struct CommonArgs {
hide_env_values = true,
hide_default_value = true,
global = true,
value_hint = ValueHint::DirPath,
)]
pub config_dir: Utf8PathBuf,

Expand All @@ -35,5 +39,15 @@ pub struct LogConfigArgs {
///
/// Overrides `--debug`
#[clap(long, global = true)]
#[clap(add(ArgValueCandidates::new(log_level_completions)))]
pub log_level: Option<tracing::Level>,
}

fn log_level_completions() -> Vec<CompletionCandidate> {
use tracing::Level as L;
let options = [L::TRACE, L::DEBUG, L::INFO, L::WARN, L::ERROR];
options
.into_iter()
.map(|level| CompletionCandidate::new(level.to_string().to_lowercase()))
.collect()
}
2 changes: 2 additions & 0 deletions crates/common/tedge_config_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = { workspace = true }
[dependencies]
anyhow = { workspace = true }
camino = { workspace = true, features = ["serde1"] }
clap_complete = { workspace = true }
doku = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
Expand All @@ -21,6 +22,7 @@ tracing = { workspace = true }
url = { workspace = true }

[dev-dependencies]
clap = { workspace = true }
serde = { workspace = true, features = ["rc"] }
serde_json = { workspace = true }
toml = { workspace = true }
Expand Down
35 changes: 35 additions & 0 deletions crates/common/tedge_config_macros/impl/src/input/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use quote::format_ident;
use syn::parse_quote;
use syn::parse_quote_spanned;
use syn::spanned::Spanned;
use syn::Expr;
use syn::Lit;
use syn::Meta;
use syn::MetaNameValue;

use crate::error::combine_errors;
use crate::optional_error::OptionalError;
Expand Down Expand Up @@ -139,6 +143,37 @@ impl FieldOrGroup {
Self::Multi(group) => &group.reader,
}
}

pub fn doc(&self) -> Option<String> {
let attrs = match self {
Self::Field(field) => field.attrs(),
Self::Group(group) | Self::Multi(group) => &group.attrs,
};
doc_comments_from(attrs)
}
}

fn doc_comments_from(attrs: &[syn::Attribute]) -> Option<String> {
let doc = attrs
.iter()
.filter(|attr| attr.path() == &parse_quote!(doc))
.filter_map(|attr| match attr.meta {
Meta::NameValue(MetaNameValue {
value:
Expr::Lit(syn::ExprLit {
lit: Lit::Str(ref lit),
..
}),
..
}) => Some(lit.value()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
match doc.as_str() {
"" => None,
_ => Some(doc),
}
}

impl TryFrom<super::parse::FieldOrGroup> for FieldOrGroup {
Expand Down
Loading
Loading