Skip to content

Commit

Permalink
feat: add check command (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat authored Sep 5, 2023
1 parent e23a72f commit f5d52ff
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pad = "0.1.6"
regex = "1.8.3"
reqwest = { version = "0.11", features = ["blocking", "json"] }
scanf = "1.2.1"
semver = "1.0.18"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
serde_yaml = "0.9.21"
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use strum::EnumVariantNames;
use crate::cmd::complete::CompleteArgs;
use crate::cmd::run::attach::AttachArgs;
use crate::cmd::run::branch::BranchArgs;
use crate::cmd::run::check::CheckArgs;
use crate::cmd::run::config::ConfigArgs;
use crate::cmd::run::detach::DetachArgs;
use crate::cmd::run::gc::GcArgs;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct App {
pub enum Commands {
Attach(AttachArgs),
Branch(BranchArgs),
Check(CheckArgs),
Complete(CompleteArgs),
Config(ConfigArgs),
Detach(DetachArgs),
Expand Down Expand Up @@ -77,6 +79,7 @@ impl Run for App {
match &self.command {
Commands::Attach(args) => args.run(),
Commands::Branch(args) => args.run(),
Commands::Check(args) => args.run(),
Commands::Complete(args) => args.run(),
Commands::Config(args) => args.run(),
Commands::Detach(args) => args.run(),
Expand Down
117 changes: 117 additions & 0 deletions src/cmd/run/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::PathBuf;

use anyhow::{bail, Context, Result};
use clap::Args;
use console::style;
use semver::VersionReq;

use crate::cmd::Run;
use crate::config;
use crate::config::types::Config;
use crate::{term, utils};

/// Check your machine env
#[derive(Args)]
pub struct CheckArgs {}

impl Run for CheckArgs {
fn run(&self) -> Result<()> {
let checks: Vec<(&str, fn() -> Result<()>)> = vec![
("git", check_git),
("fzf", check_fzf),
("shell", check_shell),
("config", check_config),
];

let mut fail_message = Vec::new();
for (name, check) in checks {
print!("Check {name} ... ");
match check() {
Ok(_) => println!("{}", style("pass").green()),
Err(err) => {
fail_message.push(format!("{name}: {err}"));
println!("{}", style("fail").red());
}
}
}

println!();
if !fail_message.is_empty() {
println!("Check failed:");
for msg in fail_message {
println!(" {msg}");
}
} else {
println!("All checks passed");
}

Ok(())
}
}

const REQ_GIT_VERSION: &str = ">=1.20.0";

fn check_git() -> Result<()> {
let ver = term::git_version()?;
let req = VersionReq::parse(REQ_GIT_VERSION).unwrap();

if !req.matches(&ver) {
bail!("git version too low, require >= 1.20.0");
}
Ok(())
}

const REQ_FZF_VERSION: &str = ">=0.40.0";

fn check_fzf() -> Result<()> {
let ver = term::fzf_version()?;
let req = VersionReq::parse(REQ_FZF_VERSION).unwrap();

if !req.matches(&ver) {
bail!("fzf version too low, require >= 0.40.0");
}
Ok(())
}

fn check_shell() -> Result<()> {
let shell = term::shell_type()?;
match shell.as_str() {
"bash" | "zsh" => Ok(()),
_ => bail!("Unsupport shell {shell}"),
}
}

fn check_config() -> Result<()> {
let config_dir = Config::dir()?;
check_config_dir(config_dir).context("Check config dir")?;

let workspace = PathBuf::from(&config::base().workspace);
check_config_dir(workspace).context("Check workspace dir")?;

let meta_dir = PathBuf::from(&config::base().metadir);
check_config_dir(meta_dir).context("Check meta dir")?;

Ok(())
}

fn check_config_dir(path: PathBuf) -> Result<()> {
let test_file = path.join(".test_roxide_file");
utils::ensure_dir(&test_file).context("Ensure dir")?;

let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&test_file)
.context("Open test file")?;

let test_data = &[1, 2, 3, 4, 5];
file.write_all(test_data).context("Write test file")?;
drop(file);

fs::remove_file(&test_file).context("Remove test file")?;

Ok(())
}
1 change: 1 addition & 0 deletions src/cmd/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod attach;
pub mod branch;
pub mod check;
pub mod config;
pub mod detach;
pub mod gc;
Expand Down
42 changes: 42 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use pad::PadStr;
use regex::{Captures, Regex};
use reqwest::blocking::Client;
use reqwest::{Method, Url};
use semver::Version;
use serde::Serialize;
use serde_json::ser::PrettyFormatter;
use serde_json::Serializer;
Expand Down Expand Up @@ -310,6 +311,47 @@ pub fn get_editor() -> Result<String> {
Ok(editor)
}

pub fn git_version() -> Result<Version> {
let version = Cmd::exec_git_mute_read(&["version"])?;
let version = match version.trim().strip_prefix("git version") {
Some(v) => v.trim(),
None => bail!("Unknown git version output: {version}"),
};

match Version::parse(&version) {
Ok(ver) => Ok(ver),
Err(_) => bail!("git version {version} is bad format"),
}
}

pub fn fzf_version() -> Result<Version> {
let version = Cmd::exec_mute_read("fzf", &["--version"])?;
let mut fields = version.split(" ");
let version = match fields.next() {
Some(v) => v.trim(),
None => bail!("Unknown fzf version output: {version}"),
};
match Version::parse(&version) {
Ok(ver) => Ok(ver),
Err(_) => bail!("fzf version {version} is bad format"),
}
}

pub fn shell_type() -> Result<String> {
let shell = env::var("SHELL").context("Get SHELL env")?;
if shell.is_empty() {
bail!("env SHELL is empty");
}
let path = PathBuf::from(&shell);
match path.file_name() {
Some(name) => match name.to_str() {
Some(name) => Ok(String::from(name)),
None => bail!("Bad SHELL format: {shell}"),
},
None => bail!("Bad SHELL env: {shell}"),
}
}

pub struct Cmd {
cmd: Command,
desc: Option<String>,
Expand Down

0 comments on commit f5d52ff

Please sign in to comment.