-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
archivefiles.go
74 lines (64 loc) · 1.73 KB
/
archivefiles.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
// Package archivefiles can evaluate a list of config.Files into their final form.
package archivefiles
import (
"fmt"
"path/filepath"
"sort"
"github.com/apex/log"
"github.com/goreleaser/fileglob"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
)
// Eval evaluates the given list of files to their final form.
func Eval(template *tmpl.Template, files []config.File) ([]config.File, error) {
var result []config.File
for _, f := range files {
replaced, err := template.Apply(f.Source)
if err != nil {
return result, fmt.Errorf("failed to apply template %s: %w", f.Source, err)
}
files, err := fileglob.Glob(replaced)
if err != nil {
return result, fmt.Errorf("globbing failed for pattern %s: %w", f.Source, err)
}
for _, file := range files {
result = append(result, config.File{
Source: file,
Destination: destinationFor(f, file),
Info: f.Info,
})
}
}
sort.Slice(result, func(i, j int) bool {
return result[i].Destination < result[j].Destination
})
return unique(result), nil
}
// remove duplicates
func unique(in []config.File) []config.File {
var result []config.File
exist := map[string]string{}
for _, f := range in {
if current := exist[f.Destination]; current != "" {
log.Warnf(
"file '%s' already exists in archive as '%s' - '%s' will be ignored",
f.Destination,
current,
f.Source,
)
continue
}
exist[f.Destination] = f.Source
result = append(result, f)
}
return result
}
func destinationFor(f config.File, path string) string {
if f.Destination == "" {
return path
}
if f.StripParent {
return filepath.Join(f.Destination, filepath.Base(path))
}
return filepath.Join(f.Destination, path)
}