flowey_cli/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![expect(missing_docs)]
5#![forbid(unsafe_code)]
6
7use flowey_core::pipeline::IntoPipeline;
8use std::path::Path;
9
10mod cli;
11mod flow_resolver;
12mod pipeline_resolver;
13mod var_db;
14
15/// Entrypoint into generic flowey infrastructure.
16pub fn flowey_main<ProjectPipelines: clap::Subcommand + IntoPipeline>(
17    flowey_crate: &str,
18    repo_root: &Path,
19) -> ! {
20    if let Err(e) = cli::cli_main::<ProjectPipelines>(flowey_crate, repo_root) {
21        log::error!("Error: {:#}", e);
22        std::process::exit(-1);
23    } else {
24        std::process::exit(0)
25    }
26}
27
28/// Check if we're running inside WSL (Windows Subsystem for Linux).
29pub fn running_in_wsl() -> bool {
30    let Ok(output) = std::process::Command::new("wslpath")
31        .args(["-aw", "/"])
32        .output()
33    else {
34        return false;
35    };
36    String::from_utf8_lossy(&output.stdout).starts_with(r"\\wsl.localhost")
37}
38
39/// Check if a path is on a Windows-accessible filesystem in WSL (DrvFs mount).
40///
41/// DrvFs mounts are Windows drives mounted into WSL, which use the 9p filesystem
42/// with `aname=drvfs` in the mount options. This function checks `/proc/mounts`
43/// to find all DrvFs mount points and determines if the given path is under
44/// one of them.
45///
46/// This handles:
47/// - Default automount paths (e.g., /mnt/c/, /mnt/d/)
48/// - Custom automount roots configured via wsl.conf
49/// - Manually mounted Windows drives via fstab or mount command
50///
51/// Returns `false` if not running in WSL or if the check fails.
52pub fn is_wsl_windows_path(path: &Path) -> bool {
53    if !running_in_wsl() {
54        return false;
55    }
56
57    let path = match std::path::absolute(path) {
58        Ok(p) => p,
59        Err(_) => return false,
60    };
61
62    // Parse /proc/mounts to find DrvFs mounts
63    let mounts = match std::fs::read_to_string("/proc/mounts") {
64        Ok(m) => m,
65        Err(_) => return false,
66    };
67
68    let drvfs_mount_points: Vec<String> = mounts
69        .lines()
70        .filter_map(|line| {
71            let parts: Vec<&str> = line.split_whitespace().collect();
72            if parts.len() >= 4 {
73                let mount_point = parts[1];
74                let fs_type = parts[2];
75                let mount_options = parts[3];
76                // DrvFs mounts use 9p filesystem with aname=drvfs in the options
77                if fs_type == "9p" && mount_options.contains("aname=drvfs") {
78                    return Some(mount_point.to_string());
79                }
80            }
81            None
82        })
83        .collect();
84
85    let path_str = path.to_string_lossy();
86
87    // Check if the path is under any DrvFs mount point
88    for mount_point in &drvfs_mount_points {
89        let mount_point_normalized = mount_point.trim_end_matches('/');
90        if path_str == mount_point_normalized
91            || path_str.starts_with(&format!("{}/", mount_point_normalized))
92        {
93            return true;
94        }
95    }
96
97    false
98}