/
artifacts.go
98 lines (83 loc) · 1.7 KB
/
artifacts.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
package runner
import (
"fmt"
"path/filepath"
"github.com/c-128/auroraci/internal/pipelines/artifacts"
"github.com/c-128/auroraci/internal/pipelines/logger"
"github.com/go-git/go-billy/v5"
)
func uploadArtifacts(
uploader artifacts.Uploader,
logger logger.Logger,
worktree billy.Filesystem,
artifacts []string,
) error {
err := artifactsDir(
uploader,
logger,
worktree,
artifacts,
worktree.Root(),
worktree.Root(),
)
if err != nil {
return err
}
return nil
}
func artifactsDir(
uploader artifacts.Uploader,
logger logger.Logger,
worktree billy.Filesystem,
artifacts []string,
dir string,
root string,
) error {
files, err := worktree.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
for _, file := range files {
fullPath := worktree.Join(dir, file.Name())
if file.IsDir() {
err := artifactsDir(
uploader,
logger,
worktree,
artifacts,
fullPath,
root,
)
if err != nil {
return err
}
continue
}
relPath, err := filepath.Rel(root, fullPath)
if err != nil {
return fmt.Errorf("failed to rel path: %w", err)
}
for _, artifact := range artifacts {
matched, err := filepath.Match(artifact, relPath)
if err != nil {
continue
}
if !matched {
continue
}
logger.Printf("Uploading artifact \"%s\"", file.Name())
reader, err := worktree.Open(fullPath)
if err != nil {
logger.Printf("Failed to upload artifact \"%s\": %s", file.Name(), err)
continue
}
defer reader.Close()
err = uploader.Upload(file.Name(), reader)
if err != nil {
logger.Printf("Failed to upload artifact \"%s\": %s", file.Name(), err)
continue
}
}
}
return nil
}