-
Notifications
You must be signed in to change notification settings - Fork 18
/
attachment.go
61 lines (52 loc) · 1.37 KB
/
attachment.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
package allure
import (
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"log"
"os"
)
type attachment struct {
uuid string
Name string `json:"name"`
Source string `json:"source"`
Type MimeType `json:"type"`
content []byte
}
type MimeType string
func AddAttachment(name string, mimeType MimeType, content []byte) error {
attachment := newAttachment(name, mimeType, content)
err := attachment.writeAttachmentFile()
if err != nil {
return errors.Wrap(err, "Failed to create an attachment file")
}
if node, ok := ctxMgr.GetValue(nodeKey); ok {
node.(hasAttachments).addAttachment(*attachment)
}
return nil
}
func (a *attachment) writeAttachmentFile() error {
resultsPathEnv := os.Getenv(resultsPathEnvKey)
ensureFolderCreated()
if resultsPathEnv == "" {
log.Printf("%s environment variable cannot be empty\n", resultsPathEnvKey)
}
if resultsPath == "" {
resultsPath = fmt.Sprintf("%s/allure-results", resultsPathEnv)
}
a.Source = fmt.Sprintf("%s-attachment", a.uuid)
err := ioutil.WriteFile(fmt.Sprintf("%s/%s-attachment", resultsPath, a.uuid), a.content, 0777)
if err != nil {
return errors.Wrap(err, "Failed to write in file")
}
return nil
}
func newAttachment(name string, mimeType MimeType, content []byte) *attachment {
result := &attachment{
uuid: generateUUID(),
content: content,
Name: name,
Type: mimeType,
}
return result
}