Skip to content

Commit 3b6a651

Browse files
File content resource
1 parent 9f3c895 commit 3b6a651

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

dsc/examples/filesys_filecontent.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Example configuration mixing native app resources with classic PS resources
2+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
3+
resources:
4+
- name: Set file content
5+
type: Microsoft.DSC/FileContent
6+
properties:
7+
path: "[path('d:\\', 'temp', 'a.txt')]"
8+
content: "Hello, World!"
9+
_exist: true

resources/filesys/filecontent.dsc.resource.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
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+
},
2940
"schema": {
3041
"command": {
3142
"executable": "filesys",

resources/filesys/src/filecontent_helper.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::config::{FileContent, Encoding};
55
use std::path::Path;
66
use std::io;
7+
use std::io::Write;
78
use tracing::{debug};
89

910
impl Encoding {
@@ -48,16 +49,35 @@ pub fn get_file_content(filecontent: &FileContent) -> Result<FileContent, Box<dy
4849
}
4950

5051
pub fn set_file_content(filecontent: &FileContent) -> Result<FileContent, Box<dyn std::error::Error>> {
51-
// debug!("In set_file_content");
52-
// let path = Path::new(&filecontent.path);
53-
// let content = filecontent.content.as_ref().unwrap_or(&String::new());
54-
// let encoding = filecontent.encoding.unwrap_or(Encoding::Utf8).to_encoding_rs().unwrap_or(encoding_rs::UTF_8);
55-
// let mut file = fsFile::create(path)?;
52+
debug!("In set_file_content");
53+
let path = Path::new(&filecontent.path);
54+
//let content = filecontent.content.as_ref().unwrap_or(&String::new());
55+
//let encoding = filecontent.encoding.unwrap_or(Encoding::Utf8).to_encoding_rs().unwrap_or(encoding_rs::UTF_8);
56+
let file_expected_exists = filecontent.exist.unwrap_or(true);
57+
58+
if path.exists() && !file_expected_exists {
59+
std::fs::remove_file(path)?;
60+
return Ok(filecontent.clone())
61+
}
62+
else if !path.exists() && !file_expected_exists {
63+
return Ok(filecontent.clone())
64+
}
65+
66+
let mut file = std::fs::File::create(path)?;
67+
5668
// let mut encoder = encoding.new_encoder();
5769
// let mut bytes = vec![0; content.len() * encoding.new_encoder().max_buffer_length()];
5870
// let (bytes_written, _, _) = encoder.encode_to_slice(content, &mut bytes, true);
5971
// file.write_all(&bytes[..bytes_written])?;
60-
// Ok(filecontent.clone())
72+
73+
match &filecontent.content {
74+
Some(content) => {
75+
file.write_all(content.as_bytes())?;
76+
},
77+
None => {
78+
}
79+
}
80+
6181
Ok(filecontent.clone())
6282
}
6383

@@ -126,7 +146,9 @@ pub fn compare_filecontent_state(filecontent: &FileContent) -> Result<FileConten
126146
else {
127147
match filecontent.exist {
128148
Some(true) | None => {
129-
return Err("File does not exist")?;
149+
let mut updated_file_content = filecontent.clone();
150+
updated_file_content.exist = Some(false);
151+
return Ok(updated_file_content);
130152
},
131153
Some(false) => {
132154
return Ok(filecontent.clone());

resources/filesys/src/main.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tracing::{debug, error};
88
use crate::config::{File, Directory, FileContent};
99
use file_helper::{get_file, set_file, delete_file, export_file_path};
1010
use dir_helpers::{get_dir, set_dir, delete_dir, export_dir_path};
11-
use filecontent_helper::{get_file_content, delete_file_content};
11+
use filecontent_helper::{get_file_content, set_file_content, delete_file_content};
1212
use schemars::schema_for;
1313

1414
mod args;
@@ -40,7 +40,7 @@ fn main() {
4040
println!("{}", json);
4141
}
4242
None => {
43-
let filecontent = match is_fillecontent_type(input.as_str()) {
43+
let filecontent = match is_filecontent_type(input.as_str()) {
4444
Some(filecontent) => {
4545
let filecontent = get_file_content(&filecontent).unwrap();
4646
let json = serde_json::to_string(&filecontent).unwrap();
@@ -73,7 +73,7 @@ fn main() {
7373
println!("{}", json);
7474
}
7575
None => {
76-
let filecontent = match is_fillecontent_type(input.as_str()) {
76+
let filecontent = match is_filecontent_type(input.as_str()) {
7777
Some(filecontent) => {
7878
let filecontent = delete_file_content(&filecontent).unwrap();
7979
let json = serde_json::to_string(&filecontent).unwrap();
@@ -107,11 +107,11 @@ fn main() {
107107
println!("{}", json);
108108
}
109109
None => {
110-
let filecontent = match is_fillecontent_type(input.as_str()) {
110+
let filecontent = match is_filecontent_type(input.as_str()) {
111111
Some(filecontent) => {
112-
// let filecontent = get_file(&filecontent).unwrap();
113-
// let json = serde_json::to_string(&filecontent).unwrap();
114-
// println!("{}", json);
112+
let filecontent = set_file_content(&filecontent).unwrap();
113+
let json = serde_json::to_string(&filecontent).unwrap();
114+
println!("{}", json);
115115
}
116116
None => {
117117
error!("Invalid input.");
@@ -142,11 +142,11 @@ fn main() {
142142
debug!("File exported successfully.");
143143
}
144144
None => {
145-
let filecontent = match is_fillecontent_type(input.as_str()) {
145+
let filecontent = match is_filecontent_type(input.as_str()) {
146146
Some(filecontent) => {
147-
// let filecontent = get_file(&filecontent).unwrap();
148-
// let json = serde_json::to_string(&filecontent).unwrap();
149-
// println!("{}", json);
147+
let filecontent = get_file_content(&filecontent).unwrap();
148+
let json = serde_json::to_string(&filecontent).unwrap();
149+
println!("{}", json);
150150
}
151151
None => {
152152
error!("Invalid input.");
@@ -200,7 +200,7 @@ fn is_directory_type(input: &str) -> Option<Directory> {
200200
Some(dir)
201201
}
202202

203-
fn is_fillecontent_type(input: &str) -> Option<FileContent> {
203+
fn is_filecontent_type(input: &str) -> Option<FileContent> {
204204
let filecontent: FileContent = match serde_json::from_str(input) {
205205
Ok(input) => input,
206206
Err(_) => return None,

0 commit comments

Comments
 (0)