/
assets.go
79 lines (68 loc) · 1.5 KB
/
assets.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
package assets
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/Nightapes/go-semantic-release/pkg/config"
"github.com/pkg/errors"
)
// Set struct
type Set struct {
assets []*Asset
repository string
algorithm string
}
//New container for assets
func New(repository, algorithm string) *Set {
return &Set{
assets: []*Asset{},
repository: repository,
algorithm: algorithm,
}
}
// Add assets to the list
func (s *Set) Add(assetConfigs ...config.Asset) error {
for _, assetConfig := range assetConfigs {
asset, err := NewAsset(s.repository, assetConfig, s.algorithm)
if err != nil {
return err
}
s.assets = append(s.assets, asset)
}
return nil
}
func (s *Set) All() []*Asset {
return s.assets
}
func (s *Set) GenerateChecksum() error {
checksumFile, err := ioutil.TempFile(os.TempDir(), "checksum.*.txt")
if err != nil {
return errors.Wrap(err, "Could not generate tmp file for checksum")
}
defer checksumFile.Close()
lines := []string{}
for _, asset := range s.assets {
checksum, err := asset.getChecksum()
if err != nil {
return err
}
lines = append(lines, fmt.Sprintf("%s %s", checksum, asset.GetName()))
}
w := bufio.NewWriter(checksumFile)
for _, line := range lines {
fmt.Fprintln(w, line)
}
filePath, err := filepath.Abs(checksumFile.Name())
if err != nil {
return err
}
s.assets = append(s.assets, &Asset{
path: filePath,
name: "checksum.txt",
isCompressed: false,
algorithm: "",
})
return w.Flush()
}