tangled-cli 0.1.0

CLI for interacting with Tangled, an AT Protocol-based git collaboration platform
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
use clap::{Args, Parser, Subcommand, ValueEnum};

#[derive(Parser, Debug, Clone)]
#[command(name = "tang", author, version, about = "Tangled CLI", long_about = None)]
pub struct Cli {
    /// Config file path override
    #[arg(long, global = true)]
    pub config: Option<String>,

    /// Output format
    #[arg(long, global = true, value_enum, default_value_t = OutputFormat::Table)]
    pub format: OutputFormat,

    /// Verbose output
    #[arg(long, global = true, action = clap::ArgAction::Count)]
    pub verbose: u8,

    /// Quiet output
    #[arg(long, global = true, default_value_t = false)]
    pub quiet: bool,

    /// Disable colors
    #[arg(long, global = true, default_value_t = false)]
    pub no_color: bool,

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

#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
    Table,
    Json,
    #[value(alias = "yml")]
    Yaml,
}

#[derive(Subcommand, Debug, Clone)]
pub enum Command {
    /// Authentication commands
    #[command(subcommand)]
    Auth(AuthCommand),
    /// Repository commands
    #[command(subcommand)]
    Repo(RepoCommand),
    /// Issue commands
    #[command(subcommand)]
    Issue(IssueCommand),
    /// Pull commands
    #[command(subcommand, alias = "pr")]
    Pull(PullCommand),
    /// Knot management commands
    #[command(subcommand)]
    Knot(KnotCommand),
    /// Spindle integration commands
    #[command(subcommand)]
    Spindle(SpindleCommand),
}

#[derive(Subcommand, Debug, Clone)]
pub enum AuthCommand {
    /// Login with OAuth (default) or an app password
    Login(AuthLoginArgs),
    /// Show authentication status
    Status,
    /// Logout and clear session
    Logout,
}

#[derive(Args, Debug, Clone)]
pub struct AuthLoginArgs {
    /// Bluesky handle (e.g. user.bsky.social)
    #[arg(long)]
    pub handle: Option<String>,
    /// App password; switches login to the app-password flow
    #[arg(long, conflicts_with = "oauth")]
    pub password: Option<String>,
    /// PDS URL for app-password login (default: https://bsky.social)
    #[arg(long)]
    pub pds: Option<String>,
    /// Prompt for an app password instead of using OAuth
    #[arg(long, conflicts_with = "oauth")]
    pub app_password: bool,
    /// Log in via OAuth in the browser (default)
    #[arg(long)]
    pub oauth: bool,
}

#[derive(Subcommand, Debug, Clone)]
pub enum RepoCommand {
    /// List repositories
    List(RepoListArgs),
    /// Create repository
    Create(RepoCreateArgs),
    /// Clone repository
    Clone(RepoCloneArgs),
    /// Show repository information
    Info(RepoInfoArgs),
    /// Delete a repository
    Delete(RepoDeleteArgs),
    /// Star a repository
    Star(RepoRefArgs),
    /// Unstar a repository
    Unstar(RepoRefArgs),
}

#[derive(Args, Debug, Clone)]
pub struct RepoListArgs {
    #[arg(long)]
    pub knot: Option<String>,
    #[arg(long)]
    pub user: Option<String>,
    #[arg(long, default_value_t = false)]
    pub starred: bool,
    /// Tangled API base URL (overrides env)
    #[arg(long)]
    pub base: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct RepoCreateArgs {
    pub name: Option<String>,
    #[arg(long)]
    pub knot: Option<String>,
    #[arg(long, default_value_t = false)]
    pub private: bool,
    #[arg(long)]
    pub description: Option<String>,
    #[arg(long, default_value_t = false)]
    pub init: bool,
}

#[derive(Args, Debug, Clone)]
pub struct RepoCloneArgs {
    pub repo: String,
    #[arg(long, default_value_t = false)]
    pub https: bool,
    #[arg(long)]
    pub depth: Option<usize>,
}

#[derive(Args, Debug, Clone)]
pub struct RepoInfoArgs {
    pub repo: String,
    #[arg(long, default_value_t = false)]
    pub stats: bool,
    #[arg(long, default_value_t = false)]
    pub contributors: bool,
}

#[derive(Args, Debug, Clone)]
pub struct RepoDeleteArgs {
    pub repo: String,
    #[arg(long, default_value_t = false)]
    pub force: bool,
}

#[derive(Args, Debug, Clone)]
pub struct RepoRefArgs {
    pub repo: String,
}

#[derive(Subcommand, Debug, Clone)]
pub enum IssueCommand {
    List(IssueListArgs),
    Create(IssueCreateArgs),
    Show(IssueShowArgs),
    Edit(IssueEditArgs),
    Comment(IssueCommentArgs),
}

#[derive(Args, Debug, Clone)]
pub struct IssueListArgs {
    #[arg(long)]
    pub repo: Option<String>,
    #[arg(long)]
    pub state: Option<String>,
    #[arg(long)]
    pub author: Option<String>,
    #[arg(long)]
    pub label: Option<String>,
    #[arg(long)]
    pub assigned: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct IssueCreateArgs {
    #[arg(long)]
    pub repo: Option<String>,
    #[arg(long)]
    pub title: Option<String>,
    #[arg(long)]
    pub body: Option<String>,
    #[arg(long)]
    pub label: Option<Vec<String>>,
    #[arg(long, value_name = "HANDLE")]
    pub assign: Option<Vec<String>>,
}

#[derive(Args, Debug, Clone)]
pub struct IssueShowArgs {
    pub id: String,
    #[arg(long, default_value_t = false)]
    pub comments: bool,
    #[arg(long, default_value_t = false)]
    pub json: bool,
}

#[derive(Args, Debug, Clone)]
pub struct IssueEditArgs {
    pub id: String,
    #[arg(long)]
    pub title: Option<String>,
    #[arg(long)]
    pub body: Option<String>,
    #[arg(long)]
    pub state: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct IssueCommentArgs {
    pub id: String,
    #[arg(long)]
    pub body: Option<String>,
    #[arg(long, default_value_t = false)]
    pub close: bool,
}

#[derive(Subcommand, Debug, Clone)]
pub enum PullCommand {
    List(PullListArgs),
    Create(PullCreateArgs),
    Show(PullShowArgs),
    Review(PullReviewArgs),
    Merge(PullMergeArgs),
}

#[derive(Args, Debug, Clone)]
pub struct PullListArgs {
    #[arg(long)]
    pub repo: Option<String>,
    #[arg(long)]
    pub state: Option<String>,
    #[arg(long)]
    pub author: Option<String>,
    #[arg(long)]
    pub reviewer: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct PullCreateArgs {
    #[arg(long)]
    pub repo: Option<String>,
    #[arg(long)]
    pub target: Option<String>,
    #[arg(long)]
    pub base: Option<String>,
    #[arg(long)]
    pub head: Option<String>,
    #[arg(long)]
    pub title: Option<String>,
    #[arg(long)]
    pub body: Option<String>,
    #[arg(long = "body-file", short = 'F')]
    pub body_file: Option<String>,
    #[arg(long, short = 'T')]
    pub template: Option<String>,
    #[arg(long, short = 'e', default_value_t = false)]
    pub editor: bool,
    #[arg(long = "no-template", default_value_t = false)]
    pub no_template: bool,
    #[arg(long, default_value_t = false)]
    pub draft: bool,
}

#[derive(Args, Debug, Clone)]
pub struct PullShowArgs {
    pub id: String,
    #[arg(long, default_value_t = false)]
    pub diff: bool,
    #[arg(long, default_value_t = false)]
    pub comments: bool,
    #[arg(long, default_value_t = false)]
    pub checks: bool,
}

#[derive(Args, Debug, Clone)]
pub struct PullReviewArgs {
    pub id: String,
    #[arg(long, default_value_t = false)]
    pub approve: bool,
    #[arg(long, default_value_t = false)]
    pub request_changes: bool,
    #[arg(long)]
    pub comment: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct PullMergeArgs {
    pub id: String,
}

#[derive(Subcommand, Debug, Clone)]
pub enum KnotCommand {
    /// Migrate a repository to another knot
    Migrate(KnotMigrateArgs),
}

#[derive(Args, Debug, Clone)]
pub struct KnotMigrateArgs {
    /// Repo to migrate: <owner>/<name> (owner defaults to your handle)
    #[arg(long)]
    pub repo: String,
    /// Target knot hostname (e.g. knot1.tangled.sh)
    #[arg(long, value_name = "HOST")]
    pub to: String,
    /// Use HTTPS source when seeding new repo
    #[arg(long, default_value_t = true)]
    pub https: bool,
    /// Update PDS record knot field after seeding
    #[arg(long, default_value_t = true)]
    pub update_record: bool,
}

#[derive(Subcommand, Debug, Clone)]
pub enum SpindleCommand {
    List(SpindleListArgs),
    Config(SpindleConfigArgs),
    Run(SpindleRunArgs),
    Logs(SpindleLogsArgs),
    /// Secrets management
    #[command(subcommand)]
    Secret(SpindleSecretCommand),
}

#[derive(Args, Debug, Clone)]
pub struct SpindleListArgs {
    #[arg(long)]
    pub repo: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct SpindleConfigArgs {
    #[arg(long)]
    pub repo: Option<String>,
    #[arg(long)]
    pub url: Option<String>,
    #[arg(long, default_value_t = false)]
    pub enable: bool,
    #[arg(long, default_value_t = false)]
    pub disable: bool,
}

#[derive(Args, Debug, Clone)]
pub struct SpindleRunArgs {
    #[arg(long)]
    pub repo: Option<String>,
    #[arg(long)]
    pub branch: Option<String>,
    #[arg(long, default_value_t = false)]
    pub wait: bool,
}

#[derive(Args, Debug, Clone)]
pub struct SpindleLogsArgs {
    pub job_id: String,
    #[arg(long, default_value_t = false)]
    pub follow: bool,
    #[arg(long)]
    pub lines: Option<usize>,
}

#[derive(Subcommand, Debug, Clone)]
pub enum SpindleSecretCommand {
    /// List secrets for a repo
    List(SpindleSecretListArgs),
    /// Add or update a secret
    Add(SpindleSecretAddArgs),
    /// Remove a secret
    Remove(SpindleSecretRemoveArgs),
}

#[derive(Args, Debug, Clone)]
pub struct SpindleSecretListArgs {
    /// Repo: <owner>/<name>
    #[arg(long)]
    pub repo: String,
}

#[derive(Args, Debug, Clone)]
pub struct SpindleSecretAddArgs {
    /// Repo: <owner>/<name>
    #[arg(long)]
    pub repo: String,
    /// Secret key
    #[arg(long)]
    pub key: String,
    /// Secret value (use '@filename' to read from file, '-' to read from stdin)
    #[arg(long)]
    pub value: String,
}

#[derive(Args, Debug, Clone)]
pub struct SpindleSecretRemoveArgs {
    /// Repo: <owner>/<name>
    #[arg(long)]
    pub repo: String,
    /// Secret key
    #[arg(long)]
    pub key: String,
}

#[cfg(test)]
mod tests {
    use clap::Parser;

    use super::*;

    #[test]
    fn parses_machine_readable_output_formats() {
        let json = Cli::try_parse_from([
            "tangled", "repo", "list", "--format", "json",
        ])
        .unwrap();
        let yaml = Cli::try_parse_from([
            "tangled", "--format", "yml", "issue", "list",
        ])
        .unwrap();

        assert_eq!(json.format, OutputFormat::Json);
        assert_eq!(yaml.format, OutputFormat::Yaml);
    }

    #[test]
    fn rejects_api_command_tree() {
        assert!(
            Cli::try_parse_from(["tangled", "api", "auth", "status"]).is_err()
        );
    }

    #[test]
    fn parses_convenience_pull_create_without_repo_base_or_head() {
        let cli = Cli::try_parse_from(["tangled", "pull", "create"]).unwrap();
        assert!(matches!(cli.command, Command::Pull(PullCommand::Create(_))));
    }

    #[test]
    fn parses_pr_alias_for_pull_create() {
        let cli = Cli::try_parse_from(["tangled", "pr", "create"]).unwrap();
        assert!(matches!(cli.command, Command::Pull(PullCommand::Create(_))));
    }

    #[test]
    fn parses_interactive_repo_create_without_name() {
        let cli = Cli::try_parse_from(["tangled", "repo", "create"]).unwrap();
        let Command::Repo(RepoCommand::Create(args)) = cli.command else {
            panic!("expected repo create");
        };
        assert!(args.name.is_none());
    }

    #[test]
    fn parses_pull_create_target_and_body_editor_flags() {
        let cli = Cli::try_parse_from([
            "tangled",
            "pull",
            "create",
            "--target",
            "source",
            "--template",
            "feature",
            "--body-file",
            "-",
            "--editor",
        ])
        .unwrap();
        let Command::Pull(PullCommand::Create(args)) = cli.command else {
            panic!("expected pull create");
        };
        assert_eq!(args.target.as_deref(), Some("source"));
        assert_eq!(args.template.as_deref(), Some("feature"));
        assert_eq!(args.body_file.as_deref(), Some("-"));
        assert!(args.editor);
    }

    #[test]
    fn parses_convenience_issue_create_without_repo_or_title() {
        let cli = Cli::try_parse_from(["tangled", "issue", "create"]).unwrap();
        assert!(matches!(
            cli.command,
            Command::Issue(IssueCommand::Create(_))
        ));
    }
}