forked from evergreen-ci/evergreen
/
project_vars.go
64 lines (56 loc) · 1.37 KB
/
project_vars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package model
import (
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/db/bsonutil"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
ProjectVarIdKey = bsonutil.MustHaveTag(ProjectVars{}, "Id")
ProjectVarsMapKey = bsonutil.MustHaveTag(ProjectVars{}, "Vars")
)
const (
ProjectVarsCollection = "project_vars"
)
//ProjectVars holds a map of variables specific to a given project.
//They can be fetched at run time by the agent, so that settings which are
//sensitive or subject to frequent change don't need to be hard-coded into
//yml files.
type ProjectVars struct {
//Should match the _id in the project it refers to
Id string `bson:"_id" json:"_id"`
//The actual mapping of variables for this project
Vars map[string]string `bson:"vars" json:"vars"`
}
func FindOneProjectVars(projectId string) (*ProjectVars, error) {
projectVars := &ProjectVars{}
err := db.FindOne(
ProjectVarsCollection,
bson.M{
ProjectVarIdKey: projectId,
},
db.NoProjection,
db.NoSort,
projectVars,
)
if err == mgo.ErrNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
return projectVars, nil
}
func (projectVars *ProjectVars) Upsert() (*mgo.ChangeInfo, error) {
return db.Upsert(
ProjectVarsCollection,
bson.M{
ProjectVarIdKey: projectVars.Id,
},
bson.M{
"$set": bson.M{
ProjectVarsMapKey: projectVars.Vars,
},
},
)
}