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

Add shell completion #325

Merged
merged 6 commits into from
Apr 15, 2024
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
89 changes: 87 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ chrono = "0.4"
parse_duration = "2.1.1"
atty = "0.2"
base64 = "0.22.0"
clap_generate = "3.0.3"
clap_complete = "4.5.1"

[dev-dependencies]
tempdir = "0.3.7"
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ curl <auth API> | jq -r .access_token | jwt decode -

Currently the underlying token encoding and decoding library, [`jsonwebtoken`](https://github.com/Keats/jsonwebtoken), doesn't support the SEC1 private key format and requires a conversion to the PKCS8 type. You can read more from [their own README](https://github.com/Keats/jsonwebtoken/blob/8fba79b25459eacc33a80e1ee37ff8eba64079ca/README.md#convert-sec1-private-key-to-pkcs8).

## Shell completion

`jwt-cli` supports shell completion for `bash`, `elvish`, `fish`, `powershell`, and `zsh`. To enable it, run the following command:

```sh
source <(jwt completion bash)
```

You may want to add this to your shell profile to have it available every time you open a new shell:

```sh
if hash jwt > /dev/null; then
source <(jwt completion bash)
fi
```

# Contributing

I welcome all issues and pull requests! This is my first project in rust, so this project almost certainly could be better written. All I ask is that you follow the [code of conduct](code_of_conduct.md) and use [rustfmt](https://github.com/rust-lang-nursery/rustfmt) to have a consistent project code style.
Expand Down
12 changes: 12 additions & 0 deletions src/cli_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::translators::{PayloadItem, SupportedTypes, TimeFormat};
use crate::utils::parse_duration_string;
use chrono::format::{parse, Parsed, StrftimeItems};
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use jsonwebtoken::Algorithm;
use std::path::PathBuf;

Expand All @@ -22,6 +23,17 @@ pub enum Commands {

/// Decode a JWT
Decode(DecodeArgs),

/// Print completion
Completion(CompletionArgs),
}

#[derive(Debug, Clone, Parser)]
pub struct CompletionArgs {
/// the shell to generate completions for
#[clap(value_enum)]
#[clap(index = 1)]
pub shell: Shell,
}

#[derive(Debug, Clone, Parser)]
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Parser;
use clap::{Command, CommandFactory, Parser};
use clap_complete::{generate, Generator};
use cli_config::{App, Commands, EncodeArgs};
use std::io;
use std::process::exit;
use translators::decode::{decode_token, print_decoded_token};
use translators::encode::{encode_token, print_encoded_token};
Expand All @@ -14,6 +16,10 @@ fn warn_unsupported(arguments: &EncodeArgs) {
};
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
let app = App::parse();
// let matches = config_options().get_matches();
Expand Down Expand Up @@ -41,5 +47,10 @@ fn main() {
},
);
}
Commands::Completion(arguments) => {
let mut cmd = App::command();
print_completions(arguments.shell, &mut cmd);
exit(0)
tonglil marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
Loading