Skip to content

add safeguard to prevent moss from eating your current operating system #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions moss/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use std::{env, fs, io, path::Path, path::PathBuf};

use clap::{Arg, ArgAction, Command};
use clap::{
builder::{IntoResettable, OsStr},
Arg, ArgAction, Command,
};
use clap_complete::{
generate_to,
shells::{Bash, Fish, Zsh},
Expand Down Expand Up @@ -46,7 +49,7 @@
.global(true)
.help("Root directory")
.action(ArgAction::Set)
.default_value("/")
.default_value(default_root_directory_arg())
.value_parser(clap::value_parser!(PathBuf)),
)
.arg(
Expand Down Expand Up @@ -97,6 +100,17 @@
.subcommand(version::command())
}

/// only provide a default directory argument if the host system can be identified as a Serpent OS system
fn default_root_directory_arg() -> impl IntoResettable<OsStr> {
// serpent os is identified by `MACHTYPE` contains `serpent`
let is_host_serpent_os = env::var("MACHTYPE")
.ok()
.map(|machtype| machtype.contains("serpent"))
.unwrap_or(false);

is_host_serpent_os.then(|| "/".into())

Check warning on line 111 in moss/src/cli/mod.rs

View workflow job for this annotation

GitHub Actions / Build & Test Project

[clippy] reported by reviewdog 🐶 warning: useless conversion to the same type: `&str` --> moss/src/cli/mod.rs:111:32 | 111 | is_host_serpent_os.then(|| "/".into()) | ^^^^^^^^^^ help: consider removing `.into()`: `"/"` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default Raw Output: moss/src/cli/mod.rs:111:32:w:warning: useless conversion to the same type: `&str` --> moss/src/cli/mod.rs:111:32 | 111 | is_host_serpent_os.then(|| "/".into()) | ^^^^^^^^^^ help: consider removing `.into()`: `"/"` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default __END__
}

/// Generate manpages for all commands recursively
fn generate_manpages(cmd: &Command, dir: &Path, prefix: Option<&str>) -> io::Result<()> {
let name = cmd.get_name();
Expand Down Expand Up @@ -159,7 +173,11 @@
}
}

let root = matches.get_one::<PathBuf>("root").unwrap();
let root = match matches.get_one::<PathBuf>("root") {
Some(root) => root,
None => return Err(Error::Args("Root directory is required, but not provided".to_owned())),
};

let cache = matches.get_one::<PathBuf>("cache");

// Make async runtime available to all of moss
Expand Down Expand Up @@ -268,4 +286,7 @@

#[error("I/O error: {0}")]
Io(#[from] io::Error),

#[error("args: {0}")]
Args(String),
}