Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/sheets-append-require-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Require row data when using `gws sheets +append`.
47 changes: 45 additions & 2 deletions crates/google-workspace-cli/src/helpers/sheets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ impl Helper for SheetsHelper {
Arg::new("values")
.long("values")
.help("Comma-separated values (simple strings)")
.value_name("VALUES"),
.value_name("VALUES")
.required_unless_present("json-values")
.conflicts_with("json-values"),
)
.arg(
Arg::new("json-values")
.long("json-values")
.help("JSON array of rows, e.g. '[[\"a\",\"b\"],[\"c\",\"d\"]]'")
.value_name("JSON"),
.value_name("JSON")
.required_unless_present("values")
.conflicts_with("values"),
)
Comment on lines 43 to 57
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The --values and --json-values arguments are mutually exclusive ways to provide row data. Currently, they are marked as required_unless_present, which ensures at least one is provided, but it allows both to be present simultaneously. If both are provided, the parsing logic in parse_append_args silently prioritizes --json-values and ignores --values, which is ambiguous and can lead to unexpected behavior or data loss. Marking them as conflicting ensures the user provides exactly one valid input method.

                    Arg::new("values")
                        .long("values")
                        .help("Comma-separated values (simple strings)")
                        .value_name("VALUES")
                        .required_unless_present("json-values")
                        .conflicts_with("json-values"),
                )
                .arg(
                    Arg::new("json-values")
                        .long("json-values")
                        .help("JSON array of rows, e.g. '[(\"a\",\"b\"], [\"c\",\"d\"]]'")
                        .value_name("JSON")
                        .required_unless_present("values")
                        .conflicts_with("values"),
                )

.arg(
Arg::new("range")
Expand Down Expand Up @@ -522,4 +526,43 @@ mod tests {
assert!(subcommands.contains(&"+append"));
assert!(subcommands.contains(&"+read"));
}

#[test]
fn test_append_requires_values_or_json_values() {
let helper = SheetsHelper;
let cmd = helper.inject_commands(
Command::new("sheets"),
&crate::discovery::RestDescription::default(),
);

let err = cmd
.try_get_matches_from(["sheets", "+append", "--spreadsheet", "123"])
.unwrap_err();

assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
}

#[test]
fn test_append_rejects_values_and_json_values_together() {
let helper = SheetsHelper;
let cmd = helper.inject_commands(
Command::new("sheets"),
&crate::discovery::RestDescription::default(),
);

let err = cmd
.try_get_matches_from([
"sheets",
"+append",
"--spreadsheet",
"123",
"--values",
"a,b",
"--json-values",
r#"["c","d"]"#,
])
.unwrap_err();

assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
}
}
Loading