Skip to content

Commit 54f5060

Browse files
dsc/examples/filesys_delete.dsc.yaml
1 parent 20567c0 commit 54f5060

File tree

5 files changed

+47
-78
lines changed

5 files changed

+47
-78
lines changed

resources/filesys/directory.dsc.resource.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
],
2727
"input": "stdin"
2828
},
29+
"delete": {
30+
"executable": "filesys",
31+
"args": [
32+
"delete",
33+
{
34+
"jsonInputArg": "--input",
35+
"mandatory": true
36+
}
37+
],
38+
"input": "stdin"
39+
},
40+
"export": {
41+
"executable": "filesys",
42+
"args": [
43+
"export",
44+
{
45+
"jsonInputArg": "--input",
46+
"mandatory": true
47+
}
48+
],
49+
"input": "stdin"
50+
},
2951
"schema": {
3052
"command": {
3153
"executable": "filesys",

resources/filesys/file.dsc.resource.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@
3636
}
3737
],
3838
"input": "stdin"
39-
}
39+
},
40+
"export": {
41+
"executable": "filesys",
42+
"args": [
43+
"export",
44+
{
45+
"jsonInputArg": "--input",
46+
"mandatory": true
47+
}
48+
],
49+
"input": "stdin"
50+
},
4051
"schema": {
4152
"command": {
4253
"executable": "filesys",

resources/filesys/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct Directory {
3636
pub files: Option<Vec<File>>,
3737

3838
/// Recurse into subdirectories.
39-
pub recurse: bool,
39+
pub recurse: Option<bool>,
4040

4141
#[serde(rename = "_exist", skip_serializing_if = "Option::is_none")]
4242
pub exist: Option<bool>,

resources/filesys/src/file_helper.rs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// Licensed under the MIT License.
33

44
use crate::config::File;
5-
use crate::config::Directory;
65
use std::fs;
76
use std::fs::File as fsFile;
87
use std::path::Path;
98
use tracing::{debug};
10-
use fs_extra::dir::get_size;
119

1210
impl File {
1311
/// Create a new `File`.
@@ -26,24 +24,6 @@ impl File {
2624
}
2725
}
2826

29-
impl Directory {
30-
/// Create a new `Directory`.
31-
///
32-
/// # Arguments
33-
///
34-
/// * `string` - The string for the Path
35-
#[must_use]
36-
pub fn new(path: &str) -> Directory {
37-
Directory {
38-
path: path.to_string(),
39-
size: None,
40-
files: None,
41-
recurse: false,
42-
exist: None,
43-
}
44-
}
45-
}
46-
4727
pub fn get_file(file: &File) -> Result<File, Box<dyn std::error::Error>> {
4828
debug!("In get_file");
4929
match compare_file_state(file) {
@@ -105,52 +85,6 @@ pub fn export_file_path(file: &File) -> Result<File, Box<dyn std::error::Error>>
10585
}
10686
}
10787

108-
pub fn export_dir_path(dir: &Directory) -> Result<Directory, Box<dyn std::error::Error>> {
109-
// Export the file or directory
110-
let path = Path::new(dir.path.as_str());
111-
112-
match path.exists() {
113-
false => {
114-
return Ok(Directory { path: path.to_str().unwrap().to_string(), size: None, files: None, recurse: dir.recurse, exist: Some(false) });
115-
}
116-
_ => {}
117-
}
118-
119-
match path.is_dir() {
120-
true => {
121-
let files: Vec<File> = {
122-
let dir = fs::read_dir(path)?;
123-
let mut files = Vec::new();
124-
for entry in dir {
125-
let entry = entry?;
126-
let path = entry.path();
127-
let f = File::new(path.to_str().unwrap());
128-
files.push(get_file(&f)?);
129-
}
130-
files
131-
};
132-
133-
let dir_size = get_size(path)?;
134-
135-
Ok(Directory { path: path.to_str().unwrap().to_string(), size: Some(dir_size), files: Some(files), recurse: dir.recurse, exist: Some(true) })
136-
}
137-
false => {
138-
let path = Path::new(path);
139-
let f = File::new(path.to_str().unwrap());
140-
let file = get_file(&f)?;
141-
let parent = path.parent();
142-
match parent {
143-
Some(parent) => {
144-
Ok(Directory { path: parent.to_str().unwrap().to_string(), size: file.size, files: vec![file].into(), recurse: dir.recurse, exist: Some(true) })
145-
}
146-
_ => {
147-
return Err("Path is not a file or directory")?;
148-
}
149-
}
150-
}
151-
}
152-
}
153-
15488
pub fn delete_file(file: &File) -> Result<(), Box<dyn std::error::Error>> {
15589
match compare_file_state(file) {
15690
Ok(f) => {

resources/filesys/src/main.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ use clap::Parser;
66
use std::process::exit;
77
use tracing::{debug, error};
88
use crate::config::{File, Directory, FileContent};
9-
use file_helper::{get_file, set_file, delete_file, export_file_path, export_dir_path};
9+
use file_helper::{get_file, set_file, delete_file, export_file_path};
10+
use dir_helpers::{get_dir, set_dir, delete_dir, export_dir_path};
1011
use schemars::schema_for;
1112

1213
mod args;
1314
pub mod config;
1415
mod file_helper;
16+
mod dir_helpers;
1517

1618
const EXIT_SUCCESS: i32 = 0;
1719
const EXIT_INVALID_INPUT: i32 = 2;
@@ -31,9 +33,9 @@ fn main() {
3133
None => {
3234
let dir = match is_directory_type(input.as_str()) {
3335
Some(dir) => {
34-
// let dir = get_file(&dir).unwrap();
35-
// let json = serde_json::to_string(&dir).unwrap();
36-
// println!("{}", json);
36+
let dir = get_dir(&dir).unwrap();
37+
let json = serde_json::to_string(&dir).unwrap();
38+
println!("{}", json);
3739
}
3840
None => {
3941
let filecontent = match is_fillecontent_type(input.as_str()) {
@@ -64,9 +66,9 @@ fn main() {
6466
None => {
6567
let dir = match is_directory_type(input.as_str()) {
6668
Some(dir) => {
67-
// let dir = delete_file(&dir).unwrap();
68-
// let json = serde_json::to_string(&dir).unwrap();
69-
// println!("{}", json);
69+
let dir = delete_dir(&dir).unwrap();
70+
let json = serde_json::to_string(&dir).unwrap();
71+
println!("{}", json);
7072
}
7173
None => {
7274
let filecontent = match is_fillecontent_type(input.as_str()) {
@@ -98,9 +100,9 @@ fn main() {
98100
None => {
99101
let dir = match is_directory_type(input.as_str()) {
100102
Some(dir) => {
101-
// let dir = get_file(&dir).unwrap();
102-
// let json = serde_json::to_string(&dir).unwrap();
103-
// println!("{}", json);
103+
let dir = set_dir(&dir).unwrap();
104+
let json = serde_json::to_string(&dir).unwrap();
105+
println!("{}", json);
104106
}
105107
None => {
106108
let filecontent = match is_fillecontent_type(input.as_str()) {

0 commit comments

Comments
 (0)