1use std::io::Write;
7
8use clap::{ArgAction, CommandFactory, Parser, Subcommand};
9use clap_complete::Shell;
10
11use crate::commands;
12use crate::context::{Ctx, GlobalArgs};
13use crate::errors::CliError;
14use crate::output::Format;
15
16#[derive(Debug, Parser)]
18#[command(
19 name = "qn",
20 version,
21 about = "Command-line interface for the Quicknode API.",
22 long_about = "qn lets you manage Quicknode endpoints, streams, webhooks, and the KV store from the terminal.\n\n\
23 Use `qn <noun> --help` (e.g. `qn endpoint --help`) for command details.\n\n\
24 Authentication is resolved in this order: --api-key flag, then the config file\n\
25 (--config-file path if given, else ~/.config/qn/config.toml). Run `qn auth login`\n\
26 to save a key the first time.",
27 propagate_version = true,
28 disable_help_subcommand = true,
29 disable_help_flag = true,
32 disable_version_flag = true,
33 after_help = "Examples:\n \
34 qn auth login\n \
35 qn endpoint create --chain ethereum --network mainnet\n \
36 qn endpoint list -o json\n \
37 qn endpoint logs ep-1234 --from 1h\n \
38 qn chain list\n\n\
39 AI agents: run 'qn agent context' for a machine-readable usage guide.",
40 next_help_heading = "Global options"
43)]
44pub struct Cli {
45 #[arg(long, global = true)]
47 pub api_key: Option<String>,
48
49 #[arg(long, global = true, value_name = "PATH")]
51 pub config_file: Option<std::path::PathBuf>,
52
53 #[arg(short = 'o', long = "format", global = true, value_enum)]
57 pub format: Option<Format>,
58
59 #[arg(long, global = true)]
61 pub no_color: bool,
62
63 #[arg(short, long, global = true)]
65 pub quiet: bool,
66
67 #[arg(short = 'w', long = "wide", global = true)]
71 pub wide: bool,
72
73 #[arg(short, long, global = true)]
75 pub verbose: bool,
76
77 #[arg(long, global = true)]
79 pub no_input: bool,
80
81 #[arg(long, global = true, default_value_t = 3, value_name = "N")]
85 pub retries: u32,
86
87 #[arg(short = 'y', long = "yes", global = true, action = ArgAction::Count)]
89 pub yes: u8,
90
91 #[arg(long, global = true, hide = true)]
94 pub base_url: Option<String>,
95
96 #[arg(short = 'h', long, global = true, action = ArgAction::Help)]
98 pub help: Option<bool>,
99
100 #[arg(short = 'V', long, global = true, action = ArgAction::Version)]
102 pub version: Option<bool>,
103
104 #[command(subcommand)]
105 pub command: Command,
106}
107
108#[derive(Debug, Subcommand)]
109pub enum Command {
110 Auth(commands::auth::Args),
112
113 Agent(commands::agent::Args),
115
116 #[command(visible_alias = "endpoints")]
118 Endpoint(commands::endpoint::Args),
119
120 #[command(visible_alias = "teams")]
122 Team(commands::team::Args),
123
124 Usage(commands::usage::Args),
126
127 Metrics(commands::metrics::Args),
129
130 #[command(visible_alias = "chains")]
132 Chain(commands::chain::Args),
133
134 Billing(commands::billing::Args),
136
137 #[command(visible_alias = "streams")]
139 Stream(commands::stream::Args),
140
141 #[command(visible_alias = "webhooks")]
143 Webhook(commands::webhook::Args),
144
145 Kv(commands::kv::Args),
147
148 #[command(after_long_help = "### bash\n\n \
159 First, ensure that you install `bash-completion` using your package manager.\n\n \
160 After, add this to your `~/.bashrc`:\n\n \
161 eval \"$(qn completions bash)\"\n\n\
162 ### zsh\n\n \
163 Homebrew already creates this `_qn` file for you on `brew install`. To\n \
164 set it up manually, generate the script into a directory on your\n \
165 `$fpath` (Apple Silicon shown; Intel brew uses\n \
166 `/usr/local/share/zsh/site-functions`):\n\n \
167 qn completions zsh > /opt/homebrew/share/zsh/site-functions/_qn\n\n \
168 Ensure that the following is present in your `~/.zshrc`:\n\n \
169 autoload -U compinit\n \
170 compinit\n\n \
171 See the zsh manual for details:\n \
172 https://zsh.sourceforge.io/Doc/Release/Completion-System.html\n\n\
173 ### fish\n\n \
174 Generate a `qn.fish` completion script:\n\n \
175 qn completions fish > ~/.config/fish/completions/qn.fish\n\n\
176 ### PowerShell\n\n \
177 Add the following line to your profile script (`$PROFILE`):\n\n \
178 qn completions powershell | Out-String | Invoke-Expression\n\n \
179 Or append the generated script so it loads each session:\n\n \
180 qn completions powershell >> $PROFILE")]
181 Completions {
182 #[arg(value_enum)]
184 shell: Shell,
185 },
186}
187
188impl Cli {
189 pub fn global_args(&self) -> GlobalArgs {
191 GlobalArgs {
192 api_key: self.api_key.clone(),
193 config_file: self.config_file.clone(),
194 format: self.format,
195 wide: self.wide,
196 no_color: self.no_color,
199 quiet: self.quiet,
200 verbose: self.verbose,
201 no_input: self.no_input,
202 yes_count: self.yes,
203 retries: self.retries,
204 base_url: self.base_url.clone(),
205 }
206 }
207
208 pub async fn run(self) -> Result<(), CliError> {
214 let global = self.global_args();
215 match self.command {
216 Command::Completions { shell } => {
217 let mut cmd = <Self as CommandFactory>::command();
218 let bin_name = cmd.get_name().to_string();
219 let mut out = std::io::stdout().lock();
220 clap_complete::generate(shell, &mut cmd, bin_name, &mut out);
221 out.flush()?;
222 Ok(())
223 }
224 Command::Auth(args) => commands::auth::run(args, global).await,
225 Command::Agent(args) => commands::agent::run(args, global).await,
226 Command::Endpoint(args) => {
227 commands::endpoint::run(args, Ctx::from_global(global)?).await
228 }
229 Command::Team(args) => commands::team::run(args, Ctx::from_global(global)?).await,
230 Command::Usage(args) => commands::usage::run(args, Ctx::from_global(global)?).await,
231 Command::Metrics(args) => commands::metrics::run(args, Ctx::from_global(global)?).await,
232 Command::Chain(args) => commands::chain::run(args, Ctx::from_global(global)?).await,
233 Command::Billing(args) => commands::billing::run(args, Ctx::from_global(global)?).await,
234 Command::Stream(args) => commands::stream::run(args, Ctx::from_global(global)?).await,
235 Command::Webhook(args) => commands::webhook::run(args, Ctx::from_global(global)?).await,
236 Command::Kv(args) => commands::kv::run(args, Ctx::from_global(global)?).await,
237 }
238 }
239}