mirror of
https://github.com/ivabus/smurf
synced 2024-11-23 17:05:10 +03:00
Initial commit
This commit is contained in:
commit
88264697ca
8 changed files with 111 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/Cargo.lock
|
10
.rustfmt.toml
Normal file
10
.rustfmt.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
edition = "2021"
|
||||||
|
hard_tabs = true
|
||||||
|
merge_derives = true
|
||||||
|
reorder_imports = true
|
||||||
|
reorder_modules = true
|
||||||
|
use_field_init_shorthand = true
|
||||||
|
use_small_heuristics = "Off"
|
||||||
|
wrap_comments = true
|
||||||
|
comment_width = 80
|
||||||
|
indent_style = "Block"
|
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
[package]
|
||||||
|
name = "smurf"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
authors = ["Ivan Bushchik <ivabus@ivabus.dev>"]
|
||||||
|
description = "SMall Useful Rust Functions"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "https://github.com/ivabus/smurf"
|
||||||
|
license = "MIT"
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
20
LICENSE
Normal file
20
LICENSE
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 Ivan Bushchik
|
||||||
|
|
||||||
|
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.
|
13
README.md
Normal file
13
README.md
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# SmURF
|
||||||
|
|
||||||
|
> SMall Useful Rust Functions
|
||||||
|
|
||||||
|
## Components
|
||||||
|
|
||||||
|
### Shell
|
||||||
|
|
||||||
|
- macro `shell!`
|
||||||
|
|
||||||
|
### File
|
||||||
|
|
||||||
|
- function `read_to_str`
|
18
src/file.rs
Normal file
18
src/file.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use log::error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{prelude::*, BufReader};
|
||||||
|
|
||||||
|
pub fn read_to_str(path: &std::path::PathBuf) -> String {
|
||||||
|
let file = File::open(&path);
|
||||||
|
let file = match file {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(err) => {
|
||||||
|
error!("Cannot open file \"{}\"", path.display());
|
||||||
|
std::process::exit(2)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut buf_reader = BufReader::new(file);
|
||||||
|
let mut content = String::new();
|
||||||
|
buf_reader.read_to_string(&mut content).unwrap();
|
||||||
|
content
|
||||||
|
}
|
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
mod file;
|
||||||
|
mod shell;
|
34
src/shell.rs
Normal file
34
src/shell.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! shell {
|
||||||
|
($($arg:tt)*) => {{
|
||||||
|
$crate::shell::run(format!($($arg)*))
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ShellResult {
|
||||||
|
pub code: i32,
|
||||||
|
pub stdout: String,
|
||||||
|
pub stderr: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn run(command: String) -> ShellResult {
|
||||||
|
let mut iter = command.split_whitespace();
|
||||||
|
let mut current = iter.next();
|
||||||
|
let mut words: Vec<&str> = vec![];
|
||||||
|
while current != None {
|
||||||
|
words.push(current.unwrap());
|
||||||
|
current = iter.next();
|
||||||
|
}
|
||||||
|
let command = Command::new(&words[0])
|
||||||
|
.args(&words[1..])
|
||||||
|
.output()
|
||||||
|
.expect(&format!("Failed to execute '{}'", command));
|
||||||
|
return ShellResult {
|
||||||
|
code: command.status.code().unwrap(),
|
||||||
|
stdout: String::from_utf8(command.stdout).unwrap(),
|
||||||
|
stderr: String::from_utf8(command.stderr).unwrap(),
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue