-
-
Save nvgoldin/2a613afb59542fc28dd920e1da2f97e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<users /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 "[email protected]" | |
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 = "[email protected]" | |
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>[email protected]</email> | |
+</user> | |
+</users> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 "[email protected]" | |
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>[email protected]</email> | |
+</user> | |
+<user><username>foo</username> | |
+<email>[email protected]</email> | |
+</user> | |
+</users> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 = "[email protected]" | |
/files/users.xml/users/user/#text[3] = " " |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<users> | |
<user> | |
<username>blah</username> | |
<email>[email protected]</email> | |
</user> | |
</users> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => '[email protected]', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment