This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
/
garden_map.go
196 lines (170 loc) · 5.17 KB
/
garden_map.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package config
import (
"errors"
"fmt"
"log"
"path"
"strings"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/sliceutil"
"gopkg.in/yaml.v2"
)
// PlantVarsMap ...
type PlantVarsMap map[string]string
// PlantModel ...
type PlantModel struct {
Path string `json:"path" yaml:"path"`
Seed string `json:"seed" yaml:"seed"`
Vars PlantVarsMap `json:"vars" yaml:"vars"`
Zones []string `json:"zones" yaml:"zones"`
}
// ZoneModel ..
type ZoneModel struct {
Vars PlantVarsMap `json:"vars" yaml:"vars"`
}
// PlantsMap ...
type PlantsMap map[string]PlantModel
// GardenMapModel ...
type GardenMapModel struct {
Plants map[string]PlantModel `json:"plants" yaml:"plants"`
Zones map[string]ZoneModel `json:"zones" yaml:"zones"`
}
// ExpandedPath ...
func (plant PlantModel) ExpandedPath(plantID string) string {
return strings.Replace(plant.Path, "$_GARDEN_PLANT_ID", plantID, -1)
}
// CollectAllVarsForPlant ...
// collects all the Vars for a plant, including the ones defined for
// the plant's zones
// In case a variable is defined in multiple Zones or in a Zone and in the Plant
// as well: Plant's Vars will always be the #1 priority, no matter whether
// it's also defined in a Zone or not.
// In case of different Zones, the last one in the Plant's Zones list
// will be the one which's Var will be used, it'll overwrite other
// zones' previously defined Vars for the same key.
func (gardenMap GardenMapModel) CollectAllVarsForPlant(plantID string) (PlantVarsMap, error) {
allVars := PlantVarsMap{}
plantModel, isFound := gardenMap.Plants[plantID]
if !isFound {
return PlantVarsMap{}, fmt.Errorf("Failed to find Plant with ID: %s", plantID)
}
for _, aZoneID := range plantModel.Zones {
zoneModel, isFound := gardenMap.Zones[aZoneID]
if !isFound {
// no Zone specific data/vars
continue
}
for k, v := range zoneModel.Vars {
allVars[k] = v
}
}
for k, v := range plantModel.Vars {
allVars[k] = v
}
return allVars, nil
}
// FilteredPlants ...
func (gardenMap GardenMapModel) FilteredPlants(plantID, zone string) PlantsMap {
if plantID != "" {
plantModel, isFound := gardenMap.Plants[plantID]
if isFound {
return PlantsMap{
plantID: plantModel,
}
}
// not found by ID
return PlantsMap{}
}
if zone != "" {
return gardenMap.plantsFilteredByZone(zone)
}
// no filter
return gardenMap.Plants
}
// FilteredPlantsIDs ...
func (gardenMap GardenMapModel) FilteredPlantsIDs(plantID, zone string) []string {
filteredPlants := gardenMap.FilteredPlants(plantID, zone)
ids := []string{}
for aPlantID := range filteredPlants {
ids = append(ids, aPlantID)
}
return ids
}
func (gardenMap GardenMapModel) plantsFilteredByZone(zone string) PlantsMap {
filtered := PlantsMap{}
for plantID, plantModel := range gardenMap.Plants {
if sliceutil.IndexOfStringInSlice(zone, plantModel.Zones) >= 0 {
filtered[plantID] = plantModel
}
}
return filtered
}
func checkGardenDirPath(relPth string) (string, string, error) {
absPth, err := pathutil.AbsPath(relPth)
if err != nil {
return "", "", err
}
isEx, err := pathutil.IsDirExists(absPth)
if err != nil {
return "", "", err
}
if !isEx {
return "", "", nil
}
return relPth, absPth, nil
}
// FindGardenDirPath ...
func FindGardenDirPath() (string, string, error) {
relPth := "./.garden"
if _, absPth, err := checkGardenDirPath(relPth); err == nil && absPth != "" {
return relPth, absPth, nil
}
relPth = "~/.garden"
if _, absPth, err := checkGardenDirPath(relPth); err == nil && absPth != "" {
return relPth, absPth, nil
}
return "", "", errors.New("Can't find Garden directory at standard paths")
}
// CreateGardenMapModelFromYMLFile ...
func CreateGardenMapModelFromYMLFile(pth string) (GardenMapModel, error) {
fileBytes, err := fileutil.ReadBytesFromFile(pth)
if err != nil {
return GardenMapModel{}, err
}
var modelToReturn GardenMapModel
if err := yaml.Unmarshal(fileBytes, &modelToReturn); err != nil {
return GardenMapModel{}, err
}
return modelToReturn, nil
}
// LoadGardenMap ..
// gardenDirPath is optional, if provided will be used as the Garden Dir path
// if not provided the standard .garden dir paths will be checked
func LoadGardenMap(gardenDirPath string) (GardenMapModel, string, error) {
relPath := ""
absPath := ""
if gardenDirPath != "" {
relPath = gardenDirPath
apth, err := pathutil.AbsPath(gardenDirPath)
if err != nil {
return GardenMapModel{}, "", fmt.Errorf("Failed to get Absolute path of provided Garden Dir (path:%s), error: %s", gardenDirPath, err)
}
absPath = apth
} else {
rpth, apth, err := FindGardenDirPath()
if err != nil {
return GardenMapModel{}, "", fmt.Errorf("Failed to find Garden directory: %s", err)
}
relPath = rpth
absPath = apth
}
log.Printf("=> Using Garden directory: %s (abs path: %s)", colorstring.Green(relPath), absPath)
gardenMapPth := path.Join(absPath, "map.yml")
gardenMap, err := CreateGardenMapModelFromYMLFile(gardenMapPth)
if err != nil {
return GardenMapModel{}, "", fmt.Errorf("Failed to load Garden Map (path:%s) with error: %s", gardenMapPth, err)
}
return gardenMap, absPath, nil
}