| 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/luci_config/common/cfgtypes" |
| 15 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 16 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 16 ) | 17 ) |
| 17 | 18 |
| 18 // LoadFile loads a File by configSet and path. | 19 // LoadFile loads a File by configSet and path. |
| 19 func LoadFile(c context.Context, project, ref string) (file *File, vers string,
err error) { | 20 func LoadFile(c context.Context, project, ref string) (file *File, vers string,
err error) { |
| 20 » cfgSet := "projects/" + project | 21 » // If ref is "", this will be a standard project config set. |
| 21 » if ref != "" { | 22 » cfgSet := cfgtypes.RefConfigSet(cfgtypes.ProjectName(project), ref) |
| 22 » » cfgSet += "/" + ref | 23 |
| 23 » } | 24 » file = &File{} |
| 24 » templateData, err := config.GetConfig(c, cfgSet, "dm/quest_templates.cfg
", false) | 25 » var meta cfgclient.Meta |
| 25 » if err != nil { | 26 » if err = cfgclient.Get(c, cfgclient.AsService, cfgSet, "dm/quest_templat
es.cfg", textproto.Message(file), &meta); err != nil { |
| 26 return | 27 return |
| 27 } | 28 } |
| 28 » file = &File{} | 29 » 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() | 30 err = file.Normalize() |
| 35 return | 31 return |
| 36 } | 32 } |
| 37 | 33 |
| 38 // Render renders the specified template with the given parameters. | 34 // Render renders the specified template with the given parameters. |
| 39 func (f *File) Render(spec *templateproto.Specifier) (*dm.Quest_Desc, error) { | 35 func (f *File) Render(spec *templateproto.Specifier) (*dm.Quest_Desc, error) { |
| 40 t := f.Template[spec.TemplateName] | 36 t := f.Template[spec.TemplateName] |
| 41 params, err := t.Parameters.Render(spec.Params) | 37 params, err := t.Parameters.Render(spec.Params) |
| 42 if err != nil { | 38 if err != nil { |
| 43 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) | 39 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) |
| 44 } | 40 } |
| 45 distribParams, err := t.DistributorParameters.Render(spec.Params) | 41 distribParams, err := t.DistributorParameters.Render(spec.Params) |
| 46 if err != nil { | 42 if err != nil { |
| 47 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) | 43 return nil, fmt.Errorf("rendering %q: field distributor paramete
rs: %s", spec.TemplateName, err) |
| 48 } | 44 } |
| 49 return &dm.Quest_Desc{ | 45 return &dm.Quest_Desc{ |
| 50 DistributorConfigName: t.DistributorConfigName, | 46 DistributorConfigName: t.DistributorConfigName, |
| 51 Parameters: params, | 47 Parameters: params, |
| 52 DistributorParameters: distribParams, | 48 DistributorParameters: distribParams, |
| 53 Meta: t.Meta, | 49 Meta: t.Meta, |
| 54 }, nil | 50 }, nil |
| 55 } | 51 } |
| OLD | NEW |