omni-dev 0.42.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, Datadog, Gmail, and Drive.
Documentation
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
//! Drive CLI commands.

pub(crate) mod account;
pub(crate) mod auth;
pub(crate) mod create;
pub(crate) mod dedupe;
pub(crate) mod edit;
pub(crate) mod format;
pub(crate) mod helpers;
/// `drive move` โ€” named `move_file` (not `move`, a Rust keyword) mirroring
/// `crate::cli::atlassian::confluence::move_page`'s identical workaround.
pub(crate) mod move_file;
pub(crate) mod permissions;
pub(crate) mod read;
pub(crate) mod rename;
pub(crate) mod search;
pub(crate) mod upload;

use anyhow::Result;
use clap::{Parser, Subcommand};

use crate::drive::account::DRIVE_ACCOUNT_ENV;
use crate::drive::client::DriveClient;

/// Drive: search, read, rename, and move Google Drive files via OAuth2.
#[derive(Parser)]
pub struct DriveCommand {
    /// Selects a named Drive account configured in
    /// `~/.omni-dev/settings.json` (AWS-CLI style, mirrors the top-level
    /// `--profile`) for this invocation.
    ///
    /// Orthogonal to `--profile`: switching the Drive account never changes
    /// which profile is active, and vice versa (see
    /// [ADR-0066](../../../docs/adrs/adr-0066.md),
    /// [ADR-0069](../../../docs/adrs/adr-0069.md)). Overrides
    /// `OMNI_DEV_DRIVE_ACCOUNT`. Scoped to the `drive` subtree โ€” not usable
    /// before the `drive` subcommand name, only after it, so it can't
    /// collide with an unrelated subcommand's own `--account` flag.
    #[arg(long, global = true, value_name = "NAME")]
    pub account: Option<String>,
    /// The Drive subcommand to execute.
    #[command(subcommand)]
    pub command: DriveSubcommands,
}

/// Drive subcommands.
#[derive(Subcommand)]
pub enum DriveSubcommands {
    /// Manages Drive OAuth2 credentials.
    Auth(auth::AuthCommand),
    /// Manages named Drive accounts.
    Account(account::AccountCommand),
    /// Searches Drive files.
    Search(search::SearchCommand),
    /// Reads a single Drive file's metadata or content.
    Read(read::ReadCommand),
    /// Finds Drive files sharing the same content hash.
    Dedupe(dedupe::DedupeCommand),
    /// Creates a new file or folder, gated by the folder write-permission
    /// rules (issue #1574). Requires the `drive.file` or `drive` scope
    /// (`drive auth login --write-file`/`--write-full`).
    Create(create::CreateCommand),
    /// Uploads local content as a new file, gated by the folder
    /// write-permission rules (issue #1574). Requires the `drive.file` or
    /// `drive` scope (`drive auth login --write-file`/`--write-full`).
    Upload(upload::UploadCommand),
    /// Replaces an existing file's content, gated by the folder
    /// write-permission rules (issue #1574). Requires the `drive.file`
    /// scope if `omni-dev` created the file, or the unrestricted `drive`
    /// scope for any pre-existing file (`drive auth login --write-file`
    /// or `--write-full`).
    Edit(edit::EditCommand),
    /// Renames a single Drive file. Requires the `drive.metadata` scope
    /// (`drive auth login --write`).
    Rename(rename::RenameCommand),
    /// Moves one or more Drive files into a destination folder. Requires
    /// the `drive.metadata` scope (`drive auth login --write`).
    Move(move_file::MoveCommand),
    /// Inspects the folder-scoped write-permission rules gating `drive
    /// create`/`upload`/`edit` (issue #1574).
    Permissions(permissions::PermissionsCommand),
}

impl DriveCommand {
    /// Executes the Drive command. `auth`/`account` must run without a
    /// resolved client (they manage credentials/account selection); every
    /// other subcommand resolves one shared client **once** here and
    /// threads it down via [`DriveSubcommands::dispatch`].
    pub async fn execute(self) -> Result<()> {
        // Propagates --account to DRIVE_ACCOUNT_ENV for the duration of this
        // call only (crate::drive::account::resolve_account reads it),
        // mirroring GmailCommand::execute. Set *before* matching Auth/
        // Account: those subcommands need the resolved account too (e.g.
        // `drive auth login --account work`). The guard restores the
        // ambient value (or removes the var) on drop at the end of this
        // function, so execute() is safe to call more than once per process
        // (#1538).
        let _account_guard = self
            .account
            .as_ref()
            .map(|account| crate::utils::env::ScopedEnvVar::set(DRIVE_ACCOUNT_ENV, account));

        match self.command {
            DriveSubcommands::Auth(cmd) => cmd.execute().await,
            DriveSubcommands::Account(cmd) => cmd.execute(),
            // Permissions' three leaves have mixed client needs (`show` is
            // config-only, `lookup-folder`/`check` both call the Drive
            // API) โ€” like Auth, it resolves its own client lazily per leaf
            // rather than sharing the single eager resolution below.
            DriveSubcommands::Permissions(cmd) => cmd.execute().await,
            command => {
                let client = helpers::create_client()?;
                command.dispatch(&client).await
            }
        }
    }
}

impl DriveSubcommands {
    /// Routes a non-`Auth`/`Account`/`Permissions` subcommand against the
    /// shared client. Those three arms are unreachable: all are handled
    /// before client resolution in [`DriveCommand::execute`].
    async fn dispatch(self, client: &DriveClient) -> Result<()> {
        match self {
            Self::Auth(_) => unreachable!("Auth is dispatched before client resolution"),
            Self::Account(_) => unreachable!("Account is dispatched before client resolution"),
            Self::Permissions(_) => {
                unreachable!("Permissions is dispatched before client resolution")
            }
            Self::Search(cmd) => cmd.execute(client).await,
            Self::Read(cmd) => cmd.execute(client).await,
            Self::Dedupe(cmd) => cmd.execute(client).await,
            Self::Create(cmd) => cmd.execute(client).await,
            Self::Upload(cmd) => cmd.execute(client).await,
            Self::Edit(cmd) => cmd.execute(client).await,
            Self::Rename(cmd) => cmd.execute(client).await,
            Self::Move(cmd) => cmd.execute(client).await,
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::cli::drive::format::OutputFormat;
    use crate::drive::auth::{DriveCredentials, DriveGrantedScopes};
    use crate::utils::secret::Secret;

    fn dead_credentials() -> DriveCredentials {
        DriveCredentials {
            client_id: "client".to_string(),
            client_secret: Secret::new("secret"),
            refresh_token: Secret::new("refresh"),
            scope: DriveGrantedScopes::READONLY,
        }
    }

    fn dead_client() -> DriveClient {
        DriveClient::new("http://127.0.0.1:1", &dead_credentials()).unwrap()
    }

    #[tokio::test]
    async fn execute_routes_auth_subcommand_and_surfaces_missing_credentials() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveCommand {
            account: None,
            command: DriveSubcommands::Auth(auth::AuthCommand {
                command: auth::AuthSubcommands::Status(auth::StatusCommand { all: false }),
            }),
        };
        let err = cmd.execute().await.unwrap_err();
        assert!(err.to_string().contains("not configured"));
    }

    #[tokio::test]
    async fn execute_non_auth_subcommand_errors_when_credentials_missing() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveCommand {
            account: None,
            command: DriveSubcommands::Search(search::SearchCommand {
                query: "name contains 'report'".to_string(),
                limit: 10,
                output: OutputFormat::Table,
            }),
        };
        let err = cmd.execute().await.unwrap_err();
        assert!(err.to_string().contains("not configured"));
    }

    #[tokio::test]
    async fn execute_restores_account_env_var_after_return() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveCommand {
            account: Some("work".to_string()),
            command: DriveSubcommands::Account(account::AccountCommand {
                command: account::AccountSubcommands::List(account::list::ListCommand {
                    output: OutputFormat::Table,
                }),
            }),
        };
        cmd.execute().await.unwrap();
        assert_eq!(std::env::var(DRIVE_ACCOUNT_ENV).ok(), None);
    }

    #[tokio::test]
    async fn execute_restores_previous_account_env_var_after_return() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        std::env::set_var(DRIVE_ACCOUNT_ENV, "personal");

        let cmd = DriveCommand {
            account: Some("work".to_string()),
            command: DriveSubcommands::Account(account::AccountCommand {
                command: account::AccountSubcommands::List(account::list::ListCommand {
                    output: OutputFormat::Table,
                }),
            }),
        };
        cmd.execute().await.unwrap();
        assert_eq!(
            std::env::var(DRIVE_ACCOUNT_ENV).ok().as_deref(),
            Some("personal")
        );
    }

    #[tokio::test]
    async fn execute_does_not_leak_account_across_sequential_calls() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let account_list_cmd = || DriveCommand {
            account: None,
            command: DriveSubcommands::Account(account::AccountCommand {
                command: account::AccountSubcommands::List(account::list::ListCommand {
                    output: OutputFormat::Table,
                }),
            }),
        };

        let first = DriveCommand {
            account: Some("alpha".to_string()),
            ..account_list_cmd()
        };
        first.execute().await.unwrap();
        assert_eq!(std::env::var(DRIVE_ACCOUNT_ENV).ok(), None);

        // If the first call's value had leaked, this second call โ€” which
        // omits --account entirely โ€” would still see it via the env var.
        account_list_cmd().execute().await.unwrap();
        assert_eq!(std::env::var(DRIVE_ACCOUNT_ENV).ok(), None);
    }

    #[tokio::test]
    async fn execute_absent_account_leaves_ambient_env_var_untouched() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        std::env::set_var(DRIVE_ACCOUNT_ENV, "personal");

        let cmd = DriveCommand {
            account: None,
            command: DriveSubcommands::Account(account::AccountCommand {
                command: account::AccountSubcommands::List(account::list::ListCommand {
                    output: OutputFormat::Table,
                }),
            }),
        };
        cmd.execute().await.unwrap();
        assert_eq!(
            std::env::var(DRIVE_ACCOUNT_ENV).ok().as_deref(),
            Some("personal")
        );
    }

    #[tokio::test]
    async fn execute_routes_account_list_without_client_resolution() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveCommand {
            account: None,
            command: DriveSubcommands::Account(account::AccountCommand {
                command: account::AccountSubcommands::List(account::list::ListCommand {
                    output: OutputFormat::Table,
                }),
            }),
        };
        cmd.execute().await.unwrap();
    }

    #[tokio::test]
    async fn execute_routes_permissions_show_without_client_resolution() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveCommand {
            account: None,
            command: DriveSubcommands::Permissions(permissions::PermissionsCommand {
                command: permissions::PermissionsSubcommands::Show(
                    permissions::show::ShowCommand {
                        output: OutputFormat::Table,
                    },
                ),
            }),
        };
        cmd.execute().await.unwrap();
    }

    #[tokio::test]
    async fn execute_routes_permissions_check_and_surfaces_missing_credentials() {
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveCommand {
            account: None,
            command: DriveSubcommands::Permissions(permissions::PermissionsCommand {
                command: permissions::PermissionsSubcommands::Check(
                    permissions::check::CheckCommand {
                        id: "f1".to_string(),
                        operation: permissions::check::OperationArg::Read,
                        output: OutputFormat::Table,
                    },
                ),
            }),
        };
        let err = cmd.execute().await.unwrap_err();
        assert!(err.to_string().contains("not configured"));
    }

    #[tokio::test]
    async fn dispatch_routes_search() {
        let cmd = DriveSubcommands::Search(search::SearchCommand {
            query: "name contains 'x'".to_string(),
            limit: 10,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_err());
    }

    #[tokio::test]
    async fn dispatch_routes_read() {
        let cmd = DriveSubcommands::Read(read::ReadCommand {
            file_id: "f1".to_string(),
            content: false,
            export_mime_type: None,
            out_file: None,
            verify: false,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_err());
    }

    #[tokio::test]
    async fn dispatch_routes_dedupe() {
        let cmd = DriveSubcommands::Dedupe(dedupe::DedupeCommand {
            query: "name contains 'x'".to_string(),
            limit: 10,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_err());
    }

    #[tokio::test]
    async fn dispatch_routes_create() {
        // Unlike rename/move (whose engine fns return `Result` and
        // propagate a network error via `?`), `create`'s engine fn always
        // returns an `Ok`-shaped `CreateOutcome` โ€” a fetch failure against
        // the dead client becomes an embedded `Failed{detail}`, matching
        // the exit-0-regardless-of-outcome convention (ADR-0071 ยง12), not
        // a dispatch-level error. Needs env isolation (unlike the other
        // dispatch_routes_* tests): `create`'s CLI layer resolves the
        // active account's write_permissions.rules via `Settings::load()`,
        // so without a clean $HOME it reads whatever real
        // ~/.omni-dev/settings.json this process has.
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();

        let cmd = DriveSubcommands::Create(create::CreateCommand {
            name: "New File".to_string(),
            parent: "parent-1".to_string(),
            folder: false,
            mime_type: None,
            dry_run: false,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_ok());
    }

    #[tokio::test]
    async fn dispatch_routes_upload() {
        // Same env-isolation and exit-0-regardless-of-outcome reasoning as
        // dispatch_routes_create.
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        let content_dir = tempfile::tempdir().unwrap();
        let local_path = content_dir.path().join("upload-me.txt");
        std::fs::write(&local_path, b"content").unwrap();

        let cmd = DriveSubcommands::Upload(upload::UploadCommand {
            local_path,
            parent: "parent-1".to_string(),
            name: None,
            mime_type: None,
            dry_run: false,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_ok());
    }

    #[tokio::test]
    async fn dispatch_routes_edit() {
        // Same env-isolation and exit-0-regardless-of-outcome reasoning as
        // dispatch_routes_create/dispatch_routes_upload.
        let guard = crate::drive::test_support::EnvGuard::take();
        let _dir = guard.clear_credentials();
        let content_dir = tempfile::tempdir().unwrap();
        let content_path = content_dir.path().join("new-content.txt");
        std::fs::write(&content_path, b"content").unwrap();

        let cmd = DriveSubcommands::Edit(edit::EditCommand {
            file_id: "f1".to_string(),
            content: content_path.to_str().unwrap().to_string(),
            mime_type: None,
            dry_run: false,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_ok());
    }

    #[tokio::test]
    async fn dispatch_routes_rename() {
        let cmd = DriveSubcommands::Rename(rename::RenameCommand {
            file_id: "f1".to_string(),
            new_name: "New Name".to_string(),
            dry_run: false,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_err());
    }

    #[tokio::test]
    async fn dispatch_routes_move() {
        let cmd = DriveSubcommands::Move(move_file::MoveCommand {
            file_ids: vec!["f1".to_string()],
            to: "dest1".to_string(),
            allow_visibility_increase: false,
            allow_visibility_decrease: false,
            allow_drive_boundary_crossing: false,
            dry_run: false,
            output: OutputFormat::Table,
        });
        assert!(cmd.dispatch(&dead_client()).await.is_err());
    }
}