deb-simple/apt.go

125 lines
2.3 KiB
Go
Raw Normal View History

2018-10-14 08:17:04 +00:00
package main
2017-06-11 06:14:13 +00:00
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
2020-06-14 08:29:16 +00:00
"github.com/spf13/afero"
"golang.org/x/crypto/openpgp"
"io"
2020-06-14 08:29:16 +00:00
"meow.tf/deb-simple/deb/release"
"path"
"strings"
"time"
2017-06-11 06:14:13 +00:00
)
2017-09-11 02:58:50 +00:00
func createRelease(config Conf, distro string) error {
2020-06-14 08:29:16 +00:00
outfile, err := fs.Create(path.Join(config.DistPath(distro), "Release"))
2017-06-11 06:14:13 +00:00
if err != nil {
return fmt.Errorf("failed to create Release: %s", err)
}
2017-06-11 06:14:13 +00:00
defer outfile.Close()
2017-09-11 02:58:50 +00:00
r := &release.Release{
Suite: distro,
Architectures: config.Repo.ArchitectureNames(),
Components: []string{"main"},
Date: time.Now(),
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
for _, arch := range config.Repo.ArchitectureNames() {
absolutePath := path.Join(config.DistPath(distro), "main", "binary-" + arch)
2017-06-11 06:14:13 +00:00
2020-06-14 08:29:16 +00:00
list, err := afero.ReadDir(fs, absolutePath)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if err != nil {
continue
}
2017-06-11 06:14:13 +00:00
basePath := path.Join("main", "binary-" + arch)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
for _, file := range list {
filePath := path.Join(absolutePath, file.Name())
2017-06-11 06:14:13 +00:00
fileLocalPath := path.Join(basePath, file.Name())
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
fileLocalPath = strings.Replace(fileLocalPath, "\\", "/", -1)
2017-06-11 06:14:13 +00:00
2020-06-14 08:29:16 +00:00
f, err := fs.Open(filePath)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if err != nil {
return err
}
2017-06-11 06:14:13 +00:00
2020-06-14 08:29:16 +00:00
var size = file.Size()
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if size == 0 {
2020-06-14 08:29:16 +00:00
if stat, err := fs.Stat(filePath); err == nil {
2017-09-11 02:58:50 +00:00
size = stat.Size()
}
2017-06-11 06:14:13 +00:00
}
2017-09-11 02:58:50 +00:00
var (
md5hash = md5.New()
sha1hash = sha1.New()
sha256hash = sha256.New()
)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
w := io.MultiWriter(md5hash, sha1hash, sha256hash)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
io.Copy(w, f)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
repoFile := &release.File{
Name: fileLocalPath,
Size: size,
MD5Sum: hex.EncodeToString(md5hash.Sum(nil)),
SHA1: hex.EncodeToString(sha1hash.Sum(nil)),
SHA256: hex.EncodeToString(sha256hash.Sum(nil)),
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
r.Append(repoFile)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
f.Close()
f = nil
2017-06-11 06:14:13 +00:00
}
2017-09-11 02:58:50 +00:00
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
r.Write(outfile)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if pgpEntity != nil {
return signRelease(config, distro)
2017-06-11 06:14:13 +00:00
}
2017-09-11 02:58:50 +00:00
return nil
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
func signRelease(config Conf, distro string) error {
distPath := config.DistPath(distro)
2017-06-11 06:14:13 +00:00
2020-06-14 08:29:16 +00:00
f, err := fs.Open(path.Join(distPath, "Release"))
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if err != nil {
return fmt.Errorf("failed to read Release: %s", err)
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
defer f.Close()
2020-06-14 08:29:16 +00:00
gpgfile, err := fs.Create(path.Join(distPath, "Release.gpg"))
2017-09-11 02:58:50 +00:00
if err != nil {
return fmt.Errorf("failed to create Release.gpg: %s", err)
}
defer gpgfile.Close()
if err := openpgp.ArmoredDetachSignText(gpgfile, pgpEntity, f, nil); err != nil {
return fmt.Errorf("failed to sign Release: %s", err)
2017-06-11 06:14:13 +00:00
}
return nil
}