-
Notifications
You must be signed in to change notification settings - Fork 584
/
order.go
106 lines (85 loc) · 1.93 KB
/
order.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
package cnbutils
import (
"bytes"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
const DefaultOrderPath = "/cnb/order.toml"
type Order struct {
Order []OrderEntry `toml:"order"`
Utils BuildUtils `toml:"-"`
}
type OrderEntry struct {
Group []BuildPackMetadata `toml:"group" json:"group"`
}
func (o Order) Save(path string) error {
var buf bytes.Buffer
err := toml.NewEncoder(&buf).Encode(o)
if err != nil {
return err
}
err = o.Utils.FileWrite(path, buf.Bytes(), 0644)
if err != nil {
return err
}
return nil
}
func loadExistingOrder(utils BuildUtils) (Order, error) {
order := Order{
Utils: utils,
}
orderReader, err := utils.Open(DefaultOrderPath)
if err != nil {
return Order{}, err
}
defer orderReader.Close()
_, err = toml.NewDecoder(orderReader).Decode(&order)
if err != nil {
return Order{}, err
}
return order, nil
}
func newOrder(bpacks []string, utils BuildUtils) (Order, error) {
buildpacksMeta, err := GetMetadata(bpacks, utils)
if err != nil {
return Order{}, err
}
return Order{
Utils: utils,
Order: []OrderEntry{{
Group: buildpacksMeta,
}},
}, nil
}
func CreateOrder(bpacks, preBpacks, postBpacks []string, dockerCreds string, utils BuildUtils) (Order, error) {
if dockerCreds != "" {
os.Setenv("DOCKER_CONFIG", filepath.Dir(dockerCreds))
}
var order Order
var err error
if len(bpacks) == 0 {
order, err = loadExistingOrder(utils)
if err != nil {
return Order{}, err
}
} else {
order, err = newOrder(bpacks, utils)
if err != nil {
return Order{}, err
}
}
for idx := range order.Order {
preMetadata, err := GetMetadata(preBpacks, utils)
if err != nil {
return Order{}, err
}
postMetadata, err := GetMetadata(postBpacks, utils)
if err != nil {
return Order{}, err
}
order.Order[idx].Group = append(preMetadata, order.Order[idx].Group...)
order.Order[idx].Group = append(order.Order[idx].Group, postMetadata...)
}
return order, nil
}