TODO: Can we extract the baseOpts from the object interface?
(templateName string, outputFile string, object Options)
| 271 | |
| 272 | // TODO: Can we extract the baseOpts from the object interface? |
| 273 | func (s *Base) WriteTemplate(templateName string, outputFile string, object Options) error { |
| 274 | if object.GetBaseOpts().NoTemplates == "true" { |
| 275 | log.Debugln("We are not (re-)creating templates since no_templates has been set to true, moving on.") |
| 276 | return nil |
| 277 | } |
| 278 | |
| 279 | log.WithFields(log.Fields{ |
| 280 | "plugin": s.Name, |
| 281 | "templateName": templateName, |
| 282 | "outputFile": outputFile, |
| 283 | }).Debug("Writing template") |
| 284 | |
| 285 | data, err := Asset(templateName) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | |
| 290 | tmpl, err := template.New(templateName).Parse(string(data[:])) |
| 291 | |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | |
| 296 | if _, err := os.Stat(outputFile); err == nil { |
| 297 | log.Debugf("File %s already exists, renaming it for back-up purposes.", outputFile) |
| 298 | err := os.Rename(outputFile, fmt.Sprintf("%s.backup", outputFile)) |
| 299 | if err != nil { |
| 300 | log.Warningln("Could not rename file, trying to create the new file anyway. Error:", err) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | file, err := os.Create(outputFile) |
| 305 | if err != nil { |
| 306 | return err |
| 307 | } |
| 308 | |
| 309 | err = tmpl.Execute(file, object) |
| 310 | if err != nil { |
| 311 | return err |
| 312 | } |
| 313 | |
| 314 | return nil |
| 315 | } |
| 316 | |
| 317 | func (self *Base) RegisterRPC(server *rpc.Server) { |
| 318 | server.Register(self) |