quartz_cli/
cli.rs

1use crate::action;
2use clap::{Parser, Subcommand};
3
4#[derive(Debug, Parser)]
5#[command(name = "quartz")]
6#[command(author = "Eduardo R. <contato@edurodrigues.dev>")]
7#[command(about = "Text-based API Client", long_about = None, version)]
8pub struct Cli {
9    /// Run command with given handle
10    #[arg(short = 'x', value_name = "HANDLE")]
11    pub from_handle: Option<String>,
12
13    /// Apply environment on endpoint as soon as possible. Allows to get resolved information on
14    /// output
15    #[arg(short = 'c', long)]
16    pub apply_environment: bool,
17
18    #[command(subcommand)]
19    pub command: Cmd,
20}
21
22#[derive(Debug, Subcommand)]
23pub enum Cmd {
24    /// Initialize quartz
25    Init(action::init::Args),
26    /// Send request using the current handle's endpoint and outputs the response
27    Send(action::send::Args),
28    /// Create a new handle
29    Create(action::handle::CreateArgs),
30    /// Switch handle or edit its endpoint
31    Use(action::handle::SwitchArgs),
32
33    /// Lists available handles
34    #[command(name = "ls", alias = "list")]
35    Ls(action::ls::Args),
36
37    /// Copy an endpoint from one handle to another
38    #[command(name = "cp", alias = "copy")]
39    Cp(action::handle::CpArgs),
40
41    /// Move handles
42    #[command(name = "mv", alias = "move")]
43    Mv(action::handle::MvArgs),
44
45    /// Delete handles
46    #[command(name = "rm", alias = "remove")]
47    Rm(action::handle::RmArgs),
48
49    /// Print out endpoint informations
50    Show {
51        #[command(subcommand)]
52        command: ShowCmd,
53    },
54
55    /// Open an editor to modify endpoint in use
56    Edit,
57
58    /// Manage current endpoint's query params
59    Query {
60        #[command(subcommand)]
61        command: QueryCmd,
62    },
63    /// Manage current endpoint's headers. Without subcomand, it prints the headers list.
64    #[command(alias = "headers")]
65    Header {
66        #[command(subcommand)]
67        command: HeaderCmd,
68    },
69    /// Manage current handle's endpoint request body
70    Body(action::body::Args),
71    /// Print information about last request or response
72    Last {
73        #[command(subcommand)]
74        command: Option<LastCmd>,
75    },
76    /// Print request history
77    History(action::history::Args),
78    /// Manage project's environments
79    #[command(name = "env", alias = "environment")]
80    Env {
81        #[command(subcommand)]
82        command: EnvCmd,
83    },
84    /// Manage current environment's variables
85    #[command(name = "var", alias = "variable")]
86    Var {
87        #[command(subcommand)]
88        command: VarCmd,
89    },
90    /// Manage configuration for quartz
91    Config {
92        #[command(subcommand)]
93        command: ConfigCmd,
94    },
95}
96
97#[derive(Debug, Subcommand)]
98pub enum LastCmd {
99    /// Print most recent handle used
100    Handle,
101
102    /// Print last request information
103    #[command(name = "req", alias = "request")]
104    Req,
105    /// Print last response information
106    #[command(name = "res", alias = "response")]
107    Res {
108        #[command(subcommand)]
109        command: Option<LastResCmd>,
110    },
111}
112
113#[derive(Debug, Subcommand)]
114pub enum LastResCmd {
115    Head,
116    Body,
117}
118
119#[derive(Debug, Subcommand)]
120pub enum QueryCmd {
121    /// Print query param value
122    Get(action::query::GetArgs),
123
124    /// Set query param value
125    Set(action::query::SetArgs),
126
127    /// Remove query param
128    #[command(name = "rm", alias = "remove")]
129    Rm(action::query::RmArgs),
130
131    /// List all query params
132    #[command(name = "ls", alias = "list")]
133    Ls,
134}
135
136#[derive(Debug, Subcommand)]
137pub enum HeaderCmd {
138    /// Print a header value
139    Get { key: String },
140
141    /// Add new or existent header. Expects "key: value" format
142    Set { header: Vec<String> },
143
144    /// Remove a header
145    #[command(name = "rm", alias = "remove")]
146    Rm { key: Vec<String> },
147
148    /// Print headers
149    #[command(name = "ls", alias = "list")]
150    Ls,
151}
152
153#[derive(Debug, Subcommand)]
154pub enum ShowCmd {
155    Url,
156    Method,
157    /// Display endpoint's headers
158    Headers {
159        key: Option<String>,
160    },
161    /// Display endpoint's query params
162    Query {
163        key: Option<String>,
164    },
165    /// Display endpoint's request body
166    Body,
167    /// Display current handle
168    Handle,
169    /// Display current environment
170    #[command(name = "env", alias = "environment")]
171    Env,
172
173    /// Display environment cookies
174    Cookies(action::cookie::PrintArgs),
175    /// Generate code snippet for endpoint
176    Snippet(action::snippet::Args),
177    /// Display endpoint configuration file
178    Endpoint,
179}
180
181#[derive(Debug, Subcommand)]
182pub enum SnippetCmd {
183    Curl(crate::snippet::Curl),
184    Http,
185}
186
187#[derive(Debug, Subcommand)]
188pub enum ConfigCmd {
189    /// Open an editor to modify ~/.quartz.toml
190    Edit,
191
192    /// Print configuration value
193    Get(action::config::GetArgs),
194
195    /// Set a configuration
196    Set(action::config::SetArgs),
197
198    /// Print ~/.quartz.toml
199    #[command(name = "ls", alias = "list")]
200    Ls,
201}
202
203#[derive(Debug, Subcommand)]
204pub enum BodyCmd {
205    /// Print request body to stdout
206    Show,
207
208    /// Expect a new request body via standard input
209    Stdin,
210
211    /// Open an editor to modify the endpoint's request body
212    Edit,
213}
214
215#[derive(Debug, Subcommand)]
216pub enum EnvCmd {
217    /// Create a new environment
218    Create(action::env::CreateArgs),
219
220    /// Switch to another environment
221    Use(action::env::SwitchArgs),
222
223    /// Print all available environments
224    #[command(name = "ls", alias = "list")]
225    Ls,
226
227    /// Copy variables from a environment to a new or existing one
228    #[command(name = "cp", alias = "copy")]
229    Cp(action::env::CpArgs),
230
231    /// Delete a environment
232    #[command(name = "rm", alias = "remove")]
233    Rm(action::env::RmArgs),
234    Header {
235        #[command(subcommand)]
236        command: HeaderCmd,
237    },
238}
239
240#[derive(Debug, Subcommand)]
241pub enum VarCmd {
242    /// Open an editor to modify variables
243    Edit,
244
245    /// Display variable value
246    Get(action::var::GetArgs),
247
248    /// Add a new or existent variable value
249    Set(action::var::SetArgs),
250
251    /// Remove variables
252    Rm(action::var::RmArgs),
253
254    /// Display the list of variables
255    #[command(name = "ls", alias = "list")]
256    Ls,
257}