mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-31 14:33:18 +02:00
Git support (#143)
This commit is contained in:
@@ -3,14 +3,14 @@ use crate::error::Result;
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha1::{Digest, Sha1};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use tokio::fs;
|
||||
use ts_rs::TS;
|
||||
use yaak_models::models::{
|
||||
AnyModel, Environment, Folder, GrpcRequest, HttpRequest, WebsocketRequest, Workspace,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "snake_case", tag = "type")]
|
||||
#[ts(export, export_to = "gen_models.ts")]
|
||||
pub enum SyncModel {
|
||||
@@ -23,12 +23,10 @@ pub enum SyncModel {
|
||||
}
|
||||
|
||||
impl SyncModel {
|
||||
pub async fn from_file(file_path: &Path) -> Result<Option<(SyncModel, Vec<u8>, String)>> {
|
||||
let content = match fs::read(file_path).await {
|
||||
Ok(c) => c,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
|
||||
pub fn from_bytes(
|
||||
content: Vec<u8>,
|
||||
file_path: &Path,
|
||||
) -> Result<Option<(SyncModel, Vec<u8>, String)>> {
|
||||
let mut hasher = Sha1::new();
|
||||
hasher.update(&content);
|
||||
let checksum = hex::encode(hasher.finalize());
|
||||
@@ -39,10 +37,20 @@ impl SyncModel {
|
||||
} else if ext == "json" {
|
||||
Ok(Some((serde_json::from_reader(content.as_slice())?, content, checksum)))
|
||||
} else {
|
||||
Err(InvalidSyncFile(file_path.to_str().unwrap().to_string()))
|
||||
let p = file_path.to_str().unwrap().to_string();
|
||||
Err(InvalidSyncFile(format!("Unknown file extension {p}")))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_file(file_path: &Path) -> Result<Option<(SyncModel, Vec<u8>, String)>> {
|
||||
let content = match fs::read(file_path) {
|
||||
Ok(c) => c,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
|
||||
Self::from_bytes(content, file_path)
|
||||
}
|
||||
|
||||
pub fn to_file_contents(&self, rel_path: &Path) -> Result<(Vec<u8>, String)> {
|
||||
let ext = rel_path.extension().unwrap_or_default();
|
||||
let content = if ext == "yaml" || ext == "yml" {
|
||||
|
||||
Reference in New Issue
Block a user