Repository: comtrya/comtrya (language: rust)

Issue:
Feature: add option to ignore user/group already exists + user already a member of group
**Description**
When running `user.add` and `user.group`, add a boolean option to treat user already exist and user already a group member as not a failure.

**Additional information**
It would be very useful to simply make sure that the user account exists with the specified details, and not fail if the user already exists. Likewise for creating groups and adding users to groups.

If the user already exists, but any of the attributes are different, Comtrya should make the changes.


Discussion / hints:
What you describe is how it should actually work, no extra Boolean required. 

Comtrya actions have plan phases to determine if it needs to run, but existing user checks are missing for the user providers.

Fix patch:
diff --git a/lib/src/actions/user/add.rs b/lib/src/actions/user/add.rs
index 7be13124..68c03a84 100644
--- a/lib/src/actions/user/add.rs
+++ b/lib/src/actions/user/add.rs
@@ -6,6 +6,9 @@ use crate::manifests::Manifest;
 use crate::steps::Step;
 use std::ops::Deref;
 
+#[cfg(unix)]
+use tracing::debug;
+
 pub type UserAdd = User;
 
 impl Action for UserAdd {
@@ -16,6 +19,17 @@ impl Action for UserAdd {
 
         let mut atoms: Vec<Step> = vec![];
 
+        if variant.username.is_empty() {
+            return Ok(atoms);
+        }
+
+        #[cfg(unix)]
+        match uzers::get_user_by_name(&variant.username) {
+            Some(_user) => debug!(message = "User already exists", username = ?variant.username),
+            None => atoms.append(&mut provider.add_user(&variant)?),
+        }
+
+        #[cfg(not(unix))]
         atoms.append(&mut provider.add_user(&variant)?);
 
         Ok(atoms)


Test patch: