raftcli 1.18.1

Command line interface for raft framework and serial monitoring
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
// RaftCLI: Local Raft library management
// Rob Dobson 2024

use clap::{Parser, Subcommand};
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use crate::raft_cli_utils::check_app_folder_valid;

const DEFAULT_DEST_FOLDER: &str = "raftdevlibs";
const DEFAULT_LIBS: [&str; 4] = ["RaftCore", "RaftSysMods", "RaftI2C", "RaftWebServer"];

#[derive(Clone, Parser, Debug)]
pub struct LibsCmd {
    #[clap(subcommand)]
    pub command: Option<LibsSubcommand>,
}

#[derive(Clone, Subcommand, Debug)]
pub enum LibsSubcommand {
    /// Clone or update Raft libraries into raftdevlibs (default action)
    #[clap(alias = "f")]
    Fetch(LibsFetchCmd),
    /// Show the git status of each library in raftdevlibs
    #[clap(alias = "st")]
    Status(LibsStatusCmd),
    /// Fast-forward pull each library in raftdevlibs from its upstream
    #[clap(alias = "p")]
    Pull(LibsPullCmd),
}

#[derive(Clone, Parser, Debug)]
pub struct LibsFetchCmd {
    #[clap(
        help = "Path to the application folder",
        value_name = "APPLICATION_FOLDER"
    )]
    pub app_folder: Option<String>,
    #[clap(
        long,
        default_value = "robdobsn",
        help = "GitHub account or organisation"
    )]
    pub account: String,
    #[clap(long, num_args = 1.., value_name = "LIB", help = "Libraries to fetch")]
    pub libs: Vec<String>,
    #[clap(
        long,
        default_value = "main",
        help = "Git branch, tag or commit to checkout"
    )]
    pub branch: String,
    #[clap(
        long,
        value_name = "DEST_DIR",
        help = "Destination directory (default: <app-folder>/raftdevlibs)"
    )]
    pub dest: Option<String>,
    #[clap(
        long,
        help = "Update existing repositories even when they have uncommitted changes"
    )]
    pub force: bool,
}

#[derive(Clone, Parser, Debug)]
pub struct LibsStatusCmd {
    #[clap(
        help = "Path to the application folder",
        value_name = "APPLICATION_FOLDER"
    )]
    pub app_folder: Option<String>,
    #[clap(
        long,
        value_name = "DEST_DIR",
        help = "Library directory to inspect (default: <app-folder>/raftdevlibs)"
    )]
    pub dest: Option<String>,
    #[clap(
        long,
        help = "Run 'git fetch' first so ahead/behind counts reflect the remote"
    )]
    pub fetch: bool,
}

#[derive(Clone, Parser, Debug)]
pub struct LibsPullCmd {
    #[clap(
        help = "Path to the application folder",
        value_name = "APPLICATION_FOLDER"
    )]
    pub app_folder: Option<String>,
    #[clap(
        long,
        value_name = "DEST_DIR",
        help = "Library directory to inspect (default: <app-folder>/raftdevlibs)"
    )]
    pub dest: Option<String>,
}

pub fn run_libs(cmd: &LibsCmd) -> Result<(), Box<dyn Error>> {
    match &cmd.command {
        Some(LibsSubcommand::Fetch(fetch)) => fetch_raft_libs(fetch),
        Some(LibsSubcommand::Status(status)) => status_raft_libs(status),
        Some(LibsSubcommand::Pull(pull)) => pull_raft_libs(pull),
        None => fetch_raft_libs(&LibsFetchCmd {
            app_folder: None,
            account: "robdobsn".to_string(),
            libs: Vec::new(),
            branch: "main".to_string(),
            dest: None,
            force: false,
        }),
    }
}

pub fn fetch_raft_libs(cmd: &LibsFetchCmd) -> Result<(), Box<dyn Error>> {
    let app_folder = cmd.app_folder.clone().unwrap_or(".".to_string());
    if !check_app_folder_valid(app_folder.clone()) {
        return Err("app folder is not a valid Raft project root".into());
    }

    let dest_dir = resolve_dest_dir(&app_folder, &cmd.dest);
    prepare_dest_dir(&dest_dir)?;

    let libs = if cmd.libs.is_empty() {
        DEFAULT_LIBS.iter().map(|lib| lib.to_string()).collect()
    } else {
        cmd.libs.clone()
    };

    let mut failures = Vec::new();
    for lib in libs {
        if let Err(err) = fetch_lib(&cmd.account, &lib, &dest_dir, &cmd.branch, cmd.force) {
            eprintln!("  ERROR fetching {}: {}", lib, err);
            failures.push(lib);
        }
    }

    if !failures.is_empty() {
        return Err(format!("Failed to fetch: {}", failures.join(", ")).into());
    }

    println!("\nAll libraries fetched successfully.");
    Ok(())
}

fn resolve_dest_dir(app_folder: &str, dest: &Option<String>) -> PathBuf {
    match dest {
        Some(dest) => {
            let dest_path = PathBuf::from(dest);
            if dest_path.is_absolute() {
                dest_path
            } else {
                PathBuf::from(app_folder).join(dest_path)
            }
        }
        None => PathBuf::from(app_folder).join(DEFAULT_DEST_FOLDER),
    }
}

fn prepare_dest_dir(dest_dir: &Path) -> Result<(), Box<dyn Error>> {
    if dest_dir.exists() && !dest_dir.is_dir() {
        return Err(format!(
            "destination exists but is not a directory: {}",
            dest_dir.display()
        )
        .into());
    }
    fs::create_dir_all(dest_dir)?;
    Ok(())
}

fn fetch_lib(
    account: &str,
    lib: &str,
    dest_dir: &Path,
    branch: &str,
    force: bool,
) -> Result<(), Box<dyn Error>> {
    let repo_url = format!("https://github.com/{}/{}.git", account, lib);
    let lib_path = dest_dir.join(lib);

    if lib_path.join(".git").is_dir() {
        println!("Updating {} from {}...", lib, account);
        update_existing_repo(&lib_path, branch, force)?;
    } else {
        println!("Cloning {} from {}...", lib, account);
        clone_repo(&repo_url, &lib_path)?;
        checkout_ref(&lib_path, branch)?;
    }

    println!("  {} OK", lib);
    Ok(())
}

fn update_existing_repo(lib_path: &Path, branch: &str, force: bool) -> Result<(), Box<dyn Error>> {
    if !force && repo_has_uncommitted_changes(lib_path)? {
        return Err(format!(
            "working tree has uncommitted changes: {} (commit/stash changes or use --force)",
            lib_path.display()
        )
        .into());
    }

    run_git(lib_path, &["fetch", "--all", "--tags"])?;
    checkout_ref(lib_path, branch)?;
    Ok(())
}

fn clone_repo(repo_url: &str, lib_path: &Path) -> Result<(), Box<dyn Error>> {
    if lib_path.exists() {
        return Err(format!(
            "destination exists but is not a git repository: {}",
            lib_path.display()
        )
        .into());
    }

    let status = Command::new("git")
        .arg("clone")
        .arg(repo_url)
        .arg(lib_path)
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()?;

    if !status.success() {
        return Err(format!("git clone failed for {}", repo_url).into());
    }

    Ok(())
}

fn checkout_ref(lib_path: &Path, branch: &str) -> Result<(), Box<dyn Error>> {
    if remote_branch_exists(lib_path, branch)? {
        if local_branch_exists(lib_path, branch)? {
            run_git(lib_path, &["checkout", branch])?;
            run_git(lib_path, &["pull", "--ff-only", "origin", branch])?;
        } else {
            let remote_branch = format!("origin/{}", branch);
            run_git(lib_path, &["checkout", "--track", &remote_branch])?;
        }
    } else {
        run_git(lib_path, &["checkout", branch])?;
    }
    Ok(())
}

fn repo_has_uncommitted_changes(lib_path: &Path) -> Result<bool, Box<dyn Error>> {
    let output = Command::new("git")
        .arg("-C")
        .arg(lib_path)
        .args(["status", "--porcelain"])
        .output()?;

    if !output.status.success() {
        return Err(format!("git status failed in {}", lib_path.display()).into());
    }

    Ok(!output.stdout.is_empty())
}

fn remote_branch_exists(lib_path: &Path, branch: &str) -> Result<bool, Box<dyn Error>> {
    git_ref_exists(lib_path, &format!("refs/remotes/origin/{}", branch))
}

fn local_branch_exists(lib_path: &Path, branch: &str) -> Result<bool, Box<dyn Error>> {
    git_ref_exists(lib_path, &format!("refs/heads/{}", branch))
}

fn git_ref_exists(lib_path: &Path, git_ref: &str) -> Result<bool, Box<dyn Error>> {
    let status = Command::new("git")
        .arg("-C")
        .arg(lib_path)
        .args(["rev-parse", "--verify", git_ref])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()?;

    Ok(status.success())
}

fn run_git(lib_path: &Path, args: &[&str]) -> Result<(), Box<dyn Error>> {
    let status = Command::new("git")
        .arg("-C")
        .arg(lib_path)
        .args(args)
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()?;

    if !status.success() {
        return Err(format!("git {} failed in {}", args.join(" "), lib_path.display()).into());
    }

    Ok(())
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Status of libraries in raftdevlibs
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct RepoStatus {
    name: String,
    branch: String,
    commit: String,
    upstream: Option<String>,
    ahead: u32,
    behind: u32,
    changes: usize,
    error: Option<String>,
}

pub fn status_raft_libs(cmd: &LibsStatusCmd) -> Result<(), Box<dyn Error>> {
    let app_folder = cmd.app_folder.clone().unwrap_or(".".to_string());
    let dest_dir = resolve_dest_dir(&app_folder, &cmd.dest);

    if !dest_dir.is_dir() {
        return Err(format!("library folder not found: {}", dest_dir.display()).into());
    }

    let mut repos = list_git_repos(&dest_dir)?;
    repos.sort();

    if repos.is_empty() {
        println!("No git repositories found in {}", dest_dir.display());
        return Ok(());
    }

    if cmd.fetch {
        println!("Fetching from remotes...");
        for repo in &repos {
            git_fetch_quiet(&dest_dir.join(repo));
        }
    }

    let rows: Vec<RepoStatus> = repos
        .iter()
        .map(|repo| gather_status(repo, &dest_dir.join(repo)))
        .collect();

    print_status_table(&dest_dir, &rows);
    Ok(())
}

fn list_git_repos(dest_dir: &Path) -> Result<Vec<String>, Box<dyn Error>> {
    let mut repos = Vec::new();
    for entry in fs::read_dir(dest_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() && path.join(".git").exists() {
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                repos.push(name.to_string());
            }
        }
    }
    Ok(repos)
}

fn git_fetch_quiet(lib_path: &Path) {
    let _ = Command::new("git")
        .arg("-C")
        .arg(lib_path)
        .args(["fetch", "--all", "--tags", "--quiet"])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

fn git_capture(lib_path: &Path, args: &[&str]) -> Option<String> {
    let output = Command::new("git")
        .arg("-C")
        .arg(lib_path)
        .args(args)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

// Run a git command capturing stdout on success or the stderr text on failure.
fn git_capture_result(lib_path: &Path, args: &[&str]) -> Result<String, String> {
    let output = Command::new("git")
        .arg("-C")
        .arg(lib_path)
        .args(args)
        .output()
        .map_err(|e| e.to_string())?;
    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else {
        let err = String::from_utf8_lossy(&output.stderr).trim().to_string();
        if err.is_empty() {
            Err("git command failed".to_string())
        } else {
            // Collapse to a single line for table display
            Err(err.lines().next().unwrap_or("git command failed").to_string())
        }
    }
}

fn gather_status(name: &str, lib_path: &Path) -> RepoStatus {
    // If we cannot even read the current branch the repo is unreadable
    let branch = match git_capture_result(lib_path, &["rev-parse", "--abbrev-ref", "HEAD"]) {
        Ok(b) => b,
        Err(err) => {
            return RepoStatus {
                name: name.to_string(),
                branch: "-".to_string(),
                commit: "-".to_string(),
                upstream: None,
                ahead: 0,
                behind: 0,
                changes: 0,
                error: Some(err),
            };
        }
    };

    let commit = git_capture(lib_path, &["rev-parse", "--short", "HEAD"])
        .unwrap_or_else(|| "-".to_string());

    let upstream = git_capture(
        lib_path,
        &["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
    );

    let (ahead, behind) = if upstream.is_some() {
        match git_capture(
            lib_path,
            &["rev-list", "--left-right", "--count", "HEAD...@{u}"],
        ) {
            Some(counts) => {
                let mut parts = counts.split_whitespace();
                let ahead = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
                let behind = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
                (ahead, behind)
            }
            None => (0, 0),
        }
    } else {
        (0, 0)
    };

    let changes = git_capture(lib_path, &["status", "--porcelain"])
        .map(|s| if s.is_empty() { 0 } else { s.lines().count() })
        .unwrap_or(0);

    RepoStatus {
        name: name.to_string(),
        branch,
        commit,
        upstream,
        ahead,
        behind,
        changes,
        error: None,
    }
}

fn print_status_table(dest_dir: &Path, rows: &[RepoStatus]) {
    println!("\nGit status of libraries in {}\n", dest_dir.display());

    let sync_strings: Vec<String> = rows
        .iter()
        .map(|r| {
            if r.error.is_some() {
                "-".to_string()
            } else if r.upstream.is_none() {
                "no upstream".to_string()
            } else {
                format!("+{}/-{}", r.ahead, r.behind)
            }
        })
        .collect();

    let state_strings: Vec<String> = rows
        .iter()
        .map(|r| match &r.error {
            Some(err) => err.clone(),
            None => {
                if r.changes == 0 {
                    "clean".to_string()
                } else {
                    format!("{} changed", r.changes)
                }
            }
        })
        .collect();

    let name_w = rows
        .iter()
        .map(|r| r.name.len())
        .chain(std::iter::once("NAME".len()))
        .max()
        .unwrap_or(4);
    let branch_w = rows
        .iter()
        .map(|r| r.branch.len())
        .chain(std::iter::once("BRANCH".len()))
        .max()
        .unwrap_or(6);
    let commit_w = rows
        .iter()
        .map(|r| r.commit.len())
        .chain(std::iter::once("COMMIT".len()))
        .max()
        .unwrap_or(6);
    let sync_w = sync_strings
        .iter()
        .map(|s| s.len())
        .chain(std::iter::once("SYNC".len()))
        .max()
        .unwrap_or(4);

    println!(
        "{:<name_w$}  {:<branch_w$}  {:<commit_w$}  {:<sync_w$}  {}",
        "NAME",
        "BRANCH",
        "COMMIT",
        "SYNC",
        "STATE",
        name_w = name_w,
        branch_w = branch_w,
        commit_w = commit_w,
        sync_w = sync_w,
    );

    let mut dirty = false;
    let mut behind_any = false;
    for (i, r) in rows.iter().enumerate() {
        println!(
            "{:<name_w$}  {:<branch_w$}  {:<commit_w$}  {:<sync_w$}  {}",
            r.name,
            r.branch,
            r.commit,
            sync_strings[i],
            state_strings[i],
            name_w = name_w,
            branch_w = branch_w,
            commit_w = commit_w,
            sync_w = sync_w,
        );
        if r.error.is_none() && r.changes > 0 {
            dirty = true;
        }
        if r.behind > 0 {
            behind_any = true;
        }
    }

    if dirty || behind_any {
        println!();
        if dirty {
            println!("Some libraries have uncommitted changes.");
        }
        if behind_any {
            println!("Some libraries are behind their upstream (run 'raft libs status --fetch' to refresh, then update).");
        }
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pull (fast-forward) libraries in raftdevlibs
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

pub fn pull_raft_libs(cmd: &LibsPullCmd) -> Result<(), Box<dyn Error>> {
    let app_folder = cmd.app_folder.clone().unwrap_or(".".to_string());
    let dest_dir = resolve_dest_dir(&app_folder, &cmd.dest);

    if !dest_dir.is_dir() {
        return Err(format!("library folder not found: {}", dest_dir.display()).into());
    }

    let mut repos = list_git_repos(&dest_dir)?;
    repos.sort();

    if repos.is_empty() {
        println!("No git repositories found in {}", dest_dir.display());
        return Ok(());
    }

    println!("Pulling libraries in {}\n", dest_dir.display());

    let mut failures = Vec::new();
    for repo in &repos {
        let lib_path = dest_dir.join(repo);
        match pull_lib(&lib_path) {
            Ok(msg) => println!("  {:<16} {}", repo, msg),
            Err(err) => {
                println!("  {:<16} SKIPPED: {}", repo, err);
                failures.push(repo.clone());
            }
        }
    }

    if !failures.is_empty() {
        return Err(format!("Could not pull: {}", failures.join(", ")).into());
    }

    println!("\nAll libraries up to date.");
    Ok(())
}

// Fast-forward a single repo. Refuses to touch repos with uncommitted changes
// or without an upstream so local work is never clobbered.
fn pull_lib(lib_path: &Path) -> Result<String, String> {
    // Refuse if the working tree is dirty
    let changes = git_capture(lib_path, &["status", "--porcelain"])
        .ok_or_else(|| "not a readable git repo".to_string())?;
    if !changes.is_empty() {
        return Err("uncommitted changes (commit or stash first)".to_string());
    }

    // Must have an upstream to pull from
    let upstream = git_capture(
        lib_path,
        &["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
    );
    if upstream.is_none() {
        return Err("no upstream configured".to_string());
    }

    let before = git_capture(lib_path, &["rev-parse", "--short", "HEAD"]);
    git_capture_result(lib_path, &["pull", "--ff-only"])?;
    let after = git_capture(lib_path, &["rev-parse", "--short", "HEAD"]);

    match (before, after) {
        (Some(b), Some(a)) if b == a => Ok("already up to date".to_string()),
        (Some(b), Some(a)) => Ok(format!("updated {} -> {}", b, a)),
        _ => Ok("updated".to_string()),
    }
}