2020-03-21 15:47:03 +01:00
|
|
|
package assets
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"github.com/Nightapes/go-semantic-release/pkg/config"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
2020-03-24 19:51:18 +01:00
|
|
|
// Set struct
|
|
|
|
|
type Set struct {
|
2020-03-21 15:47:03 +01:00
|
|
|
Assets []*Asset
|
|
|
|
|
repository string
|
|
|
|
|
algorithm string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//New container for assets
|
2020-03-24 19:51:18 +01:00
|
|
|
func New(repository, algorithm string) *Set {
|
|
|
|
|
return &Set{
|
2020-03-21 15:47:03 +01:00
|
|
|
Assets: []*Asset{},
|
|
|
|
|
repository: repository,
|
|
|
|
|
algorithm: algorithm,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add assets to the list
|
2020-03-24 19:51:18 +01:00
|
|
|
func (s *Set) Add(assetConfigs ...config.Asset) error {
|
2020-03-21 15:47:03 +01:00
|
|
|
for _, assetConfig := range assetConfigs {
|
2020-03-24 19:51:18 +01:00
|
|
|
asset, err := NewAsset(s.repository, assetConfig, s.algorithm)
|
2020-03-21 15:47:03 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-03-24 19:51:18 +01:00
|
|
|
s.Assets = append(s.Assets, asset)
|
2020-03-21 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 19:51:18 +01:00
|
|
|
func (s *Set) All() []*Asset {
|
|
|
|
|
return s.Assets
|
2020-03-21 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-24 19:51:18 +01:00
|
|
|
func (s *Set) GenerateChecksum() error {
|
2020-03-21 15:47:03 +01:00
|
|
|
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{}
|
2020-03-24 19:51:18 +01:00
|
|
|
for _, asset := range s.Assets {
|
2020-03-21 15:47:03 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 19:51:18 +01:00
|
|
|
s.Assets = append(s.Assets, &Asset{
|
2020-03-21 15:47:03 +01:00
|
|
|
path: filePath,
|
|
|
|
|
name: "checksum.txt",
|
|
|
|
|
isCompressed: false,
|
|
|
|
|
algorithm: "",
|
|
|
|
|
})
|
|
|
|
|
return w.Flush()
|
|
|
|
|
|
|
|
|
|
}
|