Skip to content
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
12 changes: 12 additions & 0 deletions cli/Cargo.lock

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

4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ members = [".", "connector"]

[dependencies]
blake3 = { version = "1", default-features = false, features = ["std"] }
clap = { version = "4.6", features = ["derive"] }
clap = { version = "4.6", features = ["derive", "unstable-ext"] }
password-auth = { version = "1.0", features = ["argon2"] }
rpassword = "7.4"
clap_complete = "4"
clap_complete = { version = "4", features = ["unstable-dynamic"] }
strum = "0.28"
strum_macros = "0.28"
serde = { version = "1.0", features = ["derive"] }
Expand Down
24 changes: 18 additions & 6 deletions cli/src/commands/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use crate::config::*;
use crate::input::*;
use crate::output::{ExitKind, Output, to_exit_kind};
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{Shell, generate};
use clap_complete::{CompleteEnv, Shell};
use connector::auth::{
CliDevicePollRequest, CliPollOutcome, MakeLoginRequest, MakeUserRequest,
};
use std::io;
use std::io::{self, Write};
use std::process::Command;
use std::time::Duration;

#[derive(Parser, Debug)]
#[command(name = "Gradient", display_name = "Gradient", bin_name = "gradient", author = "Wavelens", version, about, long_about = None)]
#[command(name = "gradient", display_name = "Gradient", bin_name = "gradient", author = "Wavelens", version, about, long_about = None)]
#[command(arg_required_else_help = true, subcommand_required = true)]
struct Cli {
/// Emit machine-readable JSON envelopes; disables interactive prompts.
Expand Down Expand Up @@ -128,15 +128,27 @@ enum MainCommands {
Hash,
}

/// Intercept dynamic completion requests (`COMPLETE=<shell> gradient …`) and exit.
/// Must run before the tokio runtime starts: completers build their own runtime.
pub fn complete_env() {
CompleteEnv::with_factory(Cli::command).complete();
}

pub async fn run_cli() -> std::io::Result<()> {
let cli = Cli::parse();
let out = Output::new(cli.json);

match cli.cmd {
MainCommands::Completion { shell } => {
let mut app = Cli::command();
let bin_name = app.get_name().to_string();
generate(shell, &mut app, bin_name, &mut io::stdout());
let exe = std::env::current_exe()
.unwrap_or_else(|e| out.err(ExitKind::Api, format!("cannot locate binary: {e}")));
let output = Command::new(&exe)
.env("COMPLETE", shell.to_string())
.output()
.unwrap_or_else(|e| {
out.err(ExitKind::Api, format!("failed to generate completions: {e}"))
});
io::stdout().write_all(&output.stdout).ok();
}
MainCommands::Config { key, value } => {
set_get_value_from_string(key, value, false)
Expand Down
5 changes: 5 additions & 0 deletions cli/src/commands/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*/

use crate::commands::cache_nar;
use crate::commands::completion;
use crate::input::{client_from_config, handle_input};
use crate::output::{ExitKind, Output, to_exit_kind};
use clap::Subcommand;
use clap_complete::engine::ArgValueCompleter;
use connector::caches::MakeCacheRequest;
use std::fs;

Expand All @@ -25,6 +27,7 @@ pub enum Commands {
},
List,
Edit {
#[arg(add = ArgValueCompleter::new(completion::complete_caches))]
name: String,
#[arg(short, long)]
display_name: Option<String>,
Expand All @@ -34,9 +37,11 @@ pub enum Commands {
priority: Option<i32>,
},
Delete {
#[arg(add = ArgValueCompleter::new(completion::complete_caches))]
name: String,
},
Show {
#[arg(add = ArgValueCompleter::new(completion::complete_caches))]
name: String,
},
/// Build a netrc entry locally for a cache and install it into a netrc file.
Expand Down
Loading
Loading