【问题标题】:Groovy copy file, modify values and save itGroovy 复制文件,修改值并保存
【发布时间】:2021-05-31 11:28:24
【问题描述】:

我需要使用 Groovy 复制“模板”文件并用当前值替换一些文本(变量),然后将文件保存在新文件中。

bay_equipment.each{

  hname="Network-${it['hostname']}-${it['function']}" 
  nodeName="${it['hostname']}"
  nodeIP="${it['ip_address']}"
  fname="${it['hostname']}-${it['function']}-Network.cfg"
  

  def src= new File("../config-files/nagios-switch-bayg20.cfg")
  def dst= new File("../dest-files/$fname")
  dst << src.text
  

//一切顺利

//现在正在尝试打开 dst 文件并进行修改然后保存。

//这个字符串--需要替换为hname变量,同样替换为nodeIP

  dst = (dst =~ /<%=@deviceType%>-<%=@deviceGroup%>-<%=@nodeName%>/).replaceFirst("$hname")
  dst = (dst =~ /<%=@nodeIP%>/).replaceFirst("$nodeIP")
  
  dst.write(dst)
  
  
}

我在运行时遇到错误:

捕获:groovy.lang.MissingMethodException:没有方法签名:java.lang.String.write() 适用于参数类型:(字符串)值:[../dest-files/u100en00700-BAYG20-Network.配置文件] 可能的解决方案:wait()、wait(long)、with(groovy.lang.Closure)、trim()、size()、toSet() groovy.lang.MissingMethodException:没有方法签名:java.lang.String.write() 适用于参数类型:(字符串)值:[../dest-files/u100en00700-BAYG20-Network.cfg] 可能的解决方案:wait()、wait(long)、with(groovy.lang.Closure)、trim()、size()、toSet() 在 nsr_nagios$_run_closure4.doCall(nsr_nagios:53) 在 nsr_nagios.run(nsr_nagios:38)

【问题讨论】:

  • 你有什么问题?
  • 将替换内容和写入文件的部分分开(例如将源读入字符串,操作字符串,最后写入字符串)。到目前为止,您正在将“dst the file”转换为“dst the string”。

标签: groovy


【解决方案1】:

有关如何使用此功能的示例,请参见以下代码:

def equipment = [
  [hostname: 'foo',   function: "bar",   ip_address: '1.2.3.4'],
  [hostname: 'alice', function: "raven", ip_address: '5.6.7.8']
]

equipment.each {
  def binding = [
    hname:    "Network-${it['hostname']}-${it['function']}",
    nodeName: it.hostname,
    nodeIP:   it.ip_address,
    fname:    "${it['hostname']}-${it['function']}-Network.cfg"
  ]  

  def srcData  = new File("src-file.txt").text
  def template = new groovy.text.StreamingTemplateEngine().createTemplate(srcData)

  new File(binding.fname).text = template.make(binding)
}

给定以下源文件:

<%= hname %>-<%= nodeName %>-<%= nodeIP %>

将创建两个文件,都是模板化的。示例执行顺序:

─➤ ls -la 
total 24
drwxrwxr-x   2 mbjarland mbjarland  4096 Mar  4 17:55 .
drwxr-xr-x 112 mbjarland mbjarland 12288 Mar  4 17:47 ..
-rw-rw-r--   1 mbjarland mbjarland   634 Mar  4 17:55 solution.groovy
-rw-rw-r--   1 mbjarland mbjarland    42 Mar  4 17:55 src-file.txt

─➤ cat src-file.txt 
<%= hname %>-<%= nodeName %>-<%= nodeIP %>

─➤ groovy solution.groovy 

─➤ ls -la 
total 32
drwxrwxr-x   2 mbjarland mbjarland  4096 Mar  4 17:56 .
drwxr-xr-x 112 mbjarland mbjarland 12288 Mar  4 17:47 ..
-rw-rw-r--   1 mbjarland mbjarland    33 Mar  4 17:56 alice-raven-Network.cfg
-rw-rw-r--   1 mbjarland mbjarland    27 Mar  4 17:56 foo-bar-Network.cfg
-rw-rw-r--   1 mbjarland mbjarland   634 Mar  4 17:55 solution.groovy
-rw-rw-r--   1 mbjarland mbjarland    42 Mar  4 17:55 src-file.txt

─➤ cat foo-bar-Network.cfg 
Network-foo-bar-foo-1.2.3.4

─➤ cat alice-raven-Network.cfg 
Network-alice-raven-alice-5.6.7.8

─➤ 

其中模板引擎是 groovy built in class,用于执行此类操作。

【讨论】:

    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 2022-11-03
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多