linear-cli 0.3.22

A powerful CLI for Linear.app - manage issues, projects, cycles, and more from your terminal
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use dialoguer::{Input, Select};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use tabled::{Table, Tabled};

use crate::api::{resolve_team_id, LinearClient};
use crate::display_options;
use crate::output::{
    ensure_non_empty, filter_values, print_json, print_json_owned, sort_values, OutputOptions,
};
use crate::priority::priority_to_string;
use crate::text::truncate;

fn safe_terminal_value(value: &str) -> String {
    crate::text::sanitize_terminal_text(value)
}
/// Issue template structure for creating issues with predefined values
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct IssueTemplate {
    /// Template name (used as identifier)
    pub name: String,
    /// Optional prefix to add to issue titles
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title_prefix: Option<String>,
    /// Default description for the issue
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Default priority (0=none, 1=urgent, 2=high, 3=normal, 4=low)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_priority: Option<i32>,
    /// Default labels to apply
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub default_labels: Vec<String>,
    /// Default team name or ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub team: Option<String>,
}

/// Storage for all templates
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct TemplateStore {
    pub templates: HashMap<String, IssueTemplate>,
}

#[derive(Subcommand)]
pub enum TemplateCommands {
    /// List available templates
    #[command(alias = "ls")]
    List,
    /// Create a new template interactively
    Create {
        /// Template name
        name: String,
    },
    /// Show template details
    #[command(alias = "get")]
    Show {
        /// Template name
        name: String,
    },
    /// Delete a template
    #[command(alias = "rm")]
    Delete {
        /// Template name
        name: String,
        /// Skip confirmation
        #[arg(short, long)]
        force: bool,
    },
    /// List Linear workspace templates
    #[command(alias = "remote-ls")]
    RemoteList {
        /// Filter by type: issue, project, document
        #[arg(short = 'T', long = "type")]
        template_type: Option<String>,
    },
    /// Get a Linear workspace template
    RemoteGet {
        /// Template ID
        id: String,
    },
    /// Create a Linear workspace template
    RemoteCreate {
        /// Template name
        #[arg(short, long)]
        name: String,
        /// Template type: issue, project, document
        #[arg(short = 'T', long = "type")]
        template_type: String,
        /// Team key/name/ID (for issue templates)
        #[arg(short, long)]
        team: Option<String>,
        /// Template description
        #[arg(short, long)]
        description: Option<String>,
        /// Template data as JSON string
        #[arg(long)]
        data: Option<String>,
    },
    /// Update a Linear workspace template
    RemoteUpdate {
        /// Template ID
        id: String,
        /// New name
        #[arg(short, long)]
        name: Option<String>,
        /// New description
        #[arg(short, long)]
        description: Option<String>,
        /// New template data as JSON
        #[arg(long)]
        data: Option<String>,
    },
    /// Delete a Linear workspace template
    RemoteDelete {
        /// Template ID
        id: String,
        /// Skip confirmation
        #[arg(short, long)]
        force: bool,
    },
}

#[derive(Tabled)]
struct TemplateRow {
    #[tabled(rename = "Name")]
    name: String,
    #[tabled(rename = "Title Prefix")]
    title_prefix: String,
    #[tabled(rename = "Team")]
    team: String,
    #[tabled(rename = "Priority")]
    priority: String,
    #[tabled(rename = "Labels")]
    labels: String,
}

fn templates_path() -> Result<PathBuf> {
    let config_dir = dirs::config_dir()
        .context("Could not find config directory")?
        .join("linear-cli");

    fs::create_dir_all(&config_dir)?;
    Ok(config_dir.join("templates.json"))
}

pub fn load_templates() -> Result<TemplateStore> {
    let path = templates_path()?;
    if path.exists() {
        let content = fs::read_to_string(&path)?;
        let store: TemplateStore = serde_json::from_str(&content)?;
        Ok(store)
    } else {
        Ok(TemplateStore::default())
    }
}

fn save_templates(store: &TemplateStore) -> Result<()> {
    let path = templates_path()?;
    let content = serde_json::to_string_pretty(store)?;
    fs::write(path, content)?;
    Ok(())
}

pub fn get_template(name: &str) -> Result<Option<IssueTemplate>> {
    let store = load_templates()?;
    Ok(store.templates.get(name).cloned())
}

pub async fn handle(cmd: TemplateCommands, output: &OutputOptions) -> Result<()> {
    match cmd {
        TemplateCommands::List => list_templates(output),
        TemplateCommands::Create { name } => create_template(&name, output),
        TemplateCommands::Show { name } => show_template(&name, output),
        TemplateCommands::Delete { name, force } => delete_template(&name, force, output),
        TemplateCommands::RemoteList { template_type } => {
            remote_list_templates(template_type.as_deref(), output).await
        }
        TemplateCommands::RemoteGet { id } => remote_get_template(&id, output).await,
        TemplateCommands::RemoteCreate {
            name,
            template_type,
            team,
            description,
            data,
        } => remote_create_template(&name, &template_type, team, description, data, output).await,
        TemplateCommands::RemoteUpdate {
            id,
            name,
            description,
            data,
        } => remote_update_template(&id, name, description, data, output).await,
        TemplateCommands::RemoteDelete { id, force } => {
            remote_delete_template(&id, force, output).await
        }
    }
}

fn list_templates(output: &OutputOptions) -> Result<()> {
    let store = load_templates()?;

    if store.templates.is_empty() {
        ensure_non_empty(&[], output)?;
        println!("No templates found.");
        println!("\nCreate one with: linear-cli templates create <name>");
        return Ok(());
    }

    let mut templates: Vec<serde_json::Value> =
        store.templates.values().map(|t| json!(t)).collect();

    filter_values(&mut templates, &output.filters);

    if let Some(sort_key) = output.json.sort.as_deref() {
        sort_values(&mut templates, sort_key, output.json.order);
    } else {
        templates.sort_by(|a, b| {
            a["name"]
                .as_str()
                .unwrap_or("")
                .cmp(b["name"].as_str().unwrap_or(""))
        });
    }

    if output.is_json() || output.has_template() {
        print_json_owned(serde_json::json!(templates), output)?;
        return Ok(());
    }

    ensure_non_empty(&templates, output)?;
    if templates.is_empty() {
        println!("No templates found.");
        return Ok(());
    }

    let width = display_options().max_width(30);
    let rows: Vec<TemplateRow> = templates
        .iter()
        .map(|t| TemplateRow {
            name: truncate(
                safe_terminal_value(t["name"].as_str().unwrap_or("")).as_str(),
                width,
            ),
            title_prefix: truncate(
                safe_terminal_value(t["title_prefix"].as_str().unwrap_or("-")).as_str(),
                width,
            ),
            team: truncate(
                safe_terminal_value(t["team"].as_str().unwrap_or("-")).as_str(),
                width,
            ),
            priority: priority_to_string(t["default_priority"].as_i64()),
            labels: {
                let labels = t["default_labels"].as_array().cloned().unwrap_or_default();
                if labels.is_empty() {
                    "-".to_string()
                } else {
                    let joined = labels
                        .iter()
                        .filter_map(|v| v.as_str())
                        .collect::<Vec<_>>()
                        .join(", ");
                    truncate(
                        safe_terminal_value(&joined).as_str(),
                        display_options().max_width(40),
                    )
                }
            },
        })
        .collect();

    let table = Table::new(rows).to_string();
    println!("{}", table);
    println!("\n{} templates", store.templates.len());

    Ok(())
}

fn create_template(name: &str, output: &OutputOptions) -> Result<()> {
    let mut store = load_templates()?;

    if store.templates.contains_key(name) {
        anyhow::bail!("Template already exists. Delete it first or choose a different name.");
    }

    println!("{} Creating template: {}", "+".green(), name.cyan());
    println!("Press Enter to skip optional fields.\n");

    let title_prefix: String = Input::new()
        .with_prompt("Title prefix (e.g., [Bug], [Feature])")
        .allow_empty(true)
        .interact_text()?;

    let title_prefix = if title_prefix.is_empty() {
        None
    } else {
        Some(title_prefix)
    };

    let description: String = Input::new()
        .with_prompt("Default description")
        .allow_empty(true)
        .interact_text()?;

    let description = if description.is_empty() {
        None
    } else {
        Some(description)
    };

    let priority_options = vec!["None", "Urgent (1)", "High (2)", "Normal (3)", "Low (4)"];
    let priority_selection = Select::new()
        .with_prompt("Default priority")
        .items(&priority_options)
        .default(0)
        .interact()?;

    let default_priority = match priority_selection {
        0 => None,
        1 => Some(1),
        2 => Some(2),
        3 => Some(3),
        4 => Some(4),
        _ => None,
    };

    let team: String = Input::new()
        .with_prompt("Default team (name or key)")
        .allow_empty(true)
        .interact_text()?;

    let team = if team.is_empty() { None } else { Some(team) };

    let labels_input: String = Input::new()
        .with_prompt("Default labels (comma-separated)")
        .allow_empty(true)
        .interact_text()?;

    let default_labels: Vec<String> = if labels_input.is_empty() {
        vec![]
    } else {
        labels_input
            .split(',')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect()
    };

    let template = IssueTemplate {
        name: name.to_string(),
        title_prefix,
        description,
        default_priority,
        default_labels,
        team,
    };

    store.templates.insert(name.to_string(), template);
    save_templates(&store)?;

    if output.is_json() || output.has_template() {
        print_json_owned(json!(store.templates.get(name)), output)?;
        return Ok(());
    }

    println!("\n{} Template created successfully!", "+".green());

    Ok(())
}

fn show_template(name: &str, output: &OutputOptions) -> Result<()> {
    let store = load_templates()?;

    let template = store
        .templates
        .get(name)
        .ok_or_else(|| anyhow::anyhow!("Template not found"))?;

    if output.is_json() || output.has_template() {
        print_json_owned(json!(template), output)?;
        return Ok(());
    }

    println!(
        "{} {}",
        "Template:".bold(),
        safe_terminal_value(&template.name).cyan().bold()
    );
    println!("{}", "-".repeat(40));

    println!(
        "Title Prefix: {}",
        template
            .title_prefix
            .as_deref()
            .map(safe_terminal_value)
            .unwrap_or_else(|| "-".to_string())
    );

    if let Some(desc) = &template.description {
        println!("Description:  {}", safe_terminal_value(desc));
    } else {
        println!("Description:  -");
    }

    println!(
        "Priority:     {}",
        priority_to_string(template.default_priority.map(|p| p as i64))
    );

    println!(
        "Team:         {}",
        template
            .team
            .as_deref()
            .map(safe_terminal_value)
            .unwrap_or_else(|| "-".to_string())
    );

    if template.default_labels.is_empty() {
        println!("Labels:       -");
    } else {
        println!(
            "Labels:       {}",
            safe_terminal_value(&template.default_labels.join(", "))
        );
    }

    Ok(())
}

fn delete_template(name: &str, force: bool, output: &OutputOptions) -> Result<()> {
    let mut store = load_templates()?;

    if !store.templates.contains_key(name) {
        anyhow::bail!("Template not found");
    }

    if !force && !crate::is_yes() {
        anyhow::bail!(
            "Delete requires --force flag. Use: linear templates delete {} --force",
            name
        );
    }

    store.templates.remove(name);
    save_templates(&store)?;

    if output.is_json() || output.has_template() {
        print_json_owned(json!({ "deleted": name }), output)?;
        return Ok(());
    }

    println!("{} Template deleted", "+".green());

    Ok(())
}

// --- Remote (Linear workspace) templates ---

#[derive(Tabled)]
struct RemoteTemplateRow {
    #[tabled(rename = "Name")]
    name: String,
    #[tabled(rename = "Type")]
    template_type: String,
    #[tabled(rename = "Team")]
    team: String,
    #[tabled(rename = "Created")]
    created: String,
    #[tabled(rename = "ID")]
    id: String,
}

async fn remote_list_templates(template_type: Option<&str>, output: &OutputOptions) -> Result<()> {
    let client = LinearClient::new()?;

    let query = r#"
        query {
            templates {
                nodes {
                    id
                    name
                    type
                    description
                    team { name }
                    createdAt
                }
            }
        }
    "#;

    let result = client.query(query, None).await?;
    let nodes = result["data"]["templates"]["nodes"]
        .as_array()
        .cloned()
        .unwrap_or_default();

    // Client-side filter by type
    let mut filtered: Vec<serde_json::Value> = if let Some(t) = template_type {
        let t_lower = t.to_lowercase();
        nodes
            .into_iter()
            .filter(|n| {
                n["type"]
                    .as_str()
                    .map(|v| v.to_lowercase() == t_lower)
                    .unwrap_or(false)
            })
            .collect()
    } else {
        nodes
    };

    if output.is_json() || output.has_template() {
        print_json_owned(json!(filtered), output)?;
        return Ok(());
    }

    filter_values(&mut filtered, &output.filters);

    if let Some(sort_key) = output.json.sort.as_deref() {
        sort_values(&mut filtered, sort_key, output.json.order);
    }

    ensure_non_empty(&filtered, output)?;
    if filtered.is_empty() {
        println!("No workspace templates found.");
        return Ok(());
    }

    let width = display_options().max_width(30);
    let rows: Vec<RemoteTemplateRow> = filtered
        .iter()
        .map(|t| RemoteTemplateRow {
            name: truncate(
                safe_terminal_value(t["name"].as_str().unwrap_or("")).as_str(),
                width,
            ),
            template_type: t["type"].as_str().unwrap_or("-").to_string(),
            team: truncate(
                safe_terminal_value(t["team"]["name"].as_str().unwrap_or("-")).as_str(),
                display_options().max_width(20),
            ),
            created: t["createdAt"]
                .as_str()
                .map(|s| s.get(..10).unwrap_or(s).to_string())
                .unwrap_or_else(|| "-".to_string()),
            id: t["id"].as_str().unwrap_or("").to_string(),
        })
        .collect();

    let rows_len = rows.len();
    let table = Table::new(rows).to_string();
    println!("{}", table);
    println!("\n{} workspace templates", rows_len);

    Ok(())
}

async fn remote_get_template(id: &str, output: &OutputOptions) -> Result<()> {
    let client = LinearClient::new()?;

    let query = r#"
        query($id: String!) {
            template(id: $id) {
                id
                name
                type
                description
                templateData
                team { name key }
                creator { name }
                createdAt
                updatedAt
            }
        }
    "#;

    let result = client.query(query, Some(json!({ "id": id }))).await?;
    let raw = &result["data"]["template"];

    if raw.is_null() {
        anyhow::bail!("Template not found: {}", id);
    }

    if output.is_json() || output.has_template() {
        print_json(raw, output)?;
        return Ok(());
    }

    println!(
        "{} {}",
        "Template:".bold(),
        safe_terminal_value(raw["name"].as_str().unwrap_or(""))
            .cyan()
            .bold()
    );
    println!("{}", "-".repeat(40));
    println!(
        "Type: {}",
        safe_terminal_value(raw["type"].as_str().unwrap_or("-"))
    );
    if let Some(desc) = raw["description"].as_str() {
        if !desc.is_empty() {
            println!("Description: {}", safe_terminal_value(desc));
        }
    }
    if let Some(team_name) = raw["team"]["name"].as_str() {
        let team_name = safe_terminal_value(team_name);
        let team_key = safe_terminal_value(raw["team"]["key"].as_str().unwrap_or(""));
        println!("Team: {} ({})", team_name, team_key);
    }
    if let Some(creator) = raw["creator"]["name"].as_str() {
        println!("Creator: {}", safe_terminal_value(creator));
    }
    println!(
        "Created: {}",
        raw["createdAt"]
            .as_str()
            .map(|s| s.get(..10).unwrap_or(s))
            .unwrap_or("-")
    );
    println!("ID: {}", id);

    if !raw["templateData"].is_null() {
        println!(
            "\nTemplate Data:\n{}",
            serde_json::to_string_pretty(&raw["templateData"]).unwrap_or_default()
        );
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_safe_terminal_value_removes_escape_sequences() {
        assert_eq!(safe_terminal_value("bad\u{1b}[31mname\u{1b}[0m"), "badname");
    }
}

async fn remote_create_template(
    name: &str,
    template_type: &str,
    team: Option<String>,
    description: Option<String>,
    data: Option<String>,
    output: &OutputOptions,
) -> Result<()> {
    let client = LinearClient::new()?;

    let mut input = json!({
        "name": name,
        "type": template_type,
    });

    if let Some(t) = team {
        let team_id = resolve_team_id(&client, &t, &output.cache).await?;
        input["teamId"] = json!(team_id);
    }
    if let Some(d) = &description {
        input["description"] = json!(d);
    }
    if let Some(d) = &data {
        let parsed: serde_json::Value = serde_json::from_str(d)
            .context("Invalid JSON for --data. Provide valid JSON string.")?;
        input["templateData"] = parsed;
    }

    let mutation = r#"
        mutation($input: TemplateCreateInput!) {
            templateCreate(input: $input) {
                success
                template { id name }
            }
        }
    "#;

    let result = client
        .mutate(mutation, Some(json!({ "input": input })))
        .await?;

    if result["data"]["templateCreate"]["success"].as_bool() == Some(true) {
        let tmpl = &result["data"]["templateCreate"]["template"];
        if output.is_json() || output.has_template() {
            print_json(tmpl, output)?;
            return Ok(());
        }
        println!(
            "{} Template created: {}",
            "+".green(),
            safe_terminal_value(tmpl["name"].as_str().unwrap_or(""))
        );
        println!("  ID: {}", tmpl["id"].as_str().unwrap_or(""));
    } else {
        anyhow::bail!("Failed to create template");
    }

    Ok(())
}

async fn remote_update_template(
    id: &str,
    name: Option<String>,
    description: Option<String>,
    data: Option<String>,
    output: &OutputOptions,
) -> Result<()> {
    let client = LinearClient::new()?;

    let mut input = json!({});
    if let Some(n) = name {
        input["name"] = json!(n);
    }
    if let Some(d) = description {
        input["description"] = json!(d);
    }
    if let Some(d) = &data {
        let parsed: serde_json::Value = serde_json::from_str(d)
            .context("Invalid JSON for --data. Provide valid JSON string.")?;
        input["templateData"] = parsed;
    }

    if input.as_object().map(|o| o.is_empty()).unwrap_or(true) {
        println!("No updates specified.");
        return Ok(());
    }

    let mutation = r#"
        mutation($id: String!, $input: TemplateUpdateInput!) {
            templateUpdate(id: $id, input: $input) {
                success
                template { id name }
            }
        }
    "#;

    let result = client
        .mutate(mutation, Some(json!({ "id": id, "input": input })))
        .await?;

    if result["data"]["templateUpdate"]["success"].as_bool() == Some(true) {
        let tmpl = &result["data"]["templateUpdate"]["template"];
        if output.is_json() || output.has_template() {
            print_json(tmpl, output)?;
            return Ok(());
        }
        println!("{} Template updated", "+".green());
    } else {
        anyhow::bail!("Failed to update template");
    }

    Ok(())
}

async fn remote_delete_template(id: &str, force: bool, output: &OutputOptions) -> Result<()> {
    if !force && !crate::is_yes() {
        anyhow::bail!(
            "Delete requires --force flag. Use: linear templates remote-delete {} --force",
            id
        );
    }

    let client = LinearClient::new()?;

    let mutation = r#"
        mutation($id: String!) {
            templateDelete(id: $id) {
                success
            }
        }
    "#;

    let result = client.mutate(mutation, Some(json!({ "id": id }))).await?;

    let success = result["data"]["templateDelete"]["success"]
        .as_bool()
        .unwrap_or(false);

    if success {
        if output.is_json() || output.has_template() {
            print_json_owned(json!({ "deleted": id }), output)?;
            return Ok(());
        }
        println!("{} Workspace template deleted", "+".green());
    } else {
        anyhow::bail!("Failed to delete template {}", id);
    }

    Ok(())
}