is-wsl-0.4.0/.cargo_vcs_info.json0000644000000001360000000000100122530ustar { "git": { "sha1": "ebb07834cdbf27328046a88fbd673c17618024e8" }, "path_in_vcs": "" }is-wsl-0.4.0/.github/workflows/ci.yaml000064400000000000000000000007111046102023000157160ustar 00000000000000name: Tests on: push: pull_request: schedule: [cron: "40 1 * * *"] permissions: contents: read jobs: test: name: Test on Ubuntu runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: [stable, beta] steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@master name: Install toolchain with: toolchain: ${{ matrix.rust }} - run: cargo testis-wsl-0.4.0/.gitignore000064400000000000000000000005001046102023000130260ustar 00000000000000# Generated by Cargo # will have compiled files and executables /target/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk is-wsl-0.4.0/Cargo.lock0000644000000011510000000000100102240ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "is-docker" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3" dependencies = [ "once_cell", ] [[package]] name = "is-wsl" version = "0.4.0" dependencies = [ "is-docker", "once_cell", ] [[package]] name = "once_cell" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" is-wsl-0.4.0/Cargo.toml0000644000000015560000000000100102600ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "is-wsl" version = "0.4.0" authors = ["Sean Larkin "] description = "Checks if the process is running inside Windows Subsystem for Linux." readme = "README.md" categories = ["command-line-utilities"] license = "MIT" repository = "https://github.com/TheLarkInn/is-wsl" [dependencies.is-docker] version = "0.2.0" [dependencies.once_cell] version = "1.17.0" is-wsl-0.4.0/Cargo.toml.orig000064400000000000000000000007101046102023000137300ustar 00000000000000[package] name = "is-wsl" description = "Checks if the process is running inside Windows Subsystem for Linux." version = "0.4.0" authors = ["Sean Larkin "] repository = "https://github.com/TheLarkInn/is-wsl" license = "MIT" categories = ["command-line-utilities"] [dependencies] is-docker = "0.2.0" once_cell = "1.17.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html is-wsl-0.4.0/LICENSE000064400000000000000000000020541046102023000120510ustar 00000000000000MIT License Copyright (c) 2023 Sean Larkin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. is-wsl-0.4.0/README.md000064400000000000000000000010751046102023000123250ustar 00000000000000# is-wsl > Check if the process is running inside [Windows Subsystem for Linux](https://msdn.microsoft.com/commandline/wsl/about) (Bash on Windows) Inspired by [sindresorhus/is-wsl](https://github.com/sindresorhus/is-wsl) and made for Rust lang. Can be useful if you need to work around unimplemented or buggy features in WSL. Supports both WSL 1 and WSL 2. ## Usage `$> cargo add is-wsl` _main.rs_ ```rust use is_wsl::is_wsl fn main() { if is_wsl() { // Do some WSL related stuff 🎇 } else { // Do some different things! <3 } } ``` is-wsl-0.4.0/examples/main.rs000064400000000000000000000002361046102023000141540ustar 00000000000000extern crate is_wsl; fn main() { if is_wsl::is_wsl() { println!("Currently in WSL") } else { println!("Currently NOT in WSL") } } is-wsl-0.4.0/src/lib.rs000064400000000000000000000044661046102023000127600ustar 00000000000000extern crate is_docker; extern crate once_cell; use once_cell::sync::OnceCell; use std::{fs::File, io::Read}; pub fn is_wsl() -> bool { static CACHED_RESULT: OnceCell = OnceCell::new(); *CACHED_RESULT.get_or_init(|| { if std::env::consts::OS != "linux" { return false; } if let Ok(os_release) = get_os_release() { if os_release.to_lowercase().contains("microsoft") { return !is_docker::is_docker(); } } if proc_version_includes_microsoft() { !is_docker::is_docker() } else { false } }) } fn proc_version_includes_microsoft() -> bool { match std::fs::read_to_string("/proc/version") { Ok(file_contents) => file_contents.to_lowercase().contains("microsoft"), Err(_) => false, } } // This function is copied from the sys-info crate to avoid taking a dependency on all of sys-info // https://docs.rs/sys-info/0.9.1/src/sys_info/lib.rs.html#426-433 // // The MIT License (MIT) // // Copyright (c) 2015 Siyu Wang // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. fn get_os_release() -> Result { let mut s = String::new(); File::open("/proc/sys/kernel/osrelease")?.read_to_string(&mut s)?; s.pop(); // pop '\n' Ok(s) }