linear-motion 0.2.0

A CLI tool for syncing between Linear and Motion
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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use clap::Parser;
use linear_motion::cli::commands::{Cli, Commands};
use linear_motion::{Error, Result};
use tracing::info;

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Initialize logging
    let subscriber = tracing_subscriber::FmtSubscriber::builder()
        .with_max_level(if cli.verbose {
            tracing::Level::DEBUG
        } else {
            tracing::Level::INFO
        })
        .finish();
    tracing::subscriber::set_global_default(subscriber)
        .map_err(|e| Error::Other(format!("Failed to set tracing subscriber: {}", e)))?;

    info!("Starting linear-motion");

    match cli.command {
        Commands::Init { output, force } => {
            handle_init(output.as_deref(), force).await?;
        }
        Commands::Sync {
            watch,
            pid_file,
            force,
        } => {
            handle_sync(cli.config.as_deref(), watch, &pid_file, force).await?;
        }
        Commands::Status => {
            handle_status().await?;
        }
        Commands::Stop => {
            handle_stop().await?;
        }
        Commands::Complete { id, current } => {
            let task_id = if current { None } else { id.as_deref() };
            handle_complete(cli.config.as_deref(), task_id).await?;
        }
        Commands::Tasks { waybar } => {
            handle_tasks(cli.config.as_deref(), waybar).await?;
        }
        Commands::List { verbose, source } => {
            handle_list(cli.config.as_deref(), verbose, source.as_deref()).await?;
        }
    }

    Ok(())
}

async fn handle_init(output: Option<&str>, force: bool) -> Result<()> {
    use linear_motion::config::{
        AppConfig, ConfigLoader, ScheduleOverride, SyncRules, SyncSource, TimeEstimateStrategy,
    };
    use std::collections::HashMap;
    use std::fs;
    use std::path::Path;

    let output_path = match output {
        Some(path) => Path::new(path).to_path_buf(),
        None => {
            ConfigLoader::ensure_config_dir()?;
            ConfigLoader::get_default_config_path()?
        }
    };

    if output_path.exists() && !force {
        return Err(Error::Other(format!(
            "Config file {} already exists. Use --force to overwrite.",
            output_path.display()
        )));
    }

    // Create Fibonacci mapping
    let mut fibonacci_mappings = HashMap::new();
    fibonacci_mappings.insert("1".to_string(), 30);
    fibonacci_mappings.insert("2".to_string(), 60);
    fibonacci_mappings.insert("3".to_string(), 120);
    fibonacci_mappings.insert("5".to_string(), 240);
    fibonacci_mappings.insert("8".to_string(), 480);

    // Create T-shirt mapping
    let mut tshirt_mappings = HashMap::new();
    tshirt_mappings.insert("XS".to_string(), 30);
    tshirt_mappings.insert("S".to_string(), 60);
    tshirt_mappings.insert("M".to_string(), 120);
    tshirt_mappings.insert("L".to_string(), 240);
    tshirt_mappings.insert("XL".to_string(), 480);

    let time_estimate_strategy = TimeEstimateStrategy {
        fibonacci: Some(fibonacci_mappings),
        tshirt: Some(tshirt_mappings),
        linear: None,
        points: None,
        default_duration_mins: Some(60),
    };

    let sync_rules = SyncRules {
        default_task_duration_mins: 60,
        completed_linear_tag: "motioned".to_string(),
        time_estimate_strategy: time_estimate_strategy.clone(),
    };

    let sync_source = SyncSource {
        name: "my-linear-workspace".to_string(),
        linear_api_key: "your_linear_api_key_here".to_string(),
        projects: Some(vec!["project-id-1".to_string(), "project-id-2".to_string()]),
        webhook_base_url: None,
        sync_rules: Some(sync_rules.clone()),
    };

    let schedule_override = ScheduleOverride {
        name: "work_hours".to_string(),
        interval_seconds: 60,
        start_time: "09:00".to_string(),
        end_time: "17:00".to_string(),
        days: vec![
            "mon".to_string(),
            "tue".to_string(),
            "wed".to_string(),
            "thu".to_string(),
            "fri".to_string(),
        ],
    };

    let template_config = AppConfig {
        motion_api_key: "your_motion_api_key_here".to_string(),
        sync_sources: vec![sync_source],
        global_sync_rules: sync_rules,
        database_path: None,
        polling_interval_seconds: 300,
        schedule_overrides: Some(vec![schedule_override]),
    };

    // Serialize to pretty JSON
    let config_json = serde_json::to_string_pretty(&template_config)?;

    fs::write(&output_path, config_json)?;
    info!(
        "Configuration template written to {}",
        output_path.display()
    );
    println!(
        "βœ… Configuration template created at {}",
        output_path.display()
    );
    println!("πŸ“ Please edit the file to add your API keys and configure sync sources.");

    Ok(())
}

async fn handle_sync(
    config_path: Option<&str>,
    watch: bool,
    pid_file: &str,
    force: bool,
) -> Result<()> {
    use linear_motion::config::ConfigLoader;
    use linear_motion::sync::orchestrator::SyncOrchestrator;
    use std::fs;

    let config_path = match config_path {
        Some(path) => path.to_string(),
        None => ConfigLoader::get_default_config_path()?
            .to_string_lossy()
            .to_string(),
    };

    // Load and validate configuration
    let config = ConfigLoader::load_from_file(&config_path).await?;
    info!(
        "Configuration loaded: {} sync sources configured",
        config.sync_sources.len()
    );

    if watch {
        info!("Starting daemon mode");

        // Write PID file
        let pid = std::process::id();
        fs::write(pid_file, pid.to_string())?;
        info!("Daemon PID {} written to {}", pid, pid_file);

        // TODO: Start daemon loop
        println!("πŸš€ Daemon started in watch mode (PID: {})", pid);
        println!("πŸ“„ PID file: {}", pid_file);
        println!("πŸ“Š {} sync sources configured", config.sync_sources.len());
        println!("πŸ’Ύ Database: {:?}", config.database_path());
        println!("⚠️  Daemon functionality not yet implemented");
    } else {
        tracing::debug!("running one-time sync...");

        // Initialize and run sync orchestrator
        let orchestrator = SyncOrchestrator::new(&config).await?;
        match orchestrator.run_full_sync(&config, force).await {
            Ok(()) => {
                println!("βœ… Sync completed successfully!");
            }
            Err(e) => {
                println!("❌ Sync failed: {}", e);
                return Err(e);
            }
        }
    }

    Ok(())
}

async fn handle_status() -> Result<()> {
    // TODO: Implement IPC client to query daemon
    println!("πŸ“Š Status functionality not yet implemented");
    println!("⚠️  Will query daemon via IPC when implemented");
    Ok(())
}

async fn handle_stop() -> Result<()> {
    // TODO: Implement daemon shutdown via IPC or signal
    println!("πŸ›‘ Stop functionality not yet implemented");
    println!("⚠️  Will send shutdown signal to daemon when implemented");
    Ok(())
}

fn current_task_state_path() -> std::path::PathBuf {
    std::env::var("XDG_RUNTIME_DIR")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| std::path::PathBuf::from("/tmp"))
        .join("linear-motion-current")
}

async fn handle_complete(config_path: Option<&str>, task_id: Option<&str>) -> Result<()> {
    use linear_motion::clients::motion::MotionClient;
    use linear_motion::config::ConfigLoader;

    let id = match task_id {
        Some(id) => id.to_string(),
        None => std::fs::read_to_string(current_task_state_path())
            .map(|s| s.trim().to_string())
            .map_err(|_| Error::Other("No task ID given and no current task state found. Run `tasks --waybar` first.".to_string()))?,
    };

    let config_path = match config_path {
        Some(path) => path.to_string(),
        None => ConfigLoader::get_default_config_path()?
            .to_string_lossy()
            .to_string(),
    };
    let config = ConfigLoader::load_from_file(&config_path).await?;
    let client = MotionClient::new(config.motion_api_key.clone())?;

    client.mark_task_completed(&id).await?;
    Ok(())
}

async fn handle_tasks(config_path: Option<&str>, waybar: bool) -> Result<()> {
    use chrono::Utc;
    use linear_motion::clients::motion::MotionClient;
    use linear_motion::config::ConfigLoader;

    let config_path = match config_path {
        Some(path) => path.to_string(),
        None => ConfigLoader::get_default_config_path()?
            .to_string_lossy()
            .to_string(),
    };
    let config = ConfigLoader::load_from_file(&config_path).await?;
    let client = MotionClient::new(config.motion_api_key.clone())?;

    // Find the preferred workspace (private workspace first)
    let workspaces = client.list_workspaces().await?;
    let workspace = workspaces
        .iter()
        .find(|w| w.workspace_type == "INDIVIDUAL")
        .or_else(|| workspaces.first())
        .ok_or_else(|| Error::Other("No Motion workspaces found".to_string()))?;

    let mut tasks = client.list_tasks(&workspace.id).await?;

    // Sort by scheduled start, tasks without a scheduled start go last
    tasks.sort_by(|a, b| {
        match (a.scheduled_start, b.scheduled_start) {
            (Some(a), Some(b)) => a.cmp(&b),
            (Some(_), None) => std::cmp::Ordering::Less,
            (None, Some(_)) => std::cmp::Ordering::Greater,
            (None, None) => std::cmp::Ordering::Equal,
        }
    });

    if waybar {
        let now = Utc::now();

        // Current task: in-progress (scheduledStart <= now < scheduledEnd), else next upcoming
        let current = tasks
            .iter()
            .find(|t| {
                matches!(
                    (t.scheduled_start, t.scheduled_end),
                    (Some(start), Some(end)) if start <= now && now < end
                )
            })
            .or_else(|| tasks.iter().find(|t| t.scheduled_start.map_or(false, |s| s > now)))
            .or_else(|| tasks.first());

        let next = current.and_then(|c| {
            tasks
                .iter()
                .skip_while(|t| t.id != c.id)
                .nth(1)
        });

        let text = current
            .map(|t| t.name.clone())
            .unwrap_or_else(|| "No tasks".to_string());

        let tooltip = next
            .map(|t| format!("Next: {}", t.name))
            .unwrap_or_else(|| "No upcoming tasks".to_string());

        let class = current
            .and_then(|t| t.priority.as_deref())
            .map(|p| p.to_lowercase())
            .unwrap_or_else(|| "none".to_string());

        let alt = current
            .and_then(|t| t.id.as_deref())
            .unwrap_or_default();

        // Write the current task ID to the state file so `complete` can use it
        // without re-fetching (avoids TOCTOU between display and click)
        let _ = std::fs::write(current_task_state_path(), alt);

        println!(
            "{}",
            serde_json::json!({
                "text": text,
                "alt": alt,
                "tooltip": tooltip,
                "class": class,
            })
        );
    } else {
        println!("{}", serde_json::to_string_pretty(&tasks)?);
    }

    Ok(())
}

async fn handle_list(
    config_path: Option<&str>,
    verbose: bool,
    source_filter: Option<&str>,
) -> Result<()> {
    use linear_motion::config::ConfigLoader;
    use linear_motion::db::SyncDatabase;

    let config_path = match config_path {
        Some(path) => path.to_string(),
        None => ConfigLoader::get_default_config_path()?
            .to_string_lossy()
            .to_string(),
    };

    // Load configuration to get database path
    let config = ConfigLoader::load_from_file(&config_path).await?;

    // Initialize database
    let database = SyncDatabase::new(config.database_path()).await?;

    println!("πŸ“Š Linear-Motion Database Contents");
    println!("πŸ’Ύ Database: {}", config.database_path().display());
    println!();

    // Get and display task mappings
    let mappings = match source_filter {
        Some(source) => database.mappings.list_mappings_by_source(source).await?,
        None => database.mappings.list_all_mappings().await?,
    };

    if mappings.is_empty() {
        println!("πŸ“­ No task mappings found");
        if let Some(source) = source_filter {
            println!("   (filtered by source: {})", source);
        }
    } else {
        println!("πŸ”— Task Mappings ({} total)", mappings.len());
        if let Some(source) = source_filter {
            println!("   (filtered by source: {})", source);
        }
        println!();

        for mapping in &mappings {
            // Parse Linear issue data from the mapping
            let issue_title = if let Ok(issue) = serde_json::from_value::<
                linear_motion::clients::linear::LinearIssue,
            >(mapping.linear_issue_data.clone())
            {
                format!("{} - {}", issue.identifier, issue.title)
            } else {
                mapping.linear_issue_id.clone()
            };

            let status_icon = match mapping.status {
                linear_motion::db::MappingStatus::Pending => "⏳",
                linear_motion::db::MappingStatus::Synced => "βœ…",
                linear_motion::db::MappingStatus::Failed => "❌",
                linear_motion::db::MappingStatus::Stale => "πŸ”„",
            };

            let status_str = match mapping.status {
                linear_motion::db::MappingStatus::Pending => "Pending",
                linear_motion::db::MappingStatus::Synced => "Synced",
                linear_motion::db::MappingStatus::Failed => "Failed",
                linear_motion::db::MappingStatus::Stale => "Stale",
            };

            println!("  {} {}", status_icon, issue_title);
            println!("    Source: {}", mapping.sync_source);
            println!("    Status: {}", status_str);
            if let Some(motion_id) = &mapping.motion_task_id {
                println!("    Motion Task: {}", motion_id);
            }
            println!(
                "    Created: {}",
                mapping.created_at.format("%Y-%m-%d %H:%M:%S UTC")
            );
            println!(
                "    Updated: {}",
                mapping.updated_at.format("%Y-%m-%d %H:%M:%S UTC")
            );

            if verbose {
                println!("    Linear ID: {}", mapping.linear_issue_id);
                if let Some(attempt) = &mapping.last_sync_attempt {
                    println!("    Last Sync: {}", attempt.format("%Y-%m-%d %H:%M:%S UTC"));
                }
                if let Some(error) = &mapping.sync_error {
                    println!("    Sync Error: {}", error);
                }

                // Show issue details if we have them
                if let Ok(issue) = serde_json::from_value::<
                    linear_motion::clients::linear::LinearIssue,
                >(mapping.linear_issue_data.clone())
                {
                    if let Some(desc) = &issue.description {
                        let truncated_desc = if desc.len() > 100 {
                            format!("{}...", &desc[..100])
                        } else {
                            desc.clone()
                        };
                        println!("    Description: {}", truncated_desc);
                    }
                    println!(
                        "    State: {} ({})",
                        issue.state.name, issue.state.state_type
                    );
                    println!("    Team: {} ({})", issue.team.name, issue.team.key);
                    if let Some(due_date) = &issue.due_date {
                        println!("    Due Date: {}", due_date);
                    }
                }
            }
            println!();
        }
    }

    // Get and display sync status entries
    let status_entries = match source_filter {
        Some(source) => database.status.list_statuses_by_source(source).await?,
        None => {
            // Get all failed entries as an example - in a real implementation,
            // we might want to add a list_all_entries method
            let mut all_entries = Vec::new();
            let source_stats = database.status.list_all_source_stats().await?;
            for source_stat in source_stats {
                let source_entries = database
                    .status
                    .list_statuses_by_source(&source_stat.source_name)
                    .await?;
                all_entries.extend(source_entries);
            }
            all_entries
        }
    };

    if status_entries.is_empty() {
        println!("πŸ“­ No sync status entries found");
    } else {
        println!("πŸ“ˆ Sync Status Entries ({} total)", status_entries.len());
        println!();

        let mut by_status = std::collections::HashMap::new();
        for entry in &status_entries {
            let status_str = match entry.status {
                linear_motion::db::status::SyncStatus::InProgress => "InProgress",
                linear_motion::db::status::SyncStatus::Completed => "Completed",
                linear_motion::db::status::SyncStatus::Failed => "Failed",
                linear_motion::db::status::SyncStatus::Paused => "Paused",
            };
            *by_status.entry(status_str).or_insert(0) += 1;
        }

        for (status, count) in &by_status {
            let icon = match *status {
                "InProgress" => "πŸ”„",
                "Completed" => "βœ…",
                "Failed" => "❌",
                "Paused" => "⏸️",
                _ => "❓",
            };
            println!("  {} {}: {}", icon, status, count);
        }
        println!();

        if verbose {
            println!("πŸ“ Detailed Status Entries:");
            for entry in status_entries.iter().take(10) {
                // Limit to first 10 for readability
                let (icon, status_str) = match entry.status {
                    linear_motion::db::status::SyncStatus::InProgress => ("πŸ”„", "InProgress"),
                    linear_motion::db::status::SyncStatus::Completed => ("βœ…", "Completed"),
                    linear_motion::db::status::SyncStatus::Failed => ("❌", "Failed"),
                    linear_motion::db::status::SyncStatus::Paused => ("⏸️", "Paused"),
                };

                println!(
                    "  {} {} - {}",
                    icon, entry.sync_source, entry.linear_issue_id
                );
                println!("     Status: {}", status_str);
                println!(
                    "     Created: {}",
                    entry.created_at.format("%Y-%m-%d %H:%M:%S UTC")
                );
                println!(
                    "     Last Sync: {}",
                    entry.last_sync_attempt.format("%Y-%m-%d %H:%M:%S UTC")
                );
                if let Some(error) = &entry.error_message {
                    println!("     Error: {}", error);
                }
                if let Some(motion_id) = &entry.motion_task_id {
                    println!("     Motion Task: {}", motion_id);
                }
                println!("     Retry Count: {}", entry.retry_count);
                if verbose {
                    println!("     Entry ID: {}", entry.id);
                }
                println!();
            }

            if status_entries.len() > 10 {
                println!(
                    "  ... and {} more entries (use --source filter to see specific source)",
                    status_entries.len() - 10
                );
                println!();
            }
        }
    }

    // Display source statistics
    let source_stats = database.status.list_all_source_stats().await?;
    if !source_stats.is_empty() {
        println!("πŸ“Š Source Statistics:");
        for stat in &source_stats {
            println!("  β€’ {}", stat.source_name);
            println!(
                "    Last sync: {}",
                if let Some(last_sync) = &stat.last_sync {
                    last_sync.format("%Y-%m-%d %H:%M:%S UTC").to_string()
                } else {
                    "Never".to_string()
                }
            );
            println!("    Total processed: {}", stat.total_issues_processed);
            println!("    Successful syncs: {}", stat.successful_syncs);
            println!("    Failed syncs: {}", stat.failed_syncs);
            if !stat.errors.is_empty() {
                println!("    Recent errors: {}", stat.errors.len());
                if verbose {
                    for error in stat.errors.iter().take(3) {
                        println!("      - {}", error);
                    }
                }
            }
            println!();
        }
    }

    Ok(())
}