-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsheets.rs
More file actions
533 lines (477 loc) · 18.8 KB
/
sheets.rs
File metadata and controls
533 lines (477 loc) · 18.8 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::Helper;
use crate::auth;
use crate::error::GwsError;
use crate::executor;
use clap::{Arg, ArgMatches, Command};
use serde_json::json;
use std::future::Future;
use std::pin::Pin;
pub struct SheetsHelper;
impl Helper for SheetsHelper {
fn inject_commands(
&self,
mut cmd: Command,
_doc: &crate::discovery::RestDescription,
) -> Command {
cmd = cmd.subcommand(
Command::new("+append")
.about("[Helper] Append a row to a spreadsheet")
.arg(
Arg::new("spreadsheet")
.long("spreadsheet")
.help("Spreadsheet ID")
.required(true)
.value_name("ID"),
)
.arg(
Arg::new("values")
.long("values")
.help("Comma-separated values (simple strings)")
.value_name("VALUES"),
)
.arg(
Arg::new("json-values")
.long("json-values")
.help("JSON array of rows, e.g. '[[\"a\",\"b\"],[\"c\",\"d\"]]'")
.value_name("JSON"),
)
.arg(
Arg::new("range")
.long("range")
.help("Target range in A1 notation (e.g. 'Sheet2!A1'). Defaults to 'A1' (first sheet)")
.value_name("RANGE"),
)
.after_help(
r#"EXAMPLES:
gws sheets +append --spreadsheet ID --values 'Alice,100,true'
gws sheets +append --spreadsheet ID --json-values '[["a","b"],["c","d"]]'
gws sheets +append --spreadsheet ID --range "Sheet2!A1" --values 'Alice,100'
TIPS:
Use --values for simple single-row appends.
Use --json-values for bulk multi-row inserts.
Use --range to target a specific sheet tab (default: A1, i.e. first sheet)."#,
),
);
cmd = cmd.subcommand(
Command::new("+read")
.about("[Helper] Read values from a spreadsheet")
.arg(
Arg::new("spreadsheet")
.long("spreadsheet")
.help("Spreadsheet ID")
.required(true)
.value_name("ID"),
)
.arg(
Arg::new("range")
.long("range")
.help("Range to read (e.g. 'Sheet1!A1:B2')")
.required(true)
.value_name("RANGE"),
)
.after_help(
"\
EXAMPLES:
gws sheets +read --spreadsheet ID --range \"Sheet1!A1:D10\"
gws sheets +read --spreadsheet ID --range Sheet1
TIPS:
Read-only — never modifies the spreadsheet.
For advanced options, use the raw values.get API.",
),
);
cmd
}
fn handle<'a>(
&'a self,
doc: &'a crate::discovery::RestDescription,
matches: &'a ArgMatches,
_sanitize_config: &'a crate::helpers::modelarmor::SanitizeConfig,
) -> Pin<Box<dyn Future<Output = Result<bool, GwsError>> + Send + 'a>> {
Box::pin(async move {
if let Some(sub_matches) = matches.subcommand_matches("+append") {
let config = parse_append_args(sub_matches);
let (params_str, body_str, scopes) = build_append_request(&config, doc)?;
let output_format = matches
.get_one::<String>("format")
.map(|s| crate::formatter::OutputFormat::from_str(s))
.unwrap_or_default();
let scope_strs: Vec<&str> = scopes.iter().map(|s| s.as_str()).collect();
let (token, auth_method) = match auth::get_token(&scope_strs).await {
Ok(t) => (Some(t), executor::AuthMethod::OAuth),
Err(_) if matches.get_flag("dry-run") => (None, executor::AuthMethod::None),
Err(e) => return Err(GwsError::Auth(format!("Sheets auth failed: {e}"))),
};
let spreadsheets_res = doc.resources.get("spreadsheets").ok_or_else(|| {
GwsError::Discovery("Resource 'spreadsheets' not found".to_string())
})?;
let values_res = spreadsheets_res.resources.get("values").ok_or_else(|| {
GwsError::Discovery("Resource 'spreadsheets.values' not found".to_string())
})?;
let append_method = values_res.methods.get("append").ok_or_else(|| {
GwsError::Discovery("Method 'spreadsheets.values.append' not found".to_string())
})?;
let pagination = executor::PaginationConfig {
page_all: false,
page_limit: 10,
page_delay_ms: 100,
};
executor::execute_method(
doc,
append_method,
Some(¶ms_str),
Some(&body_str),
token.as_deref(),
auth_method,
None,
None,
matches.get_flag("dry-run"),
&pagination,
None,
&crate::helpers::modelarmor::SanitizeMode::Warn,
&output_format,
false,
)
.await?;
return Ok(true);
}
if let Some(sub_matches) = matches.subcommand_matches("+read") {
let config = parse_read_args(sub_matches);
let (params_str, scopes) = build_read_request(&config, doc)?;
let output_format = matches
.get_one::<String>("format")
.map(|s| crate::formatter::OutputFormat::from_str(s))
.unwrap_or_default();
// Re-find method
let spreadsheets_res = doc.resources.get("spreadsheets").ok_or_else(|| {
GwsError::Discovery("Resource 'spreadsheets' not found".to_string())
})?;
let values_res = spreadsheets_res.resources.get("values").ok_or_else(|| {
GwsError::Discovery("Resource 'spreadsheets.values' not found".to_string())
})?;
let get_method = values_res.methods.get("get").ok_or_else(|| {
GwsError::Discovery("Method 'spreadsheets.values.get' not found".to_string())
})?;
let scope_strs: Vec<&str> = scopes.iter().map(|s| s.as_str()).collect();
let (token, auth_method) = match auth::get_token(&scope_strs).await {
Ok(t) => (Some(t), executor::AuthMethod::OAuth),
Err(_) if matches.get_flag("dry-run") => (None, executor::AuthMethod::None),
Err(e) => return Err(GwsError::Auth(format!("Sheets auth failed: {e}"))),
};
executor::execute_method(
doc,
get_method,
Some(¶ms_str),
None,
token.as_deref(),
auth_method,
None,
None,
matches.get_flag("dry-run"),
&executor::PaginationConfig::default(),
None,
&crate::helpers::modelarmor::SanitizeMode::Warn,
&output_format,
false,
)
.await?;
return Ok(true);
}
Ok(false)
})
}
}
fn build_append_request(
config: &AppendConfig,
doc: &crate::discovery::RestDescription,
) -> Result<(String, String, Vec<String>), GwsError> {
let spreadsheets_res = doc
.resources
.get("spreadsheets")
.ok_or_else(|| GwsError::Discovery("Resource 'spreadsheets' not found".to_string()))?;
let values_res = spreadsheets_res.resources.get("values").ok_or_else(|| {
GwsError::Discovery("Resource 'spreadsheets.values' not found".to_string())
})?;
let append_method = values_res.methods.get("append").ok_or_else(|| {
GwsError::Discovery("Method 'spreadsheets.values.append' not found".to_string())
})?;
let params = json!({
"spreadsheetId": config.spreadsheet_id,
"range": config.range,
"valueInputOption": "USER_ENTERED"
});
let body = json!({
"values": config.values
});
// Map `&String` scope URLs to owned `String`s for the return value
let scopes: Vec<String> = append_method.scopes.iter().map(|s| s.to_string()).collect();
Ok((params.to_string(), body.to_string(), scopes))
}
fn build_read_request(
config: &ReadConfig,
doc: &crate::discovery::RestDescription,
) -> Result<(String, Vec<String>), GwsError> {
// ... resource lookup omitted for brevity ...
let spreadsheets_res = doc
.resources
.get("spreadsheets")
.ok_or_else(|| GwsError::Discovery("Resource 'spreadsheets' not found".to_string()))?;
let values_res = spreadsheets_res.resources.get("values").ok_or_else(|| {
GwsError::Discovery("Resource 'spreadsheets.values' not found".to_string())
})?;
let get_method = values_res.methods.get("get").ok_or_else(|| {
GwsError::Discovery("Method 'spreadsheets.values.get' not found".to_string())
})?;
let params = json!({
"spreadsheetId": config.spreadsheet_id,
"range": config.range
});
let scopes: Vec<String> = get_method.scopes.iter().map(|s| s.to_string()).collect();
Ok((params.to_string(), scopes))
}
/// Configuration for appending values to a spreadsheet.
///
/// Holds the parsed arguments for the `+append` subcommand.
pub struct AppendConfig {
/// The ID of the spreadsheet to append to.
pub spreadsheet_id: String,
/// Target range in A1 notation (e.g. "Sheet2!A1"). Defaults to "A1".
pub range: String,
/// The rows to append, where each inner Vec represents one row.
pub values: Vec<Vec<String>>,
}
/// Parses arguments for the `+append` command.
///
/// Supports both `--values` (single row) and `--json-values` (single or multi-row).
pub fn parse_append_args(matches: &ArgMatches) -> AppendConfig {
let values = if let Some(json_str) = matches.get_one::<String>("json-values") {
// Try parsing as array-of-arrays (multi-row) first
if let Ok(parsed) = serde_json::from_str::<Vec<Vec<String>>>(json_str) {
parsed
} else if let Ok(parsed) = serde_json::from_str::<Vec<String>>(json_str) {
// Single flat array — treat as one row
vec![parsed]
} else {
eprintln!(
"Warning: --json-values is not valid JSON; expected an array or array-of-arrays"
);
Vec::new()
}
} else if let Some(values_str) = matches.get_one::<String>("values") {
vec![values_str.split(',').map(|s| s.to_string()).collect()]
} else {
Vec::new()
};
let range = matches
.get_one::<String>("range")
.cloned()
.unwrap_or_else(|| "A1".to_string());
AppendConfig {
spreadsheet_id: matches.get_one::<String>("spreadsheet").unwrap().clone(),
range,
values,
}
}
/// Configuration for reading values from a spreadsheet.
pub struct ReadConfig {
pub spreadsheet_id: String,
/// A1 notation range (e.g. "Sheet1!A1:B2").
pub range: String,
}
pub fn parse_read_args(matches: &ArgMatches) -> ReadConfig {
ReadConfig {
spreadsheet_id: matches.get_one::<String>("spreadsheet").unwrap().clone(),
range: matches.get_one::<String>("range").unwrap().clone(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::discovery::{RestDescription, RestMethod, RestResource};
use std::collections::HashMap;
fn make_mock_doc() -> RestDescription {
let mut methods = HashMap::new();
methods.insert(
"append".to_string(),
RestMethod {
scopes: vec!["https://scope".to_string()],
..Default::default()
},
);
methods.insert(
"get".to_string(),
RestMethod {
scopes: vec!["https://scope".to_string()],
..Default::default()
},
);
let mut values_res = RestResource::default();
values_res.methods = methods;
let mut spreadsheets_res = RestResource::default();
spreadsheets_res
.resources
.insert("values".to_string(), values_res);
let mut resources = HashMap::new();
resources.insert("spreadsheets".to_string(), spreadsheets_res);
RestDescription {
resources,
..Default::default()
}
}
fn make_matches_append(args: &[&str]) -> ArgMatches {
let cmd = Command::new("test")
.arg(Arg::new("spreadsheet").long("spreadsheet"))
.arg(Arg::new("values").long("values"))
.arg(Arg::new("json-values").long("json-values"))
.arg(Arg::new("range").long("range"));
cmd.try_get_matches_from(args).unwrap()
}
fn make_matches_read(args: &[&str]) -> ArgMatches {
let cmd = Command::new("test")
.arg(Arg::new("spreadsheet").long("spreadsheet"))
.arg(Arg::new("range").long("range"));
cmd.try_get_matches_from(args).unwrap()
}
#[test]
fn test_build_append_request() {
let doc = make_mock_doc();
let config = AppendConfig {
spreadsheet_id: "123".to_string(),
range: "A1".to_string(),
values: vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]],
};
let (params, body, scopes) = build_append_request(&config, &doc).unwrap();
assert!(params.contains("123"));
assert!(params.contains("USER_ENTERED"));
assert!(params.contains("A1"));
assert!(body.contains("a"));
assert!(body.contains("b"));
assert_eq!(scopes[0], "https://scope");
}
#[test]
fn test_build_append_request_with_range() {
let doc = make_mock_doc();
let config = AppendConfig {
spreadsheet_id: "123".to_string(),
range: "Sheet2!A1".to_string(),
values: vec![vec!["x".to_string()]],
};
let (params, _body, _scopes) = build_append_request(&config, &doc).unwrap();
let parsed: serde_json::Value = serde_json::from_str(¶ms).unwrap();
assert_eq!(parsed["range"], "Sheet2!A1");
}
#[test]
fn test_build_read_request() {
let doc = make_mock_doc();
let config = ReadConfig {
spreadsheet_id: "123".to_string(),
range: "A1:B2".to_string(),
};
let (params, scopes) = build_read_request(&config, &doc).unwrap();
assert!(params.contains("123"));
assert!(params.contains("A1:B2"));
assert_eq!(scopes[0], "https://scope");
}
#[test]
fn test_parse_append_args_values() {
let matches = make_matches_append(&["test", "--spreadsheet", "123", "--values", "a,b,c"]);
let config = parse_append_args(&matches);
assert_eq!(config.spreadsheet_id, "123");
assert_eq!(config.range, "A1");
assert_eq!(config.values, vec![vec!["a", "b", "c"]]);
}
#[test]
fn test_parse_append_args_with_range() {
let matches = make_matches_append(&[
"test",
"--spreadsheet",
"123",
"--range",
"Sheet2!A1",
"--values",
"a,b",
]);
let config = parse_append_args(&matches);
assert_eq!(config.range, "Sheet2!A1");
}
#[test]
fn test_parse_append_args_default_range() {
let matches = make_matches_append(&["test", "--spreadsheet", "123", "--values", "a"]);
let config = parse_append_args(&matches);
assert_eq!(config.range, "A1");
}
#[test]
fn test_parse_append_args_json_single_row() {
let matches = make_matches_append(&[
"test",
"--spreadsheet",
"123",
"--json-values",
r#"["a","b","c"]"#,
]);
let config = parse_append_args(&matches);
assert_eq!(config.values, vec![vec!["a", "b", "c"]]);
}
#[test]
fn test_parse_append_args_json_multi_row() {
let matches = make_matches_append(&[
"test",
"--spreadsheet",
"123",
"--json-values",
r#"[["Alice","100"],["Bob","200"]]"#,
]);
let config = parse_append_args(&matches);
assert_eq!(
config.values,
vec![vec!["Alice", "100"], vec!["Bob", "200"]]
);
}
#[test]
fn test_build_append_request_multi_row() {
let doc = make_mock_doc();
let config = AppendConfig {
spreadsheet_id: "123".to_string(),
range: "A1".to_string(),
values: vec![
vec!["Alice".to_string(), "100".to_string()],
vec!["Bob".to_string(), "200".to_string()],
],
};
let (_params, body, _scopes) = build_append_request(&config, &doc).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
let values = parsed["values"].as_array().unwrap();
assert_eq!(values.len(), 2);
assert_eq!(values[0], json!(["Alice", "100"]));
assert_eq!(values[1], json!(["Bob", "200"]));
}
#[test]
fn test_parse_read_args() {
let matches = make_matches_read(&["test", "--spreadsheet", "123", "--range", "A1:B2"]);
let config = parse_read_args(&matches);
assert_eq!(config.spreadsheet_id, "123");
assert_eq!(config.range, "A1:B2");
}
#[test]
fn test_inject_commands() {
let helper = SheetsHelper;
let cmd = Command::new("test");
let doc = crate::discovery::RestDescription::default();
let cmd = helper.inject_commands(cmd, &doc);
let subcommands: Vec<_> = cmd.get_subcommands().map(|s| s.get_name()).collect();
assert!(subcommands.contains(&"+append"));
assert!(subcommands.contains(&"+read"));
}
}