Skip to content

Commit

Permalink
completion experiment
Browse files Browse the repository at this point in the history
ultimately i think this is pretty bad with yq.
two optional, contextual positional args and a hacky subcommand
that's only there to create completions end up kind of ruining them.

i think this is only pushed to show what we tried.

Signed-off-by: clux <sszynrae@gmail.com>
  • Loading branch information
clux committed Sep 20, 2023
1 parent 34b1062 commit 513a2a5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 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 @@ -26,6 +26,7 @@ toml = { version = "0.7.6", features = ["display"] }
serde_yaml = "0.9.25"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
clap_complete = "4.4.1"

[profile.release]
lto = true
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ apps/v1.Deployment
Output formatting such as `-y` for YAML or `-t` for TOML will require the output from `jq` to be parseable json.
If you pass on `-r`,`-c` or `-c` for raw/compact output, then this will generally not be parseable as json.

## Autocomplete

Autocompletion for most shells available via `yq completions`:

```sh
source <(yq completions bash)
```

### Debug Logs

The project respects `RUST_LOG` when set, and sends these diagnostic logs to stderr:
Expand Down
33 changes: 30 additions & 3 deletions yq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use clap::{Parser, ValueEnum};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use serde_yaml::{self, with::singleton_map_recursive, Deserializer};
use std::io::{stderr, stdin, BufReader, IsTerminal, Read, Write};
use std::path::PathBuf;
Expand All @@ -22,7 +22,17 @@ enum Output {
Toml,
}

/// A lightweight and portable Rust implementation of a common jq wrapper
#[derive(Clone, Copy, Debug, Subcommand)]
#[command(args_conflicts_with_subcommands = true)]
enum Sub {
#[command(about = "Generate completions", hide = true)]
Completions {
#[arg(help = "The shell to generate completions for")]
shell: clap_complete::Shell,
},
}

// A lightweight and portable jq wrapper
///
/// Allows doing arbitrary jq style queries editing on YAML documents.
///
Expand All @@ -36,7 +46,10 @@ enum Output {
///
/// yq -y '.[2].metadata' < manifest.yml
#[derive(Parser, Debug, Default)]
#[command(author, version, about)]
#[command(
version = clap::crate_version!(),
author = "clux <sszynrae@gmail.com>",
)]
struct Args {
/// Input format of the input file or stdin
#[arg(long, value_enum, default_value_t)]
Expand Down Expand Up @@ -68,6 +81,10 @@ struct Args {
#[arg(short, long, default_value = "false", requires = "file")]
in_place: bool,

/// Hidden extras
#[command(subcommand)]
command: Option<Sub>,

/// Query to be sent to jq (see https://jqlang.github.io/jq/manual/)
///
/// Default "."
Expand Down Expand Up @@ -277,6 +294,16 @@ fn main() -> Result<()> {
} else if args.toml_output {
args.output = Output::Toml
}
if let Some(command) = args.command {
match command {
Sub::Completions { shell } => {
let mut cmd = Args::command();
clap_complete::generate(shell, &mut cmd, "yq", &mut std::io::stdout());
return Ok(());
}
}
}

debug!("args: {:?}", args);
let input = args.read_input()?;
let stdout = args.shellout(input)?;
Expand Down

0 comments on commit 513a2a5

Please sign in to comment.