| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package dmTemplate | 5 package dmTemplate |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/config" | |
| 13 "github.com/luci/luci-go/common/data/text/templateproto" | 12 "github.com/luci/luci-go/common/data/text/templateproto" |
| 14 "github.com/luci/luci-go/common/proto" | |
| 15 dm "github.com/luci/luci-go/dm/api/service/v1" | 13 dm "github.com/luci/luci-go/dm/api/service/v1" |
| 14 "github.com/luci/luci-go/server/config" |
| 15 "github.com/luci/luci-go/server/config/textproto" |
| 16 ) | 16 ) |
| 17 | 17 |
| 18 // LoadFile loads a File by configSet and path. | 18 // LoadFile loads a File by configSet and path. |
| 19 func LoadFile(c context.Context, project, ref string) (file *File, vers string,
err error) { | 19 func LoadFile(c context.Context, project, ref string) (file *File, vers string,
err error) { |
| 20 cfgSet := "projects/" + project | 20 cfgSet := "projects/" + project |
| 21 if ref != "" { | 21 if ref != "" { |
| 22 cfgSet += "/" + ref | 22 cfgSet += "/" + ref |
| 23 } | 23 } |
| 24 » templateData, err := config.GetConfig(c, cfgSet, "dm/quest_templates.cfg
", false) | 24 |
| 25 » if err != nil { | 25 » file = &File{} |
| 26 » var meta config.Meta |
| 27 » if err = config.Get(c, config.AsService, cfgSet, "dm/quest_templates.cfg
", textproto.Message(file), &meta); err != nil { |
| 26 return | 28 return |
| 27 } | 29 } |
| 28 » file = &File{} | 30 » vers = meta.ContentHash |
| 29 » vers = templateData.ContentHash | |
| 30 » err = proto.UnmarshalTextML(templateData.Content, file) | |
| 31 » if err != nil { | |
| 32 » » return | |
| 33 » } | |
| 34 err = file.Normalize() | 31 err = file.Normalize() |
| 35 return | 32 return |
| 36 } | 33 } |
| 37 | 34 |
| 38 // Render renders the specified template with the given parameters. | 35 // Render renders the specified template with the given parameters. |
| 39 func (f *File) Render(spec *templateproto.Specifier) (*dm.Quest_Desc, error) { | 36 func (f *File) Render(spec *templateproto.Specifier) (*dm.Quest_Desc, error) { |
| 40 t := f.Template[spec.TemplateName] | 37 t := f.Template[spec.TemplateName] |
| 41 params, err := t.Parameters.Render(spec.Params) | 38 params, err := t.Parameters.Render(spec.Params) |
| 42 if err != nil { | 39 if err != nil { |
| 43 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) | 40 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) |
| 44 } | 41 } |
| 45 distribParams, err := t.DistributorParameters.Render(spec.Params) | 42 distribParams, err := t.DistributorParameters.Render(spec.Params) |
| 46 if err != nil { | 43 if err != nil { |
| 47 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) | 44 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) |
| 48 } | 45 } |
| 49 return &dm.Quest_Desc{ | 46 return &dm.Quest_Desc{ |
| 50 DistributorConfigName: t.DistributorConfigName, | 47 DistributorConfigName: t.DistributorConfigName, |
| 51 Parameters: params, | 48 Parameters: params, |
| 52 DistributorParameters: distribParams, | 49 DistributorParameters: distribParams, |
| 53 Meta: t.Meta, | 50 Meta: t.Meta, |
| 54 }, nil | 51 }, nil |
| 55 } | 52 } |
| OLD | NEW |