-
Notifications
You must be signed in to change notification settings - Fork 1
/
artifact.go
55 lines (47 loc) · 1.06 KB
/
artifact.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
const (
fileArtifactV1 string = "fileUpload/v1"
)
type (
File struct {
Name string `json:"name"`
URL string `json:"url"`
}
Data struct {
FileArtifacts []File `json:"fileArtifacts"`
}
FileArtifact struct {
Kind string `json:"kind"`
Data Data `json:"data"`
}
)
func writeArtifactFile(files []File, artifactFilePath string) error {
artifact := FileArtifact{
Kind: fileArtifactV1,
Data: Data{
FileArtifacts: files,
},
}
b, err := json.MarshalIndent(artifact, "", "\t")
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to marshal output %+v", artifact))
}
dir := filepath.Dir(artifactFilePath)
err = os.MkdirAll(dir, 0644)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory for artifact file", dir))
}
err = ioutil.WriteFile(artifactFilePath, b, 0644)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to write artifact to artifact file %s", artifactFilePath))
}
return nil
}