Compare commits

...

8 Commits

8 changed files with 182 additions and 14 deletions

68
Cargo.lock generated
View File

@@ -53,6 +53,15 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.4.0"
@@ -108,6 +117,21 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac"
[[package]]
name = "cpuid-bool"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array",
]
[[package]]
name = "dtoa"
version = "0.4.6"
@@ -224,6 +248,16 @@ dependencies = [
"slab",
]
[[package]]
name = "generic-array"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60fb4bb6bba52f78a471264d9a3b7d026cc0af47b22cd2cffbc0b787ca003e63"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.1.14"
@@ -272,6 +306,12 @@ dependencies = [
"libc",
]
[[package]]
name = "hex"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
[[package]]
name = "http"
version = "0.2.1"
@@ -564,6 +604,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.30"
@@ -796,12 +842,15 @@ name = "rust-script-tool"
version = "0.1.0"
dependencies = [
"clap",
"hex",
"lazy_static",
"log",
"pretty_env_logger",
"quick-error",
"reqwest",
"serde",
"serde_json",
"sha2",
"tokio",
]
@@ -887,6 +936,19 @@ dependencies = [
"url",
]
[[package]]
name = "sha2"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2933378ddfeda7ea26f48c555bdad8bb446bf8a3d17832dc83e380d444cfb8c1"
dependencies = [
"block-buffer",
"cfg-if",
"cpuid-bool",
"digest",
"opaque-debug",
]
[[package]]
name = "signal-hook-registry"
version = "1.2.0"
@@ -1080,6 +1142,12 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "unicase"
version = "2.6.0"

View File

@@ -15,4 +15,6 @@ reqwest = { version = "0.10", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
lazy_static = "1.4.0"
hex = "0.4.2"
sha2 = "0.9.1"
quick-error = "1.2.3"

View File

@@ -9,6 +9,14 @@ Software
* Linux
* macOS
* Git
* Cargo \[when install from source\]
## Install
Install from git repo:
```shell
% cargo install --git https://git.hatter.ink/rust-scripts/rust-script-tool.git
```
## Usage

View File

@@ -11,8 +11,10 @@ impl CommandDefault {
}
pub fn run(_arg_matches: &ArgMatches) -> CommandError {
information!("Git path : {:?}", crate::git::get_git_base_path());
information!("Git status: {}", crate::git::check_git_status());
information!("Git hash : {}", crate::git::get_git_hash().unwrap_or_else(|| "<none>".to_owned()));
information!("SHA256 R : {}", hex::encode(crate::util::digest_file_sha256("README.md").unwrap()));
error!("Default command is not implemented!");
Ok(())
}

View File

@@ -1,8 +1,37 @@
use std::path::PathBuf;
use std::process::Command;
trait RunCommandAsEnUs {
fn en_us(&mut self) -> &mut Self;
}
impl RunCommandAsEnUs for Command {
fn en_us(&mut self) -> &mut Self {
self.env("LANG", "en_US")
}
}
pub fn get_git_base_path() -> Option<PathBuf> {
match PathBuf::from(".").canonicalize() {
Err(e) => {
warn!("Get current path failed: {}", e);
None
},
Ok(mut path) => loop {
if path.join(".git").is_dir() {
debug!("Found git base dir: {:?}", path);
return Some(path);
}
if !path.pop() {
return None;
}
}
}
}
pub fn check_git_status() -> bool {
trace!("Run: git fetch --dry-run");
match Command::new("git").env("LANG", "en_US").args(&["fetch", "--dry-run"]).output() {
match Command::new("git").en_us().args(&["fetch", "--dry-run"]).output() {
Err(e) => {
warn!("Error in run git fetch --dry-run: {}", e);
return false;
@@ -19,7 +48,7 @@ pub fn check_git_status() -> bool {
},
}
trace!("Run: git status");
match Command::new("git").env("LANG", "en_US").args(&["status"]).output() {
match Command::new("git").en_us().args(&["status"]).output() {
Err(e) => {
warn!("Error in run git status: {}", e);
return false;
@@ -43,7 +72,7 @@ pub fn check_git_status() -> bool {
pub fn get_git_hash() -> Option<String> {
trace!("Run: git rev-parse HEAD");
match Command::new("git").args(&["rev-parse", "HEAD"]).output(){
match Command::new("git").en_us().args(&["rev-parse", "HEAD"]).output(){
Err(e) => {
warn!("Error in run git rev-parse HEAD: {}", e);
None
@@ -53,7 +82,7 @@ pub fn get_git_hash() -> Option<String> {
warn!("Error in get utf8 git rev-parse HEAD: {}", e);
None
},
Ok(stdout) => Some(stdout),
Ok(stdout) => Some(stdout.trim().into()),
}
}
}

View File

@@ -1,4 +1,5 @@
#[macro_use] extern crate log;
#[macro_use] extern crate quick_error;
//#[macro_use] extern crate lazy_static;
use log::LevelFilter;
use clap::{ App, ArgMatches };
@@ -8,6 +9,7 @@ mod cmd_default;
mod cmd_new;
mod git;
mod script;
mod util;
use cmd::{ Command, CommandError };
use cmd_default::CommandDefault;
@@ -23,17 +25,22 @@ use cmd_new::CommandNew;
($($arg:tt)+) => ( crate::print_wa(&format!($($arg)+)); )
}
const GREEN: &str = "\x1B[92m";
const YELLOW: &str = "\x1B[93m";
const BOLD: &str = "\x1B[1m";
const END: &str = "\x1B[0m";
#[inline]
fn print_in (msg: &str) {
println!("{b}[INFO]{e} {m}", b = "\x1B[1m", e = "\x1B[0m", m = msg);
println!("{b}[INFO]{e} {m}", b = BOLD, e = END, m = msg);
}
#[inline]
fn print_ok (msg: &str) {
println!("{g}{b}[ OK ]{e} {g}{m}{e}", g = "\x1B[92m", b = "\x1B[1m", e = "\x1B[0m", m = msg);
println!("{g}{b}[ OK ]{e} {g}{m}{e}", g = GREEN, b = BOLD, e = END, m = msg);
}
#[inline]
fn print_wa (msg: &str) {
println!("{g}{b}[WARN]{e} {g}{m}{e}", g = "\x1B[93m", b = "\x1B[1m", e = "\x1B[0m", m = msg);
println!("{g}{b}[WARN]{e} {g}{m}{e}", g = YELLOW, b = BOLD, e = END, m = msg);
}
fn main() -> CommandError {

View File

@@ -1,13 +1,34 @@
use std::fs;
use std::path::PathBuf;
use serde::{ Deserialize, Serialize };
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScriptDescription {
pub name: String,
pub version: String,
pub hash: String,
pub repo: String,
#[serde(rename = "gitHash")]
pub git_hash: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScriptConfig {
pub repo: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScriptLocalConfig {
#[serde(rename = "indexPath")]
pub index_path: String,
}
pub fn _get_script_base_path(base: &PathBuf, file_name: &str) -> PathBuf {
let mut fn_chars = file_name.chars().take_while(|c| *c != '.');
let path = base.join(fn_chars.next().unwrap_or('_').to_string())
.join(fn_chars.next().unwrap_or('_').to_string())
.join(fn_chars.next().unwrap_or('_').to_string())
.join(file_name);
path
base.join(fn_chars.next().unwrap_or('_').to_string())
.join(fn_chars.next().unwrap_or('_').to_string())
.join(fn_chars.next().unwrap_or('_').to_string())
.join(file_name)
}
pub fn _list_versions(script_base_path: &PathBuf) -> Vec<(PathBuf, String)> {
@@ -21,7 +42,7 @@ pub fn _list_versions(script_base_path: &PathBuf) -> Vec<(PathBuf, String)> {
trace!("Found dir: {:?}", path);
match path.file_name().to_str() {
None => error!("Get path file name: {:?}, is none", path),
Some(file_name) => if file_name.starts_with("v") {
Some(file_name) => if file_name.starts_with('v') {
let ver = get_dir_ver(&file_name);
ret.push((path.path(), ver));
},

31
src/util.rs Normal file
View File

@@ -0,0 +1,31 @@
use std::fs;
use sha2::{ Digest, Sha256 };
quick_error! {
#[derive(Debug)]
pub enum DigestError {
FileOpenError(f: String, m: String) {
display("Read file: {}, failed: {}", f, m)
}
}
}
// pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
pub fn digest_file_sha256(file: &str) -> Result<Vec<u8>, DigestError> {
let bs = match fs::read(file) {
Ok(bs) => bs, Err(e) => return Err(DigestError::FileOpenError(file.into(), e.to_string())),
};
Ok(digest_sha256(&bs))
}
pub fn digest_sha256(bs: &[u8]) -> Vec<u8> {
let mut sha256 = Sha256::new();
sha256.update(bs);
sha256.finalize().to_vec()
}
#[test]
fn test_digest_sha256() {
assert_eq!(hex::decode("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9").unwrap(), digest_sha256(b"hello world"));
}