使用Augeas创建或更新XML文件中的部分 [英] Create or update a section in XML file with Augeas

查看:100
本文介绍了使用Augeas创建或更新XML文件中的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新XML配置文件中的一个部分,或者如果尚未使用Augeas来添加一个新的部分.

I would like to update a section in an XML config file or add a new one if does not exist already using Augeas.

XML文件如下所示:

The XML file looks like this:

<?xml version="1.0"?>
<security>
          <users>
            ...
            <user>
              <id>deployment</id>
              <firstName>Deployment</firstName>
              <lastName>User</lastName>
              <password>somepasshere</password>
              <status>active</status>
              <email>changeme1@yourcompany.com</email>
            </user>
          </users>
</security>

如果ID已经存在,我想更新姓氏/名字/电子邮件,如果是新ID,我想添加一个新的用户部分.

I would like to update the last name/first name/email if the ID exists already or add a new user section if it's a new ID.

在AugTool中,我使用:

In AugTool I use:

augtool> set /augeas/load/Xml/lens Xml.lns
augtool> set /augeas/load/Xml/incl /security.xml
augtool> load

我仍在学习Augeas,所以这是我第一次尝试获得该节点:

I'm still learning Augeas, so this was my first try to get the node :

augtool> print /files/security.xml/security/users/user/*[ #text= 'deployment']

在用户中更新或创建新版用户的命令是什么?

What would be the command to update or create a new section user in users ?

谢谢!

推荐答案

首先,在最新的Augeas版本(> = 1.0.0)中,可以使用--transform设置转换.假设文件是​​./users.xml:

First, with recent Augeas versions (>= 1.0.0), you can use --transform to set up the transformation. Let's say the file is ./users.xml:

$ augtool -r . --noautoload --transform "Xml.lns incl /users.xml"
augtool> defnode user /files/users.xml/security/users/user[id/#text="deployment"] # Create a new user entry if it doesn't exist yet, assign node to the "user" variable
augtool> set $user/id/#text "deployment"         # Set id if node was just created
augtool> set $user/firstName/#text "Deployment"  # Set firstName
augtool> set $user/lastName/#text "User"         # Set lastName
augtool> set $user/email/#text "changeme1@yourcompany.com"  # set email
...
augtool> save
Saved 1 file(s)

您甚至可以将其转换为脚本,例如user.augtool:

You can even turn this into a script, say user.augtool:

#!/usr/bin/augtool -sf
defnode user /files/users.xml/security/users/user[id/#text="deployment"]
set $user/id/#text "deployment"
set $user/firstName/#text "Deployment"
set $user/lastName/#text "User"
set $user/email/#text "changeme1@yourcompany.com"

然后您可以启动:

$ chmod +x user.augtool
$ ./user.augtool --transform "Xml.lns incl /users.xml" -r .
Saved 1 file(s)

这篇关于使用Augeas创建或更新XML文件中的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆