Repository: comtrya/comtrya (language: rust)

Issue:
New Action: macOS Defaults
https://macos-defaults.com/


Discussion / hints:

Fix patch:
diff --git a/examples/macos/defaults.yaml b/examples/macos/defaults.yaml
new file mode 100644
index 00000000..404d8bdc
--- /dev/null
+++ b/examples/macos/defaults.yaml
@@ -0,0 +1,18 @@
+actions:
+  - action: macos.default
+    domain: com.apple.dock
+    key: orientation
+    kind: string
+    value: right
+
+  - action: macos.default
+    domain: com.apple.screencapture
+    key: include-date
+    kind: bool
+    value: "false"
+
+  - action: macos.default
+    domain: NSGlobalDomain
+    key: "NSTableViewDefaultSizeMode"
+    kind: int
+    value: "1"
diff --git a/src/actions/macos/default.rs b/src/actions/macos/default.rs
new file mode 100644
index 00000000..6ef09d40
--- /dev/null
+++ b/src/actions/macos/default.rs
@@ -0,0 +1,36 @@
+use crate::atoms::command::Exec;
+use crate::contexts::Contexts;
+use crate::steps::Step;
+use crate::{actions::Action, manifests::Manifest};
+use serde::{Deserialize, Serialize};
+
+// I went through all the examples here: https://macos-defaults.com/
+// and while arrays and dictionaries are valid values, I couldn't
+// find any usable examples. So omitting for now
+#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
+pub struct MacOSDefault {
+    pub domain: String,
+    pub key: String,
+    pub kind: String,
+    pub value: String,
+}
+
+impl Action for MacOSDefault {
+    fn plan(&self, _: &Manifest, _: &Contexts) -> Vec<Step> {
+        vec![Step {
+            atom: Box::new(Exec {
+                command: String::from("defaults"),
+                arguments: vec![
+                    String::from("write"),
+                    self.domain.clone(),
+                    self.key.clone(),
+                    format!("-{}", self.kind),
+                    self.value.clone(),
+                ],
+                ..Default::default()
+            }),
+            initializers: vec![],
+            finalizers: vec![],
+        }]
+    }
+}
diff --git a/src/actions/macos/mod.rs b/src/actions/macos/mod.rs
new file mode 100644
index 00000000..56846bb7
--- /dev/null
+++ b/src/actions/macos/mod.rs
@@ -0,0 +1,2 @@
+mod default;
+pub use default::MacOSDefault;
diff --git a/src/actions/mod.rs b/src/actions/mod.rs
index 292874e5..3a000b8e 100644
--- a/src/actions/mod.rs
+++ b/src/actions/mod.rs
@@ -2,6 +2,7 @@ mod command;
 mod directory;
 mod file;
 mod git;
+mod macos;
 mod package;
 
 use crate::contexts::{to_koto, Contexts};
@@ -14,6 +15,7 @@ use file::download::FileDownload;
 use file::link::FileLink;
 use git::GitClone;
 use koto::{Koto, KotoSettings};
+use macos::MacOSDefault;
 use package::install::PackageInstall;
 use serde::{Deserialize, Serialize};
 
@@ -128,6 +130,9 @@ pub enum Actions {
     #[serde(alias = "git.clone")]
     GitClone(ConditionalVariantAction<GitClone>),
 
+    #[serde(alias = "macos.default")]
+    MacOSDefault(ConditionalVariantAction<MacOSDefault>),
+
     #[serde(alias = "package.install", alias = "package.installed")]
     PackageInstall(ConditionalVariantAction<PackageInstall>),
 }
@@ -142,6 +147,7 @@ impl Actions {
             Actions::FileDownload(a) => a,
             Actions::FileLink(a) => a,
             Actions::GitClone(a) => a,
+            Actions::MacOSDefault(a) => a,
             Actions::PackageInstall(a) => a,
         }
     }


Test patch: