aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2012-04-28 01:04:59 +0400
committerIgor Pashev <pashev.igor@gmail.com>2012-04-28 01:04:59 +0400
commitb7051553920006266ecce46b02c1ffe467d066cd (patch)
tree7949367e3a8affec54213234cf8aa6a531045cd0
parent6755d4cfd56682ba0455b285cdb573fb5649be7d (diff)
downloadnode-augeas-b7051553920006266ecce46b02c1ffe467d066cd.tar.gz
example: async reading and async writing of /etc/hosts
-rw-r--r--examples/async-write-etc-hosts.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/examples/async-write-etc-hosts.js b/examples/async-write-etc-hosts.js
new file mode 100644
index 0000000..70640ab
--- /dev/null
+++ b/examples/async-write-etc-hosts.js
@@ -0,0 +1,29 @@
+var aug = require('../build/Release/libaugeas');
+
+// NOTE:
+// running this example as non-root user will probably fail,
+// but for the goodness sake we set AUG_SAVE_NEWFILE flag here,
+// so your /etc/hosts will be kept untouched :-)
+aug.createAugeas(
+ {lens: 'hosts', incl: '/etc/hosts', flags:aug.AUG_SAVE_NEWFILE},
+ function(aug) {
+ if (!aug.error()) {
+ console.log('Making changes ...');
+ aug.set('/files/etc/hosts/1/ipaddr', '127.0.0.2');
+ aug.set('/files/etc/hosts/1/canonical', 'localhost');
+ aug.save(function(success) {
+ if (success) {
+ console.log('Saved! Take a look at /etc/hosts.augnew');
+ } else {
+ console.log('Failed :-( Here is the reason:');
+ console.log(aug.get('/augeas/files/etc/hosts/error/message'));
+ }
+ });
+ console.log('Saving started!');
+ } else {
+ console.log('Sad, but true :-(');
+ }
+ }
+);
+console.log('Waiting for Augeas ...');
+