1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::path::PathBuf;

use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[command(name = "quartz")]
#[command(author = "Eduardo R. <contato@edurodrigues.dev>")]
#[command(about = "API Client made into a CLI tool", long_about = None, version)]
pub struct Cli {
    /// Run quartz using a specific handle
    #[arg(short = 'x', value_name = "HANDLE")]
    pub from_handle: Option<String>,

    /// Apply context on endpoint as soon as possible. Allows to get resolved information on
    /// output
    #[arg(short = 'c', long)]
    pub apply_context: bool,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Initialize quartz
    Init { directory: Option<PathBuf> },
    /// Send request using the current handle's endpoint and outputs the response
    Send,
    /// Create a new handle
    Create {
        handle: String,

        /// Set handle's endpoint URL
        #[arg(long)]
        url: Option<String>,

        /// Set handle's endpoint method value
        #[arg(long)]
        method: Option<String>,

        /// Set a query entry in "key=value" format.
        #[arg(long)]
        query: Vec<String>,

        /// Set a header entry in "<key>: <value>" format. This argument can be passed multiple times
        #[arg(long)]
        header: Vec<String>,

        /// Immediatly switches to this handle after creating it.
        #[arg(name = "use", long)]
        switch: bool,
    },
    /// Switch to a handle
    Use { handle: String },
    /// Print the current status of quartz
    Status {
        #[command(subcommand)]
        command: StatusCommands,
    },
    /// Lists available handles
    #[command(alias = "ls")]
    List {
        /// Set a limit for how deep the listing goes in sub-handles
        #[arg(long, value_name = "N")]
        depth: Option<u16>,
    },
    /// Delete the specified handle recursively
    #[command(alias = "rm")]
    Remove {
        /// Endpoint specification
        handle: String,
    },
    /// Print endpoint informations at a handle
    Show { handle: Option<String> },
    /// Open an editor to modify endpoint in use
    Edit {
        #[arg(long)]
        /// Defines the editor to be used for that run, overriding the quartz settings.
        editor: Option<String>,
    },
    /// Manage current handle's endpoint URL
    Url {
        #[command(subcommand)]
        command: EndpointUrlCommands,
    },
    /// Manage current handle's endpoint method
    Method {
        #[command(subcommand)]
        command: EndpointMethodCommands,
    },
    /// Manage current handle's endpoint query params
    Query {
        #[command(subcommand)]
        command: EndpointQueryCommands,
    },
    /// Manage current handle's endpoint headers
    Header {
        /// Add new header entry in "key: value" format
        #[arg(long, value_name = "HEADER")]
        set: Vec<String>,

        /// Print a header value
        #[arg(long, value_name = "KEY")]
        get: Option<String>,

        /// Remove a header
        #[arg(long, value_name = "KEY")]
        remove: Vec<String>,

        /// Print headers
        #[arg(long)]
        list: bool,
    },
    /// Manage current handle's endpoint request body
    Body {
        /// Expect a new request body via standard input
        #[arg(long)]
        stdin: bool,

        /// Open an editor to modify the endpoint's request body
        #[arg(long, short)]
        edit: bool,

        /// Print request body
        #[arg(long, short)]
        print: bool,
    },
    /// Print request history
    History {
        /// Maximum number of requests to be listed
        #[arg(short = 'n', long, value_name = "N")]
        max_count: Option<usize>,
        /// Format date time output
        #[arg(long, value_name = "FORMAT")]
        date: Option<String>,
    },
    Context {
        #[command(subcommand)]
        command: ContextCommands,
    },
    /// Manage current context's variables
    #[command(alias = "var")]
    Variable {
        /// Print a variable value
        #[arg(long, value_name = "KEY")]
        get: Option<String>,

        /// Set a variable: key=value
        #[arg(long, value_name = "VARIABLE")]
        set: Vec<String>,

        /// Print all variables
        #[arg(long)]
        list: bool,

        /// Open an editor to modify the context variables file
        #[arg(short, long)]
        edit: bool,
    },
    /// Manage configuration for quartz
    Config {
        #[command(subcommand)]
        command: ConfigCommands,
    },
}

#[derive(Debug, Subcommand)]
pub enum StatusCommands {
    /// Print the handle for the endpoint in use
    #[command(name = "--endpoint")]
    Endpoint,

    /// Print the context in use
    #[command(name = "--context")]
    Context,
}

#[derive(Debug, Subcommand)]
pub enum EndpointUrlCommands {
    /// Print URL
    #[command(name = "--get")]
    Get {
        /// Combine URL with query params
        #[arg(long)]
        full: bool,
    },

    /// Set a value for URL
    #[command(name = "--set")]
    Set { url: String },
}

#[derive(Debug, Subcommand)]
pub enum EndpointMethodCommands {
    /// Print method
    #[command(name = "--get")]
    Get,

    /// Set a value for method
    #[command(name = "--set")]
    Set { method: String },
}

#[derive(Debug, Subcommand)]
pub enum EndpointQueryCommands {
    /// Print query param value
    #[command(name = "--get")]
    Get { key: Option<String> },

    /// Set query param value
    #[command(name = "--set")]
    Set { query: String },

    /// Remove query param
    #[command(name = "--remove")]
    Remove { key: String },

    /// List all query params
    #[command(name = "--list")]
    List,
}

#[derive(Debug, Subcommand)]
pub enum ConfigCommands {
    /// Open an editor to modify ~/.quartz.toml
    #[command(name = "--edit")]
    Edit,

    /// Print configuration value
    #[command(name = "--get")]
    Get { key: String },

    /// Set a configuration
    #[command(name = "--set")]
    Set { key: String, value: String },

    /// Print ~/.quartz.toml
    #[command(name = "--list")]
    List,
}

#[derive(Debug, Subcommand)]
pub enum ContextCommands {
    /// Create a new context
    Create {
        name: String,
        /// Copy variables from another context
        #[arg(short, long, value_name = "CONTEXT")]
        copy: Option<String>,
    },
    /// Switch to another context
    Use { context: String },
    /// Print all available contexts
    #[command(alias = "ls")]
    List,
    /// Delete a context
    #[command(alias = "rm")]
    Remove { context: String },
}