Repository: comtrya/comtrya (language: rust)

Issue:
New Action: Create Directories



Discussion / hints:

Fix patch:
diff --git a/examples/directory/create.yaml b/examples/directory/create.yaml
new file mode 100644
index 00000000..733faec6
--- /dev/null
+++ b/examples/directory/create.yaml
@@ -0,0 +1,3 @@
+actions:
+  - action: directory.create
+    path: /some-directory
diff --git a/src/actions/directory/create.rs b/src/actions/directory/create.rs
new file mode 100644
index 00000000..5b89992f
--- /dev/null
+++ b/src/actions/directory/create.rs
@@ -0,0 +1,53 @@
+use crate::actions::Action;
+use crate::atoms::directory::Create as DirectoryCreateAtom;
+use crate::manifests::Manifest;
+use crate::steps::Step;
+use serde::{Deserialize, Serialize};
+use std::path::PathBuf;
+use tera::Context;
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+pub struct DirectoryCreate {
+    pub path: String,
+}
+
+impl Action for DirectoryCreate {
+    fn plan(&self, _: &Manifest, _context: &Context) -> Vec<Step> {
+        vec![Step {
+            atom: Box::new(DirectoryCreateAtom {
+                path: PathBuf::from(&self.path),
+            }),
+            initializers: vec![],
+            finalizers: vec![],
+        }]
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::actions::Actions;
+    use crate::manifests::Manifest;
+    use std::path::PathBuf;
+
+    fn get_manifest_dir() -> PathBuf {
+        std::env::current_dir()
+            .unwrap()
+            .join("examples")
+            .join("directory")
+    }
+
+    #[test]
+    fn it_can_be_deserialized() {
+        let example_yaml = std::fs::File::open(get_manifest_dir().join("create.yaml")).unwrap();
+        let mut manifest: Manifest = serde_yaml::from_reader(example_yaml).unwrap();
+
+        match manifest.actions.pop() {
+            Some(Actions::DirectoryCreate(action)) => {
+                assert_eq!("/some-directory", action.path);
+            }
+            _ => {
+                panic!("DirectoryCopy didn't deserialize to the correct type");
+            }
+        };
+    }
+}
diff --git a/src/actions/directory/mod.rs b/src/actions/directory/mod.rs
index 6303e7ef..8ce27777 100644
--- a/src/actions/directory/mod.rs
+++ b/src/actions/directory/mod.rs
@@ -1,8 +1,11 @@
-pub mod copy;
-use std::path::PathBuf;
-
 use crate::{actions::Action, manifests::Manifest};
 use anyhow::{anyhow, Result};
+use std::path::PathBuf;
+
+mod copy;
+mod create;
+pub use copy::DirectoryCopy;
+pub use create::DirectoryCreate;
 
 pub trait DirectoryAction: Action {
     fn resolve(&self, manifest: &Manifest, path: &str) -> Result<PathBuf> {
diff --git a/src/actions/file/copy.rs b/src/actions/file/copy.rs
index 5d05c77f..1269cb3b 100644
--- a/src/actions/file/copy.rs
+++ b/src/actions/file/copy.rs
@@ -68,7 +68,6 @@ impl Action for FileCopy {
             }
         };
 
-        use crate::atoms::command::Exec;
         use crate::atoms::directory::Create as DirCreate;
         use crate::atoms::file::{Chmod, Create, SetContents};
 
diff --git a/src/actions/file/link.rs b/src/actions/file/link.rs
index 2530d2e6..c1e65099 100644
--- a/src/actions/file/link.rs
+++ b/src/actions/file/link.rs
@@ -22,7 +22,6 @@ impl FileAction for FileLink {}
 
 impl Action for FileLink {
     fn plan(&self, manifest: &Manifest, _: &Context) -> Vec<Step> {
-        use crate::atoms::command::Exec;
         use crate::atoms::directory::Create as DirCreate;
         use crate::atoms::file::Link;
 
diff --git a/src/actions/mod.rs b/src/actions/mod.rs
index 24c87dd4..47bf4a25 100644
--- a/src/actions/mod.rs
+++ b/src/actions/mod.rs
@@ -6,7 +6,7 @@ mod package;
 use crate::manifests::Manifest;
 use crate::steps::Step;
 use command::run::RunCommand;
-use directory::copy::DirectoryCopy;
+use directory::{DirectoryCopy, DirectoryCreate};
 use file::copy::FileCopy;
 use file::link::FileLink;
 use package::install::PackageInstall;
@@ -18,12 +18,19 @@ use tera::Context;
 pub enum Actions {
     #[serde(alias = "command.run", alias = "cmd.run")]
     CommandRun(RunCommand),
+
     #[serde(alias = "directory.copy", alias = "dir.copy")]
     DirectoryCopy(DirectoryCopy),
+
+    #[serde(alias = "directory.create", alias = "dir.create")]
+    DirectoryCreate(DirectoryCreate),
+
     #[serde(alias = "file.copy")]
     FileCopy(FileCopy),
+
     #[serde(alias = "file.link")]
     FileLink(FileLink),
+
     #[serde(alias = "package.install", alias = "package.installed")]
     PackageInstall(PackageInstall),
 }
@@ -33,6 +40,7 @@ impl Actions {
         match self {
             Actions::CommandRun(a) => a,
             Actions::DirectoryCopy(a) => a,
+            Actions::DirectoryCreate(a) => a,
             Actions::FileCopy(a) => a,
             Actions::FileLink(a) => a,
             Actions::PackageInstall(a) => a,


Test patch: