Skip to content

Commit

Permalink
Merge pull request #11 from algoux/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
MeiK2333 authored Mar 13, 2024
2 parents 18e9089 + 395053e commit 4e23b2a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clap-verbosity-flag = "2.2.0"
log = "0.4.20"
env_logger = "0.10.2"
tempfile = "3.10.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"

[target.'cfg(windows)'.dependencies.windows]
version = "0.53.0"
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
# river

## 用法

```bash
$ river -h
example: `river -vvv -- /usr/bin/echo hello world`

Usage: river.exe [OPTIONS] -- <COMMAND>...

Arguments:
<COMMAND>... Program to run and command line arguments

Options:
-i, --input <INPUT>
Input stream. The default value is STDIN(0)
-o, --output <OUTPUT>
Output stream. The default value is STDOUT(1)
-e, --error <ERROR>
Error stream. The default value is STDERR(2)
-r, --result <RESULT>
Output location of the running result. The default value is STDOUT(1)
-t, --time-limit <TIME_LIMIT>
Time limit, in ms. The default value is unlimited
-c, --cpu-time-limit <CPU_TIME_LIMIT>
CPU Time limit, in ms. The default value is unlimited
-m, --memory-limit <MEMORY_LIMIT>
Memory limit, in kib. The default value is unlimited
-v, --verbose...
Increase logging verbosity
-q, --quiet...
Decrease logging verbosity
-h, --help
Print help
-V, --version
Print version
```

## 结果

结果的格式为 JSON

| 字段 | 含义 |
|-----------------|--------------------|
| `time_used` | 程序运行用时 |
| `cpu_time_used` | 程序运行使用 CPU 时间 |
| `memory_used` | 程序运行使用内存 |
| `exit_code` | 程序退出 code,正常情况下为 0 |
| `status` | 正常情况下为 0 |
| `signal` | 正常情况下为 0 |

## 系统支持

`~` 代表开发中的功能
Expand Down
18 changes: 11 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use serde_json::Error as SerdeJsonError;
use std::fmt::Formatter;
use std::io::Error as IOError;
use std::{fmt, result};

#[cfg(target_os = "windows")]
Expand All @@ -7,6 +9,8 @@ use windows::core::Error as WIN_ERROR;
#[derive(Debug)]
pub enum Error {
E(String, u32, String),
IOError(IOError),
SerdeJsonError(SerdeJsonError),
/// Windows 平台下的 LastError
#[cfg(target_os = "windows")]
WinError(String, u32, WIN_ERROR),
Expand All @@ -18,13 +22,13 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self {
Error::E(ref filename, ref line, ref e) => {
write!(
f,
"{}:{}: Error: {}",
filename,
line,
e
)
write!(f, "{}:{}: Error: {}", filename, line, e)
}
Error::IOError(ref e) => {
write!(f, "{}", e)
}
Error::SerdeJsonError(ref e) => {
write!(f, "{}", e)
}
#[cfg(target_os = "windows")]
Error::WinError(ref filename, ref line, ref e) => {
Expand Down
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clap::Parser;
use clap_verbosity_flag::Verbosity;
use env_logger::Builder;
use log::{error, info, trace};
use log::{error, trace};

#[cfg(target_os = "linux")]
use sys::linux::Sandbox;
#[cfg(target_os = "macos")]
Expand All @@ -11,9 +12,9 @@ use sys::windows::Sandbox;

use crate::sys::SandboxImpl;

mod sys;
mod error;
mod status;
mod sys;

/// example: `river -vvv -- /usr/bin/echo hello world`
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -105,10 +106,14 @@ fn main() {
.init();

trace!("{:?}", opts);
let result = opts.result.clone();
let status = unsafe { Sandbox::with_opts(opts).run() };
match status {
Ok(_) => {
info!("success");
Ok(val) => {
if let Err(e) = val.write(result) {
error!("{}", e);
std::process::exit(1);
}
}
Err(e) => {
error!("{}", e);
Expand Down
20 changes: 19 additions & 1 deletion src/status.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::fmt;
use std::fmt::Formatter;
use std::fs;

#[derive(Debug)]
use serde::{Deserialize, Serialize};

use crate::error::Error::{IOError, SerdeJsonError};
use crate::error::Result;

#[derive(Debug, Serialize, Deserialize)]
pub struct Status {
pub time_used: u64,
pub cpu_time_used: u64,
Expand All @@ -11,6 +17,18 @@ pub struct Status {
pub signal: i32,
}

impl Status {
pub fn write(&self, file: Option<String>) -> Result<()> {
let json_str = serde_json::to_string_pretty(&self).map_err(|e| SerdeJsonError(e))?;
if let Some(f) = file {
fs::write(f, format!("{}", json_str)).map_err(|e| IOError(e))?;
} else {
println!("{}", json_str);
};
Ok(())
}
}

impl fmt::Display for Status {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
Expand Down
2 changes: 0 additions & 2 deletions src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub struct Sandbox {
input: Option<String>,
output: Option<String>,
error: Option<String>,
result: Option<String>,
}

impl Sandbox {
Expand Down Expand Up @@ -182,7 +181,6 @@ impl SandboxImpl for Sandbox {
input: opts.input,
output: opts.output,
error: opts.error,
result: opts.result,
}
}

Expand Down

0 comments on commit 4e23b2a

Please sign in to comment.