-
Notifications
You must be signed in to change notification settings - Fork 351
/
compose.go
33 lines (29 loc) · 898 Bytes
/
compose.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
package gs
import (
"fmt"
)
const MaxPartsInCompose = 32
type ComposeFunc func(target string, parts []string) error
func ComposeAll(target string, parts []string, composeFunc ComposeFunc) error {
for layer := 1; len(parts) > MaxPartsInCompose; layer++ {
var nextParts []string
for i := 0; i < len(parts); i += MaxPartsInCompose {
chunkSize := len(parts) - i
if chunkSize > MaxPartsInCompose {
chunkSize = MaxPartsInCompose
}
chunk := parts[i : i+chunkSize]
if chunkSize == 1 || (chunkSize < MaxPartsInCompose && len(nextParts)+chunkSize <= MaxPartsInCompose) {
nextParts = append(nextParts, chunk...)
} else {
targetName := fmt.Sprintf("%s_%d", chunk[0], layer)
if err := composeFunc(targetName, chunk); err != nil {
return err
}
nextParts = append(nextParts, targetName)
}
}
parts = nextParts
}
return composeFunc(target, parts)
}