Skip to content

File Directory and File Content resource #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Prev Previous commit
Next Next commit
dsc/examples/filesys_delete.dsc.yaml
  • Loading branch information
adityapatwardhan committed Mar 17, 2025
commit 54f5060c2ce9d0d6620e8591730edfef53e89cbb
22 changes: 22 additions & 0 deletions resources/filesys/directory.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
],
"input": "stdin"
},
"delete": {
"executable": "filesys",
"args": [
"delete",
{
"jsonInputArg": "--input",
"mandatory": true
}
],
"input": "stdin"
},
"export": {
"executable": "filesys",
"args": [
"export",
{
"jsonInputArg": "--input",
"mandatory": true
}
],
"input": "stdin"
},
"schema": {
"command": {
"executable": "filesys",
Expand Down
13 changes: 12 additions & 1 deletion resources/filesys/file.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@
}
],
"input": "stdin"
}
},
"export": {
"executable": "filesys",
"args": [
"export",
{
"jsonInputArg": "--input",
"mandatory": true
}
],
"input": "stdin"
},
"schema": {
"command": {
"executable": "filesys",
Expand Down
2 changes: 1 addition & 1 deletion resources/filesys/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Directory {
pub files: Option<Vec<File>>,

/// Recurse into subdirectories.
pub recurse: bool,
pub recurse: Option<bool>,

#[serde(rename = "_exist", skip_serializing_if = "Option::is_none")]
pub exist: Option<bool>,
Expand Down
66 changes: 0 additions & 66 deletions resources/filesys/src/file_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Licensed under the MIT License.

use crate::config::File;
use crate::config::Directory;
use std::fs;
use std::fs::File as fsFile;
use std::path::Path;
use tracing::{debug};
use fs_extra::dir::get_size;

impl File {
/// Create a new `File`.
Expand All @@ -26,24 +24,6 @@ impl File {
}
}

impl Directory {
/// Create a new `Directory`.
///
/// # Arguments
///
/// * `string` - The string for the Path
#[must_use]
pub fn new(path: &str) -> Directory {
Directory {
path: path.to_string(),
size: None,
files: None,
recurse: false,
exist: None,
}
}
}

pub fn get_file(file: &File) -> Result<File, Box<dyn std::error::Error>> {
debug!("In get_file");
match compare_file_state(file) {
Expand Down Expand Up @@ -105,52 +85,6 @@ pub fn export_file_path(file: &File) -> Result<File, Box<dyn std::error::Error>>
}
}

pub fn export_dir_path(dir: &Directory) -> Result<Directory, Box<dyn std::error::Error>> {
// Export the file or directory
let path = Path::new(dir.path.as_str());

match path.exists() {
false => {
return Ok(Directory { path: path.to_str().unwrap().to_string(), size: None, files: None, recurse: dir.recurse, exist: Some(false) });
}
_ => {}
}

match path.is_dir() {
true => {
let files: Vec<File> = {
let dir = fs::read_dir(path)?;
let mut files = Vec::new();
for entry in dir {
let entry = entry?;
let path = entry.path();
let f = File::new(path.to_str().unwrap());
files.push(get_file(&f)?);
}
files
};

let dir_size = get_size(path)?;

Ok(Directory { path: path.to_str().unwrap().to_string(), size: Some(dir_size), files: Some(files), recurse: dir.recurse, exist: Some(true) })
}
false => {
let path = Path::new(path);
let f = File::new(path.to_str().unwrap());
let file = get_file(&f)?;
let parent = path.parent();
match parent {
Some(parent) => {
Ok(Directory { path: parent.to_str().unwrap().to_string(), size: file.size, files: vec![file].into(), recurse: dir.recurse, exist: Some(true) })
}
_ => {
return Err("Path is not a file or directory")?;
}
}
}
}
}

pub fn delete_file(file: &File) -> Result<(), Box<dyn std::error::Error>> {
match compare_file_state(file) {
Ok(f) => {
Expand Down
22 changes: 12 additions & 10 deletions resources/filesys/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use clap::Parser;
use std::process::exit;
use tracing::{debug, error};
use crate::config::{File, Directory, FileContent};
use file_helper::{get_file, set_file, delete_file, export_file_path, export_dir_path};
use file_helper::{get_file, set_file, delete_file, export_file_path};
use dir_helpers::{get_dir, set_dir, delete_dir, export_dir_path};
use schemars::schema_for;

mod args;
pub mod config;
mod file_helper;
mod dir_helpers;

const EXIT_SUCCESS: i32 = 0;
const EXIT_INVALID_INPUT: i32 = 2;
Expand All @@ -31,9 +33,9 @@ fn main() {
None => {
let dir = match is_directory_type(input.as_str()) {
Some(dir) => {
// let dir = get_file(&dir).unwrap();
// let json = serde_json::to_string(&dir).unwrap();
// println!("{}", json);
let dir = get_dir(&dir).unwrap();
let json = serde_json::to_string(&dir).unwrap();
println!("{}", json);
}
None => {
let filecontent = match is_fillecontent_type(input.as_str()) {
Expand Down Expand Up @@ -64,9 +66,9 @@ fn main() {
None => {
let dir = match is_directory_type(input.as_str()) {
Some(dir) => {
// let dir = delete_file(&dir).unwrap();
// let json = serde_json::to_string(&dir).unwrap();
// println!("{}", json);
let dir = delete_dir(&dir).unwrap();
let json = serde_json::to_string(&dir).unwrap();
println!("{}", json);
}
None => {
let filecontent = match is_fillecontent_type(input.as_str()) {
Expand Down Expand Up @@ -98,9 +100,9 @@ fn main() {
None => {
let dir = match is_directory_type(input.as_str()) {
Some(dir) => {
// let dir = get_file(&dir).unwrap();
// let json = serde_json::to_string(&dir).unwrap();
// println!("{}", json);
let dir = set_dir(&dir).unwrap();
let json = serde_json::to_string(&dir).unwrap();
println!("{}", json);
}
None => {
let filecontent = match is_fillecontent_type(input.as_str()) {
Expand Down