Skip to content

Instantly share code, notes, and snippets.

@nvgoldin
Forked from raphink/adduser.sh
Created June 16, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvgoldin/2a613afb59542fc28dd920e1da2f97e0 to your computer and use it in GitHub Desktop.
Save nvgoldin/2a613afb59542fc28dd920e1da2f97e0 to your computer and use it in GitHub Desktop.
#!/bin/bash
FILE=$1
USER=$2
EMAIL=$3
realfile=$(readlink -f $FILE)
cat << EOF | augtool -Ast "Xml incl ${realfile}"
# Remove #empty node
clear /files${realfile}/users
defnode user /files${realfile}/users/user[username/#text = "${USER}"]
set \$user/username/#text "${USER}"
set \$user/email/#text "${EMAIL}"
EOF
$ augtool -r . -At "Xml incl /empty.xml"
augtool> clear /files/empty.xml/users
users
augtool> clear /files/empty.xml/users
augtool> set /files/empty.xml/users/user/username/#text "blah"
augtool> set /files/empty.xml/users/user/email/#text "blah@example.com"
augtool> print /files/empty.xml/
/files/empty.xml
/files/empty.xml/users
/files/empty.xml/users/user
/files/empty.xml/users/user/username
/files/empty.xml/users/user/username/#text = "blah"
/files/empty.xml/users/user/email
/files/empty.xml/users/user/email/#text = "blah@example.com"
augtool> save
Saved 1 file(s)
$ git diff empty.xml
diff --git a/empty.xml b/empty.xml
index 8909974..9979def 100644
--- a/empty.xml
+++ b/empty.xml
@@ -1 +1,4 @@
-<users />
\ No newline at end of file
+<users><user><username>blah</username>
+<email>blah@example.com</email>
+</user>
+</users>
$ augtool -r . -At "Xml incl /empty.xml"
augtool> defnode foo /files/empty.xml/users/user[username/#text = "foo"]
augtool> set $foo/username/#text "foo"
augtool> set $foo/email/#text "foo@example.com"
augtool> save
Saved 1 file(s)
$ git diff empty.xml
diff --git a/empty.xml b/empty.xml
index 8909974..8fd2881 100644
--- a/empty.xml
+++ b/empty.xml
@@ -1 +1,7 @@
-<users />
\ No newline at end of file
+<users><user><username>blah</username>
+<email>blah@example.com</email>
+</user>
+<user><username>foo</username>
+<email>foo@example.com</email>
+</user>
+</users>
$ augtool -r . -At "Xml incl /empty.xml"
augtool> print /files/empty.xml
/files/home/rpinson/bas/augeas/empty.xml
/files/home/rpinson/bas/augeas/empty.xml/users = "#empty"
$ augtool -r . -At "Xml incl /users.xml"
augtool> print /files//users.xml
/files/users.xml
/files/users.xml/users
/files/users.xml/users/#text = "\n "
/files/users.xml/users/user
/files/users.xml/users/user/#text[1] = "\n "
/files/users.xml/users/user/username
/files/users.xml/users/user/username/#text = "blah"
/files/users.xml/users/user/#text[2] = " "
/files/users.xml/users/user/email
/files/users.xml/users/user/email/#text = "blah@example.com"
/files/users.xml/users/user/#text[3] = " "
<users>
<user>
<username>blah</username>
<email>blah@example.com</email>
</user>
</users>
define xml_user (
$file,
$user,
$email,
$ensure = 'present',
) {
case $ensure {
'present': {
$changes = [
"clear users",
"defnode user users/user[username/#text = '${user}'] ''",
"set \$user/username/#text '${user}'",
"set \$user/email/#text '${email}'",
]
}
'absent': {
$changes = "rm users/user[username/#text = '${user}']"
}
default: {
fail("Unknown value for \$ensure '${ensure}'")
}
}
augeas { "manage user ${user} in ${file}":
lens => 'Xml.lns',
incl => $file,
context => "/files${file}",
changes => $changes,
}
}
xml_user { 'foo':
file => '/home/rpinson/bas/augeas/d34795066930bcd46d1000cd7a0ecf02/empty.xml',
user => 'foo',
email => 'foo@example.com',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment